Merge pull request #316 from brianc/ignore-socket-hangup

ignore socket hangup. fixes #314
This commit is contained in:
Brian C 2013-03-29 08:36:36 -07:00
commit 1dd7caf26e
2 changed files with 28 additions and 1 deletions

View File

@ -18,6 +18,7 @@ var Connection = function(config) {
this.parsedStatements = {};
this.writer = new Writer();
this.ssl = config.ssl || false;
this._ending = false;
};
util.inherits(Connection, EventEmitter);
@ -37,6 +38,11 @@ Connection.prototype.connect = function(port, host) {
});
this.stream.on('error', function(error) {
//don't raise ECONNRESET errors - they can & should be ignored
//during disconnect
if(self._ending && error.code == 'ECONNRESET') {
return;
}
self.emit('error', error);
});
@ -263,6 +269,7 @@ Connection.prototype.end = function() {
//0x58 = 'X'
this.writer.add(emptyBuffer);
this._send(0x58);
this._ending = true;
};
Connection.prototype.describe = function(msg, more) {

View File

@ -1,10 +1,30 @@
var helper = require(__dirname + '/test-helper');
var Connection = require(__dirname + '/../../../lib/connection');
var con = new Connection({stream: new MemoryStream()});
test("connection emits stream errors", function() {
var con = new Connection({stream: new MemoryStream()});
assert.emits(con, 'error', function(err) {
assert.equal(err.message, "OMG!");
});
con.connect();
con.stream.emit('error', new Error("OMG!"));
});
test('connection emits ECONNRESET errors during normal operation', function() {
var con = new Connection({stream: new MemoryStream()});
con.connect();
assert.emits(con, 'error', function(err) {
assert.equal(err.code, 'ECONNRESET');
});
var e = new Error('Connection Reset');
e.code = 'ECONNRESET';
con.stream.emit('error', e);
});
test('connection does not emit ECONNRESET errors during disconnect', function() {
var con = new Connection({stream: new MemoryStream()});
con.connect();
var e = new Error('Connection Reset');
e.code = 'ECONNRESET';
con.end();
con.stream.emit('error', e);
});