Skip to content

Commit

Permalink
Handle MySQL servers not closing TCP connection after QUIT -> OK exch…
Browse files Browse the repository at this point in the history
…ange

fixes #1277
  • Loading branch information
dougwilson committed Nov 13, 2015
1 parent e578e8c commit 26d3f04
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changes.md
Expand Up @@ -6,6 +6,7 @@ you spot any mistakes.

## HEAD

* Handle MySQL servers not closing TCP connection after QUIT -> OK exchange #1277
* Minor SqlString Date to string performance improvement #1233
* Support Node.js 4.x
* Support Node.js 5.x
Expand Down
12 changes: 9 additions & 3 deletions lib/protocol/Protocol.js
Expand Up @@ -84,7 +84,14 @@ Protocol.prototype.quit = function quit(options, callback) {
options = {};
}

return this._quitSequence = this._enqueue(new Sequences.Quit(options, callback));
var self = this;
var sequence = this._enqueue(new Sequences.Quit(options, callback));

sequence.on('end', function () {
self.end();
});

return this._quitSequence = sequence;
};

Protocol.prototype.end = function() {
Expand All @@ -93,8 +100,7 @@ Protocol.prototype.end = function() {
}
this._ended = true;

var expected = (this._quitSequence && this._queue[0] === this._quitSequence);
if (expected) {
if (this._quitSequence && (this._quitSequence._ended || this._queue[0] === this._quitSequence)) {
this._quitSequence.end();
this.emit('end');
return;
Expand Down
5 changes: 3 additions & 2 deletions test/FakeServer.js
Expand Up @@ -330,8 +330,9 @@ FakeConnection.prototype._parsePacket = function(header) {
this._parser.resetPacketNumber();
break;
case Packets.ComQuitPacket:
this.emit('quit', packet);
this._socket.end();
if (!this.emit('quit', packet)) {
this._socket.end();
}
break;
default:
throw new Error('Unexpected packet: ' + Packet.name)
Expand Down
22 changes: 22 additions & 0 deletions test/unit/connection/test-quit-ok-packet.js
@@ -0,0 +1,22 @@
var assert = require('assert');
var common = require('../../common');
var connection = common.createConnection({port: common.fakeServerPort});

var server = common.createFakeServer();

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

connection.end(function (err) {
assert.ifError(err);
server.destroy();
});
});

server.on('connection', function (conn) {
conn.handshake();
conn.on('quit', function () {
conn._sendPacket(new common.Packets.OkPacket());
conn._parser.resetPacketNumber();
});
});

0 comments on commit 26d3f04

Please sign in to comment.