Skip to content

Commit 6d8fc34

Browse files
committed
Stop catching parser errors
Having any exception on the parser stack trapped requires any callback/event going back to the user to be wrapped in process.nextTick in order for it to not be trapped by try...catch block which is very annoying. This also makes it hard to guarantee row streaming pause() / resume() to work properly as invoking the 'result' events in a nextTick means that other 'result' events may fire, even if the first one calls pause() (because the other ones are already scheduled as nextTicks). Even so it would be nice to allow the user to handle potentially unknown problems in the parser gracefully, it is ultimatively not worth the tradeoffs that are involved. Going forward any unknown parser defects will cause hard exceptions which may only be trapped by using domains or 'uncaughtException'. This patch is also done in preparation for implementing exception safety, so that it becomes possible to use a shared mysql connection across multiple domains.
1 parent 91948cf commit 6d8fc34

File tree

4 files changed

+9
-27
lines changed

4 files changed

+9
-27
lines changed

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ object. Additionally they come with two properties:
423423

424424
* `err.code`: Either a [MySQL server error][] (e.g.
425425
`'ER_ACCESS_DENIED_ERROR'`), a node.js error (e.g. `'ECONNREFUSED'`) or an
426-
internal error (e.g. `'PROTOCOL_PARSER_EXCEPTION'`).
426+
internal error (e.g. `'PROTOCOL_CONNECTION_LOST'`).
427427
* `err.fatal`: Boolean, indicating if this error is terminal to the connection
428428
object.
429429

lib/protocol/Protocol.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,8 @@ function Protocol(options) {
2727
}
2828

2929
Protocol.prototype.write = function(buffer) {
30-
try {
31-
this._parser.write(buffer);
32-
return true;
33-
} catch (err) {
34-
err.code = err.code || 'PROTOCOL_PARSER_EXCEPTION';
35-
err.fatal = true;
36-
37-
this._delegateError(err);
38-
}
39-
40-
return false;
30+
this._parser.write(buffer);
31+
return true;
4132
};
4233

4334
Protocol.prototype.handshake = function(cb) {

lib/protocol/sequences/Sequence.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,17 @@ Sequence.prototype.end = function(err) {
5050
}
5151

5252
this._ended = true;
53-
var self = this;
54-
var args = arguments;
5553

5654
if (err) {
5755
this._addLongStackTrace(err);
56+
this.emit('error', err);
5857
}
5958

59+
if (this._callback) {
60+
this._callback.apply(this, arguments);
61+
}
6062

61-
// Escape stack (so try..catch in Protocol#write does not interfer here)
62-
process.nextTick(function() {
63-
if (err) {
64-
self.emit('error', err);
65-
}
66-
67-
if (self._callback) {
68-
self._callback.apply(self, args);
69-
}
70-
71-
self.emit('end');
72-
});
63+
this.emit('end');
7364
};
7465

7566
Sequence.prototype['OkPacket'] = function(packet) {

test/integration/test-query-after-end-without-callback.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ connection.end(function(err) {
1111
});
1212

1313
var err;
14-
connection.query('SELECT 1');
1514
connection.on('error', function(_err) {
1615
err = _err;
1716
});
17+
connection.query('SELECT 1');
1818

1919
process.on('exit', function() {
2020
assert.equal(didEnd, true);

0 commit comments

Comments
 (0)