question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled 'error' event and PROTOCOL_CONNECTION_LOST

See original GitHub issue

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!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ianbalecommented, Jun 15, 2020

You were too quick! I just edited my comment above having just checked the logs that were produced in the last 30 mins… That was indeed the cause!

0reactions
chris-heathwoodcommented, Jun 19, 2021

Just in case someone else finds this thread I had an issue with the PROTOCOL_CONNECTION_LOST on my local machine and in github actions and it was just me being too fast.

Waiting for 10 seconds or so and then try the connection worked.

Read more comments on GitHub >

github_iconTop Results From Across the Web

nodejs mysql Error: Connection lost The server closed the ...
In your code i am missing the parts after connection = mysql. ... ensures that the connection will remain alive and PROTOCOL_CONNECTION_LOST does...
Read more >
Unhandled 'error' event with connections · Issue #2096 - GitHub
So I think you're getting the unhandled error message because I don't see you adding an 'error' event handler to the connection object....
Read more >
nodejs mysql Error Connection lost The server closed the ...
When I use node mysql, an error appears between 12:00 and 2:00, stating that the server has terminated the TCP connection.
Read more >
Connector/Node.js - MySQL Forums
So I can successfully update a database using the following code in my node server: var mysql = require('mysql'); var con = mysql....
Read more >
Error: Connection lost: The server closed the connection
After using the MYSQL connection the project ends. I know I am not closing the connection and I get this error events.js:183 throw...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found