From 7c7a3b884d73a7f1385af2d11113a87aa8eb99d6 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Thu, 20 Feb 2020 17:37:26 -0600 Subject: [PATCH 1/4] Call callback when end called on unconnected client Closes #2108 --- packages/pg/lib/client.js | 13 +++++++++++-- .../pg/test/integration/gh-issues/2108-tests.js | 13 +++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 packages/pg/test/integration/gh-issues/2108-tests.js diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 05efbdc5a..596d3decf 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -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.pending && !this.connection.stream.connecting) { + 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 @@ -554,10 +563,10 @@ Client.prototype.end = function (cb) { } if (cb) { - this.connection.once('end', cb) + this.connection.stream.once('close', cb) } else { return new this._Promise((resolve) => { - this.connection.once('end', resolve) + this.connection.stream.once('close', resolve) }) } } diff --git a/packages/pg/test/integration/gh-issues/2108-tests.js b/packages/pg/test/integration/gh-issues/2108-tests.js new file mode 100644 index 000000000..9832dae37 --- /dev/null +++ b/packages/pg/test/integration/gh-issues/2108-tests.js @@ -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() +}) From 2c27edf9d8307f718292913727fca9e59ad8862f Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Thu, 20 Feb 2020 17:39:56 -0600 Subject: [PATCH 2/4] Revert a bit of the change --- packages/pg/lib/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 596d3decf..375bdcbd8 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -563,10 +563,10 @@ Client.prototype.end = function (cb) { } if (cb) { - this.connection.stream.once('close', cb) + this.connection.once('end', cb) } else { return new this._Promise((resolve) => { - this.connection.stream.once('close', resolve) + this.connection.once('end', resolve) }) } } From 81bf8b3f59c75875988f13ed83c9401cbea31b9b Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Thu, 20 Feb 2020 18:01:47 -0600 Subject: [PATCH 3/4] Use readyState because pending doesn't exist in node 8.x --- packages/pg/lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 375bdcbd8..3f8ca34cb 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -546,7 +546,7 @@ Client.prototype.end = function (cb) { this._ending = true // if we have never connected, then end is a noop, callback immediately - if (this.connection.stream.pending && !this.connection.stream.connecting) { + if (this.connection.stream.readyState === 'closed') { if (cb) { cb() } else { From 7ff80543c01f2528c91b5c243b02c9e1bdf1342b Mon Sep 17 00:00:00 2001 From: Brian C Date: Fri, 21 Feb 2020 13:05:23 -0600 Subject: [PATCH 4/4] Update packages/pg/lib/client.js use bring your own promise Co-Authored-By: Charmander <~@charmander.me> --- packages/pg/lib/client.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/pg/lib/client.js b/packages/pg/lib/client.js index 3f8ca34cb..cdae3b7c2 100644 --- a/packages/pg/lib/client.js +++ b/packages/pg/lib/client.js @@ -550,7 +550,7 @@ Client.prototype.end = function (cb) { if (cb) { cb() } else { - return Promise.resolve() + return this._Promise.resolve() } }