Unhandled 'error' event and PROTOCOL_CONNECTION_LOST
See original GitHub issueHello, 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:
- Created 4 years ago
- Reactions:2
- Comments:11 (4 by maintainers)
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!
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.