Skip to content

Commit 1dd7caf

Browse files
committed
Merge pull request brianc#316 from brianc/ignore-socket-hangup
ignore socket hangup. fixes brianc#314
2 parents 95b1c75 + 6e3cc79 commit 1dd7caf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/connection.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var Connection = function(config) {
1818
this.parsedStatements = {};
1919
this.writer = new Writer();
2020
this.ssl = config.ssl || false;
21+
this._ending = false;
2122
};
2223

2324
util.inherits(Connection, EventEmitter);
@@ -37,6 +38,11 @@ Connection.prototype.connect = function(port, host) {
3738
});
3839

3940
this.stream.on('error', function(error) {
41+
//don't raise ECONNRESET errors - they can & should be ignored
42+
//during disconnect
43+
if(self._ending && error.code == 'ECONNRESET') {
44+
return;
45+
}
4046
self.emit('error', error);
4147
});
4248

@@ -263,6 +269,7 @@ Connection.prototype.end = function() {
263269
//0x58 = 'X'
264270
this.writer.add(emptyBuffer);
265271
this._send(0x58);
272+
this._ending = true;
266273
};
267274

268275
Connection.prototype.describe = function(msg, more) {

test/unit/connection/error-tests.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
var helper = require(__dirname + '/test-helper');
22
var Connection = require(__dirname + '/../../../lib/connection');
3-
var con = new Connection({stream: new MemoryStream()});
43
test("connection emits stream errors", function() {
4+
var con = new Connection({stream: new MemoryStream()});
55
assert.emits(con, 'error', function(err) {
66
assert.equal(err.message, "OMG!");
77
});
88
con.connect();
99
con.stream.emit('error', new Error("OMG!"));
1010
});
11+
12+
test('connection emits ECONNRESET errors during normal operation', function() {
13+
var con = new Connection({stream: new MemoryStream()});
14+
con.connect();
15+
assert.emits(con, 'error', function(err) {
16+
assert.equal(err.code, 'ECONNRESET');
17+
});
18+
var e = new Error('Connection Reset');
19+
e.code = 'ECONNRESET';
20+
con.stream.emit('error', e);
21+
});
22+
23+
test('connection does not emit ECONNRESET errors during disconnect', function() {
24+
var con = new Connection({stream: new MemoryStream()});
25+
con.connect();
26+
var e = new Error('Connection Reset');
27+
e.code = 'ECONNRESET';
28+
con.end();
29+
con.stream.emit('error', e);
30+
});

0 commit comments

Comments
 (0)