Skip to content

Fix for issue #964: connection error prevents pool from ending connection #964 #974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/PoolConnection.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ function PoolConnection(pool, options) {
// the connection to end as well, thus we only need to watch for the end event
// and we will be notified of disconnects.
this.on('end', this._removeFromPool);
this.on('error', this._removeFromPool);

// we need this listener to avoid an exception
// but we can't call removeFromPool on error,
// otherwise the connection will be removed but not destroyed
// which may cause a stall
this.on('error', function() {});
}

PoolConnection.prototype.changeUser = function changeUser(options, callback) {
Expand Down
40 changes: 40 additions & 0 deletions test/unit/pool/test-query-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var assert = require('assert');
var common = require('../../common');
var pool = common.createPool({
connectionLimit : 1,
port : common.fakeServerPort
});
var server = common.createFakeServer();


server.listen(common.fakeServerPort, function(err){

pool.getConnection(function(err, conn) {

var socket_closed = false;

// socket should be closed after pool.end()
conn._socket.on('close', function() {
server.destroy();
socket_closed = true;
});

// an open socket will prevent the script from ending,
// hence the timeout
setTimeout( function() {
assert(socket_closed, true);
server.destroy();
}, 5000);

// attempt to make an invalid query
var seq = conn.query('');

// NOT listening to 'error' from seq

seq.on('end', function() {
conn.release();
pool.end();
});

});
});