Skip to content

Commit dc2408d

Browse files
committed
Destroy socket when there was an error on it
When error happens on socket, potentially dead socket is kept open indefinitely by calling "connection.end()". Similar issue is that it keeps socket open until long-running query is finished even though the connection was ended.
1 parent 60d8df6 commit dc2408d

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/client.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ Client.prototype.query = function (config, values, callback) {
538538
Client.prototype.end = function (cb) {
539539
this._ending = true
540540

541-
if (this.activeQuery) {
541+
if (this.activeQuery || !this._queryable) {
542542
// if we have an active query we need to force a disconnect
543543
// on the socket - otherwise a hung query could block end forever
544544
this.connection.stream.destroy()

test/integration/connection-pool/error-tests.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,18 @@ suite.test('connection-level errors cause future queries to fail', (cb) => {
9898
}))
9999
}))
100100
}))
101+
// })
102+
103+
suite.test('handles socket error during pool.query and destroys it immediately', (cb) => {
104+
const pool = new pg.Pool({ max: 1 })
105+
pool.query('SELECT pg_sleep(10)', [], (err) => {
106+
assert.equal(err.message, 'network issue')
107+
assert.equal(stream.destroyed, true)
108+
cb()
109+
})
110+
111+
const stream = pool._clients[0].connection.stream
112+
setTimeout(() => {
113+
stream.emit('error', new Error('network issue'))
114+
}, 100)
101115
})

0 commit comments

Comments
 (0)