Skip to content

Destroy socket when there was an error on it #1975

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,7 @@ Client.prototype.query = function (config, values, callback) {
Client.prototype.end = function (cb) {
this._ending = true

if (this.activeQuery) {
if (this.activeQuery || !this._queryable) {
// if we have an active query we need to force a disconnect
// on the socket - otherwise a hung query could block end forever
this.connection.stream.destroy()
Expand Down
29 changes: 29 additions & 0 deletions packages/pg/test/integration/connection-pool/error-tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'
var helper = require('./test-helper')
const pg = helper.pg
const native = helper.args.native

const suite = new helper.Suite()
suite.test('connecting to invalid port', (cb) => {
Expand Down Expand Up @@ -99,3 +100,31 @@ suite.test('connection-level errors cause future queries to fail', (cb) => {
}))
}))
})

suite.test('handles socket error during pool.query and destroys it immediately', (cb) => {
const pool = new pg.Pool({ max: 1 })

if (native) {
pool.query('SELECT pg_sleep(10)', [], (err) => {
assert.equal(err.message, 'canceling statement due to user request')
cb()
})

setTimeout(() => {
pool._clients[0].native.cancel((err) => {
assert.ifError(err)
})
}, 100)
} else {
pool.query('SELECT pg_sleep(10)', [], (err) => {
assert.equal(err.message, 'network issue')
assert.equal(stream.destroyed, true)
cb()
})

const stream = pool._clients[0].connection.stream
setTimeout(() => {
stream.emit('error', new Error('network issue'))
}, 100)
}
})