Skip to content

Commit

Permalink
Call callback when end called on unconnected client (#2109)
Browse files Browse the repository at this point in the history
* Call callback when end called on unconnected client

Closes #2108

* Revert a bit of the change

* Use readyState because pending doesn't exist in node 8.x

* Update packages/pg/lib/client.js

use bring your own promise

Co-Authored-By: Charmander <~@charmander.me>

Co-authored-by: Charmander <~@charmander.me>
  • Loading branch information
brianc and charmander committed Feb 25, 2020
1 parent 2987753 commit 1c8b6b9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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 this._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()
})

0 comments on commit 1c8b6b9

Please sign in to comment.