From 4cf67b23d48c7c37a0a8a2e5701fd297a14a49cf Mon Sep 17 00:00:00 2001 From: Pasi Eronen Date: Wed, 13 Dec 2017 12:49:06 +0200 Subject: [PATCH] Ignore socket hangup when ending connection also with ssl (#1344). --- lib/connection.js | 8 ++++++- test/unit/connection/error-tests.js | 37 ++++++++++++++++++++++++++--- test/unit/connection/test-helper.js | 2 +- 3 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/connection.js b/lib/connection.js index 0f98cb06..61f2562f 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -97,11 +97,17 @@ Connection.prototype.connect = function (port, host) { NPNProtocols: self.ssl.NPNProtocols }) self.attachListeners(self.stream) - self.emit('sslconnect') self.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) }) + + self.emit('sslconnect') }) } diff --git a/test/unit/connection/error-tests.js b/test/unit/connection/error-tests.js index 19674608..c8b61b43 100644 --- a/test/unit/connection/error-tests.js +++ b/test/unit/connection/error-tests.js @@ -1,31 +1,62 @@ 'use strict' var helper = require(__dirname + '/test-helper') var Connection = require(__dirname + '/../../../lib/connection') -test('connection emits stream errors', function () { +var net = require('net') + +const suite = new helper.Suite() + +suite.test('connection emits stream errors', function (done) { var con = new Connection({stream: new MemoryStream()}) assert.emits(con, 'error', function (err) { assert.equal(err.message, 'OMG!') + done() }) con.connect() con.stream.emit('error', new Error('OMG!')) }) -test('connection emits ECONNRESET errors during normal operation', function () { +suite.test('connection emits ECONNRESET errors during normal operation', function (done) { var con = new Connection({stream: new MemoryStream()}) con.connect() assert.emits(con, 'error', function (err) { assert.equal(err.code, 'ECONNRESET') + done() }) var e = new Error('Connection Reset') e.code = 'ECONNRESET' con.stream.emit('error', e) }) -test('connection does not emit ECONNRESET errors during disconnect', function () { +suite.test('connection does not emit ECONNRESET errors during disconnect', function (done) { 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) + done() +}) + + +suite.test('connection does not emit ECONNRESET errors during disconnect also when using SSL', function (done) { + // our fake postgres server, which just responds with 'S' to start SSL + var socket + var server = net.createServer(function (c) { + socket = c + c.once('data', function (data) { + c.write(new Buffer('S', 'utf8')) + }) + }) + + server.listen(7778, function () { + var con = new Connection({ssl: true}) + con.connect(7778, 'localhost') + assert.emits(con, 'sslconnect', function () { + con.end() + socket.destroy() + server.close() + done() + }) + con.requestSsl() + }) }) diff --git a/test/unit/connection/test-helper.js b/test/unit/connection/test-helper.js index bcb4b25c..53c4b0c9 100644 --- a/test/unit/connection/test-helper.js +++ b/test/unit/connection/test-helper.js @@ -1,2 +1,2 @@ 'use strict' -require(__dirname + '/../test-helper') +module.exports = require(__dirname + '/../test-helper')