Skip to content

Call callback when end called on unconnected client #2109

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 4 commits into from
Feb 25, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions packages/pg/lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,15 @@ Client.prototype.query = function (config, values, callback) {
Client.prototype.end = function (cb) {
this._ending = true

// if we have never connected, then end is a noop, callback immediately
if (this.connection.stream.readyState === 'closed') {
if (cb) {
cb()
} else {
return Promise.resolve()
}
}

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
Expand Down
13 changes: 13 additions & 0 deletions packages/pg/test/integration/gh-issues/2108-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict"
var helper = require('./../test-helper')
const suite = new helper.Suite()

suite.test('Closing an unconnected client calls callback', (done) => {
const client = new helper.pg.Client()
client.end(done)
})

suite.testAsync('Closing an unconnected client resolves promise', () => {
const client = new helper.pg.Client()
return client.end()
})