Skip to content

Commit 4cf67b2

Browse files
pasieronenbrianc
authored andcommitted
Ignore socket hangup when ending connection also with ssl (brianc#1344).
1 parent c9ca1da commit 4cf67b2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

lib/connection.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,17 @@ Connection.prototype.connect = function (port, host) {
9797
NPNProtocols: self.ssl.NPNProtocols
9898
})
9999
self.attachListeners(self.stream)
100-
self.emit('sslconnect')
101100

102101
self.stream.on('error', function (error) {
102+
// don't raise ECONNRESET errors - they can & should be ignored
103+
// during disconnect
104+
if (self._ending && error.code === 'ECONNRESET') {
105+
return
106+
}
103107
self.emit('error', error)
104108
})
109+
110+
self.emit('sslconnect')
105111
})
106112
}
107113

test/unit/connection/error-tests.js

+34-3
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,62 @@
11
'use strict'
22
var helper = require(__dirname + '/test-helper')
33
var Connection = require(__dirname + '/../../../lib/connection')
4-
test('connection emits stream errors', function () {
4+
var net = require('net')
5+
6+
const suite = new helper.Suite()
7+
8+
suite.test('connection emits stream errors', function (done) {
59
var con = new Connection({stream: new MemoryStream()})
610
assert.emits(con, 'error', function (err) {
711
assert.equal(err.message, 'OMG!')
12+
done()
813
})
914
con.connect()
1015
con.stream.emit('error', new Error('OMG!'))
1116
})
1217

13-
test('connection emits ECONNRESET errors during normal operation', function () {
18+
suite.test('connection emits ECONNRESET errors during normal operation', function (done) {
1419
var con = new Connection({stream: new MemoryStream()})
1520
con.connect()
1621
assert.emits(con, 'error', function (err) {
1722
assert.equal(err.code, 'ECONNRESET')
23+
done()
1824
})
1925
var e = new Error('Connection Reset')
2026
e.code = 'ECONNRESET'
2127
con.stream.emit('error', e)
2228
})
2329

24-
test('connection does not emit ECONNRESET errors during disconnect', function () {
30+
suite.test('connection does not emit ECONNRESET errors during disconnect', function (done) {
2531
var con = new Connection({stream: new MemoryStream()})
2632
con.connect()
2733
var e = new Error('Connection Reset')
2834
e.code = 'ECONNRESET'
2935
con.end()
3036
con.stream.emit('error', e)
37+
done()
38+
})
39+
40+
41+
suite.test('connection does not emit ECONNRESET errors during disconnect also when using SSL', function (done) {
42+
// our fake postgres server, which just responds with 'S' to start SSL
43+
var socket
44+
var server = net.createServer(function (c) {
45+
socket = c
46+
c.once('data', function (data) {
47+
c.write(new Buffer('S', 'utf8'))
48+
})
49+
})
50+
51+
server.listen(7778, function () {
52+
var con = new Connection({ssl: true})
53+
con.connect(7778, 'localhost')
54+
assert.emits(con, 'sslconnect', function () {
55+
con.end()
56+
socket.destroy()
57+
server.close()
58+
done()
59+
})
60+
con.requestSsl()
61+
})
3162
})

test/unit/connection/test-helper.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
'use strict'
2-
require(__dirname + '/../test-helper')
2+
module.exports = require(__dirname + '/../test-helper')

0 commit comments

Comments
 (0)