sidorares / sidorares/node-mysql2

Unhandled 'error' event and PROTOCOL_CONNECTION_LOST

Open
#1,085 11 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

Hello,
First of all, thank you for your work building this amazing library!

I am having the following problem when trying to connect on purpose for testing into a server using an invalid username:

events.js:187
      throw er; // Unhandled 'error' event
      ^
Error: Connection lost: The server closed the connection.
    at Socket.<anonymous> (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:88:31)
    at Socket.emit (events.js:210:5)
    at TCP.<anonymous> (net.js:658:12)
Emitted 'error' event on PoolConnection instance at:
    at PoolConnection._notifyError (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:218:12)
    at Socket.<anonymous> (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:94:12)
    at Socket.emit (events.js:210:5)
    at TCP.<anonymous> (net.js:658:12) {
  fatal: true,
  code: 'PROTOCOL_CONNECTION_LOST'
}

This is how I setup my connection pool:

this.connectionPool = mysql.createPool({
                host: this.cfg.dbServer,
                user: this.cfg.dbUserName,
                database: this.cfg.dbSchema,
                password: this.cfg.dbPassword,                
        
             }).promise();

This is how I attach the error listener, I use the following techniques (tried both):

this.connectionPool.on('connection', (connection) => {
                console.log('[MYSQL-DRIVER]-NEW CONNECTION ' + connection.threadId);
                connection.on('error', function(err){
                    console.log('[MYSQL-DRIVER]-PROBLEM: ' + err);
                })
            });

Or:
this.connectionPool.on('error', function(err) {
                console.log("[MYSQL-DRIVER]-PROBLEM(ATTACHED ON POOL): " + err);
             });

I have a simple test routine, so everytime I start my nodeJS server, after the pool is created, I run the following:

await this.connTest().then((result)=>{
                this.connected = 1;                
            }).catch((err) => {
                this.connected = 0;
            });

connTest() {
        return new Promise((resolve, reject) => {
            this.connectionPool.execute('SELECT 1 + 1 AS SOLUTION;', function (error, resulds, fields) {
                if (error) reject();
                resolve();
            })
        });
    }

Now the strange part. I am developing in a Windows 10 machine with MySQL hosted on another machine on the network. This problem “Unhandled 'error' event” does NOT always occur. Sometimes I work for hours and disconnections are handled JUST FINE by the ATTACHED ERROR FUNCTION. I repeat: sometimes the disconnections ARE handled by the handlers!

I don´t know exactly what changes for the behavior appear, but it appears to be after I restart MySQL server.

I decided to turn on the mysql2 library debug, to see what kind of information it shows, during the times the error is happening. Everytime I run my test routine, just before the error, I see this:

raw: 0a382e302e313800c50300001e014540076c0f0f00ffffe00200ffc71500000000000000000000235744157b3f7557525e27020063616368696e675f736861325f70617373776f726400
Trace
    at PoolConnection.handlePacket (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:393:17)
    at PacketParser.onPacket (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:70:12)
    at PacketParser.executeStart (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.<anonymous> (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:77:25)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
0 undefined ==> undefined#unknown name(0,,78)
 raw: fe7368613235365f70617373776f726400717e517834167a03770763452506255719282e4100
Trace
    at PoolConnection.handlePacket (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:393:17)
    at PacketParser.onPacket (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:70:12)
    at PacketParser.executeStart (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\packet_parser.js:75:16)
    at Socket.<anonymous> (G:\dev\giamcis\gwsi\aarm-api-n\node_modules\mysql2\lib\connection.js:77:25)
    at Socket.emit (events.js:210:5)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
0 965 ==> undefined#unknown name(2,,42)

I wonder if is there any reason why the emitted error in this situation does not arrive at my error handler?

Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the invalid-username connection against a restarted MySQL server, comparing the pool and connection listeners shown in the issue. Inspect node_modules/mysql2/lib/connection.js around the reported lines 88, 94, 218, and 393 to trace how PROTOCOL_CONNECTION_LOST is emitted; done means the failure is consistently delivered to an attached handler without an unhandled error.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js
Domain
backend, database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.