Skip to content

Commit 5e43c5c

Browse files
committed
Fix pool leaking connections after conn.changeUser
fixes #833
1 parent bce163c commit 5e43c5c

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

Changes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ This file is a manually maintained list of changes for each release. Feel free
44
to add your changes here when sending pull requests. Also send corrections if
55
you spot any mistakes.
66

7+
## HEAD
8+
9+
* Fix pool leaking connections after `conn.changeUser` #833
10+
711
## v2.3.1 (2014-05-26)
812

913
* Add database errors to error constants

lib/Pool.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Pool.prototype.releaseConnection = function releaseConnection(connection) {
9898

9999
if (connection._purge) {
100100
// purge connection from pool
101-
this._removeConnection(connection);
101+
this._purgeConnection(connection);
102102
return;
103103
} else if (this._freeConnections.indexOf(connection) !== -1) {
104104
// connection already in free connection pool
@@ -190,6 +190,18 @@ Pool.prototype.query = function (sql, values, cb) {
190190
});
191191
};
192192

193+
Pool.prototype._purgeConnection = function _purgeConnection(connection) {
194+
var pool = this;
195+
196+
connection._realEnd(function(err) {
197+
if (err) {
198+
connection.destroy();
199+
}
200+
201+
pool._removeConnection(connection);
202+
});
203+
};
204+
193205
Pool.prototype._removeConnection = function(connection) {
194206
var index;
195207

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,61 @@
11
var assert = require('assert');
22
var common = require('../../common');
33
var pool = common.createPool({
4-
connectionLimit : 1,
4+
connectionLimit : 2,
55
user : 'user_1',
66
port : common.fakeServerPort
77
});
88

9+
var closed = 0;
910
var server = common.createFakeServer();
1011
var thread = 0;
1112

1213
server.listen(common.fakeServerPort, function(err) {
1314
assert.ifError(err);
1415

16+
var conn0;
1517
pool.getConnection(function(err, conn) {
1618
assert.ifError(err);
1719
assert.strictEqual(conn.threadId, 1);
18-
conn.release();
20+
conn0 = conn;
1921
});
2022

2123
pool.getConnection(function(err, conn) {
2224
assert.ifError(err);
23-
assert.strictEqual(conn.threadId, 1);
25+
assert.strictEqual(conn.threadId, 2);
2426

2527
conn.changeUser({user: 'user_2'}, function(err) {
2628
assert.ifError(err);
27-
assert.strictEqual(conn.threadId, 1);
29+
assert.strictEqual(conn.threadId, 2);
2830
conn.release();
31+
conn0.release();
2932
});
3033
});
3134

32-
pool.getConnection(function(err, conn) {
35+
pool.getConnection(function(err, conn1) {
3336
assert.ifError(err);
34-
assert.strictEqual(conn.threadId, 2);
35-
conn.destroy();
36-
server.destroy();
37+
assert.strictEqual(conn1.threadId, 1);
38+
39+
pool.getConnection(function(err, conn2) {
40+
assert.ifError(err);
41+
assert.strictEqual(conn2.threadId, 3);
42+
conn1.release();
43+
conn2.release();
44+
45+
pool.end(function(err) {
46+
assert.ifError(err);
47+
assert.strictEqual(closed, 3);
48+
server.destroy();
49+
});
50+
});
3751
});
3852
});
3953

4054
server.on('connection', function(incomingConnection) {
4155
incomingConnection.handshake({
4256
threadId: ++thread
4357
});
58+
incomingConnection.on('quit', function() {
59+
closed++;
60+
});
4461
});

0 commit comments

Comments
 (0)