Repository navigation
Closed
Description
I just switched to using mysql2 so I can get promise support but have an issue with pools that I didn't have before when using mysql.
I have the following code...
const tempPool = mysql.createPool({
host: this.configuration[i].server,
user: this.credentials.user,
password: this.credentials.password,
database: 'testDB',
connectionLimit: connectionLimit,
debug: false,
timezone: 'utc',
supportBigNumbers: true,
bigNumberStrings: true
});
_this.databaseConnections[name] = tempPool;
tempPool.getConnection().then(function (connection) {
connection.release();
}).catch(function (err) {
console.log(err);
});
This creates a pool, a quick connect to test and then waits. If the mySQL server restarts once connected I get the following error.
events.js:182
throw er; // Unhandled 'error' event
^
Error: Connection lost: The server closed the connection.
at Socket.<anonymous> (/Users/me/Documents/code/node-archiver/node_modules/mysql2/lib/connection.js:113:35)
at emitNone (events.js:110:20)
at Socket.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1047:12)
at _combinedTickCallback (internal/process/next_tick.js:102:11)
at process._tickCallback (internal/process/next_tick.js:161:9)
I have tried attaching an error event handler to tempPool but still get the unhandled error exception.
Any ideas where I need to add the handler!?
Metadata
Metadata
Assignees
Labels
No labels
Activity
sidorares commentedon Jul 8, 2017
the code looks right.
what's in your connection limit?
Also if you set
debugconnection flag totrueit will enable verbose logging from driverleearmstrong commentedon Jul 10, 2017
50 is in the connection limit, I have just used this standalone example to replicate without all my code around it and still get it.
With debug set to true this is the log... The unhandled exception happens shortly after a stop of the MySQL service. Hopefully you can replicate this also
sidorares commentedon Jul 10, 2017
I'm afraid this is type of error you have to handle in your app
While pool tries to remove connection when there is 'fatal error' ( underlying socket closed etc ) sometimes it does not get notified about that and you see error in the query callback after timeout
This is expected in case of total radio silence from mysql server ( there is no way to know if it's down except for timing out ) However on my windows machine I don't even see 'error' event on node net stream when I turn off wifi or eject ethernect cable
Possible work around is to add 'heartbeat' to your pool ( setInterval on on 'connect' event and stop when connection closed) so that timeout error triggered early and connection removed from pool
leearmstrong commentedon Jul 10, 2017
I would handle in my app if I could attach to an error event handler which it doesn't seem possible to do in mysql2?
Using mysql the following code doesn't error at all when the sql server goes away.
My test is restarting the mysql service, I am not pulling a cable or network. Simply stopping the mysql server process.
Like I said, happy to handle the logic in my app but I can't seem to attach to an error event to then handle it which then panics node
sidorares commentedon Jul 10, 2017
Do you see same behaviour if you use non-promise mysql2 client similar to your last example?
leearmstrong commentedon Jul 10, 2017
Good point, sorry I had not tried that before!
So I ran the code and no unhandled exception error at all and the script as you would expect (and same behaviour as "mysql") is that the script ends without an error.
sidorares commentedon Jul 10, 2017
ok I assume there might be a bug in a way promise wrapper handles pool errors, I'll try to Isolate that ( if you happen to discover anything before me feel free to add more info! )
leearmstrong commentedon Jul 10, 2017
Yeah sure no problem, I'd love to help with the debugging if at all possible, if you can offer any advice on where to look I will have a go!
I wonder if it's somewhere in pool.js where it doesn't handle the 'error' exception but not sure why it works in the non promise model.
I'll give it some time today as I'd really like to get this solved :D
leearmstrong commentedon Jul 10, 2017
Ok so far I have seen that the PoolConnection is getting the error callback that is being bubbled up in this event.
The removeFromPool seems to be running so I think the error is being handled it is almost as if the symantics around the 'error' event being handled are not being captured by node?
sidorares commentedon Jul 10, 2017
yes, that what I was talking about with "no error on socket even when I unplug cable" - not sure if this is I doing something wrong or node / libuv feature
leearmstrong commentedon Jul 10, 2017
Do you get the error when restarting the MySQL server process as well?
I'm starting to get out of my depth a little but trying to understand why it doesn't happen in the non Promise version!
sidorares commentedon Jul 10, 2017
looks like not, or at least not always
leearmstrong commentedon Jul 10, 2017
Ok well I get this on macOS and Linux so please let me know if I can capture any data for you! My node is ok but not as good as yours!!
sidorares commentedon Jul 10, 2017
very related discussion in #447
a0000778 commentedon Aug 5, 2017
I think this issue has been fixed in #620.
leearmstrong commentedon Aug 12, 2017
Yes it does look like that. Is it in a published NPM version yet? @sidorares
sidorares commentedon Aug 12, 2017
yes #620 is in 1.4.0
leearmstrong commentedon Aug 12, 2017
leearmstrong commentedon Aug 16, 2017
I have run my test scripts with 1.4.1 and this is fixed now.
juan-ferrer-toribio commentedon Feb 13, 2018
I think this bug it's open again at version 1.5.2
With the following code:
Sometimes, when connection isn't established due to timeout, I can't capture the error in the catch, it goes to the global scope:
sidorares commentedon Feb 14, 2018
@juan-ferrer-toribio are you sure it's 1.5.2? Can't replicate this, get errors caught in
catch(e)block and stack trace points tocreateConnection()line ( this is because we save original callsite here -node-mysql2/promise.js
Line 31 in 415959b
I'm testing with local tcp server ( and no mysql server running ) to simulate timeout:
juan-ferrer-toribio commentedon Feb 25, 2018
Yes, it was 1.5.2 under Debian stretch with node 8.9.0.
I tested it under windows 10 with same mysql2 version (1.5.2) and same node version (8.9.0) and the code works as spected (Errors are always captured in catch).
Researching a litle bit more I found the problem is related to the unhandled 'error' event emitted by the conn object, but it only happens under Linux.
As a workaround i used this line of code after establishing connection.
conn.on('error', () => {});digiperfect-tech commentedon May 26, 2018
I get a similar error in version 1.5.3
Node version
8.11.2The code I have is (using typescript):
We are rewriting code using async/await hence temporarily we have 2 executeQuery styles. Currently
executeQueryWithAwaitis not being used.