Skip to content

Commit 53ae1f3

Browse files
authored
Merge pull request #372 from sidorares/pool-connect-time-error
pool connection time error is not propagated correctly to users and as a result process crashes
2 parents 2674803 + 635ea36 commit 53ae1f3

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed

lib/commands/client_handshake.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ ClientHandshake.prototype.handshakeInit = function (helloPacket, connection) {
7575
var command = this;
7676

7777
this.on('error', function (e) {
78+
connection._fatalError = e;
7879
connection._protocolError = e;
7980
});
8081

lib/connection.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ function Connection (opts)
108108
connection.threadId = handshakeCommand.handshake.connectionId;
109109
connection.emit('connect', handshakeCommand.handshake);
110110
});
111+
handshakeCommand.on('error', function(err) {
112+
connection._notifyError(err);
113+
});
111114
this.addCommand(handshakeCommand);
112115
}
113116
}

lib/pool.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,10 @@ Pool.prototype.query = function (sql, values, cb) {
130130

131131
this.getConnection(function (err, conn) {
132132
if (err) {
133-
cmdQuery.emit('error', err);
134133
if (typeof cmdQuery.onResult === 'function') {
135134
cmdQuery.onResult(err);
135+
} else {
136+
cmdQuery.emit('error', err);
136137
}
137138
return;
138139
}

lib/pool_connection.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ function PoolConnection (pool, options) {
1313
// When a fatal error occurs the connection's protocol ends, which will cause
1414
// the connection to end as well, thus we only need to watch for the end event
1515
// and we will be notified of disconnects.
16-
this.on('end', this._removeFromPool);
17-
this.on('error', this._removeFromPool);
16+
var connection = this;
17+
this.on('end', function(err) {
18+
this._removeFromPool();
19+
});
20+
this.on('error', function(err) {
21+
this._removeFromPool();
22+
});
1823
}
1924

2025
PoolConnection.prototype.release = function () {
@@ -38,11 +43,11 @@ PoolConnection.prototype.end = function () {
3843
};
3944

4045
PoolConnection.prototype.destroy = function () {
41-
this._removeFromPool(this);
46+
this._removeFromPool();
4247
return Connection.prototype.destroy.apply(this, arguments);
4348
};
4449

45-
PoolConnection.prototype._removeFromPool = function (connection) {
50+
PoolConnection.prototype._removeFromPool = function () {
4651
if (!this._pool || this._pool._closed) {
4752
return;
4853
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
var util = require('util');
2+
var mysql = require('../../index.js');
3+
var Command = require('../../lib/commands/command.js');
4+
var Packets = require('../../lib/packets/index.js');
5+
6+
var assert = require('assert');
7+
8+
var server = mysql.createServer(function (conn) {
9+
conn.serverHandshake({
10+
protocolVersion: 10,
11+
serverVersion: '5.6.10',
12+
connectionId: 1234,
13+
statusFlags: 2,
14+
characterSet: 8,
15+
capabilityFlags: 0xffffff,
16+
authCallback: function (params, cb) {
17+
cb(null, {message: 'too many connections', code: 1040});
18+
}
19+
});
20+
});
21+
22+
var err1, err2;
23+
24+
var portfinder = require('portfinder');
25+
portfinder.getPort(function (err, port) {
26+
27+
server.listen(port);
28+
var conn = mysql.createConnection({
29+
user: 'test_user',
30+
password: 'test',
31+
database: 'test_database',
32+
port: port
33+
});
34+
conn.on('error', function (err) {
35+
err1 = err;
36+
});
37+
38+
var pool = mysql.createPool({
39+
user: 'test_user',
40+
password: 'test',
41+
database: 'test_database',
42+
port: port
43+
});
44+
45+
pool.query('test sql', function (err, res, rows) {
46+
err2 = err;
47+
pool.end();
48+
server.close();
49+
});
50+
51+
});
52+
53+
process.on('exit', function () {
54+
assert.equal(err1.errno, 1040);
55+
assert.equal(err2.errno, 1040);
56+
});

0 commit comments

Comments
 (0)