Ignore socket hangup when ending connection also with ssl (#1344).

This commit is contained in:
Pasi Eronen 2017-12-13 12:49:06 +02:00 committed by Brian C
parent c9ca1dad69
commit 4cf67b23d4
3 changed files with 42 additions and 5 deletions

View File

@ -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')
})
}

View File

@ -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()
})
})

View File

@ -1,2 +1,2 @@
'use strict'
require(__dirname + '/../test-helper')
module.exports = require(__dirname + '/../test-helper')