Skip to content

Commit

Permalink
Do not return broken clients to the pool (#2083)
Browse files Browse the repository at this point in the history
* Prevent requeuing a broken client

If a client is not queryable, the pool should prevent requeuing instead
of strictly enforcing errors to be propagated back to it.

* Write tests for change

* Use node 13.6 in travis

Some weird behavior changed w/ async iteration in node 13.7...I'm not sure if this was an unintentional break or not but it definitely diverges in behavior from node 12 and earlier versions in node 13...so for now going to run tests on 13.6 to unblock the tests from running while I track this down.

* Update packages/pg-pool/test/releasing-clients.js

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

* Update .travis.yml

Co-authored-by: Johannes Würbach <johannes.wuerbach@googlemail.com>
Co-authored-by: Charmander <~@charmander.me>
  • Loading branch information
3 people committed Jan 28, 2020
1 parent 3f6760c commit 727f1a0
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .travis.yml
Expand Up @@ -10,7 +10,10 @@ env:
node_js:
- lts/dubnium
- lts/erbium
- 13
# node 13.7 seems to have changed behavior of async iterators exiting early on streams
# if 13.8 still has this problem when it comes down I'll talk to the node team about the change
# in the mean time...peg to 13.6
- 13.6

addons:
postgresql: "10"
Expand Down
9 changes: 7 additions & 2 deletions packages/pg-pool/index.js
Expand Up @@ -25,6 +25,10 @@ class PendingItem {
}
}

function throwOnDoubleRelease () {
throw new Error('Release called on client which has already been released to the pool.')
}

function promisify (Promise, callback) {
if (callback) {
return { callback: callback, result: undefined }
Expand Down Expand Up @@ -244,7 +248,7 @@ class Pool extends EventEmitter {

client.release = (err) => {
if (released) {
throw new Error('Release called on client which has already been released to the pool.')
throwOnDoubleRelease()
}

released = true
Expand Down Expand Up @@ -280,7 +284,8 @@ class Pool extends EventEmitter {
_release (client, idleListener, err) {
client.on('error', idleListener)

if (err || this.ending) {
// TODO(bmc): expose a proper, public interface _queryable and _ending
if (err || this.ending || !client._queryable || client._ending) {
this._remove(client)
this._pulseQueue()
return
Expand Down
54 changes: 54 additions & 0 deletions packages/pg-pool/test/releasing-clients.js
@@ -0,0 +1,54 @@
const Pool = require('../')

const expect = require('expect.js')
const net = require('net')

describe('releasing clients', () => {
it('removes a client which cannot be queried', async () => {
// make a pool w/ only 1 client
const pool = new Pool({ max: 1 })
expect(pool.totalCount).to.eql(0)
const client = await pool.connect()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)
// reach into the client and sever its connection
client.connection.end()

// wait for the client to error out
const err = await new Promise((resolve) => client.once('error', resolve))
expect(err).to.be.ok()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)

// try to return it to the pool - this removes it because its broken
client.release()
expect(pool.totalCount).to.eql(0)
expect(pool.idleCount).to.eql(0)

// make sure pool still works
const { rows } = await pool.query('SELECT NOW()')
expect(rows).to.have.length(1)
await pool.end()
})

it('removes a client which is ending', async () => {
// make a pool w/ only 1 client
const pool = new Pool({ max: 1 })
expect(pool.totalCount).to.eql(0)
const client = await pool.connect()
expect(pool.totalCount).to.eql(1)
expect(pool.idleCount).to.eql(0)
// end the client gracefully (but you shouldn't do this with pooled clients)
client.end()

// try to return it to the pool
client.release()
expect(pool.totalCount).to.eql(0)
expect(pool.idleCount).to.eql(0)

// make sure pool still works
const { rows } = await pool.query('SELECT NOW()')
expect(rows).to.have.length(1)
await pool.end()
})
})

0 comments on commit 727f1a0

Please sign in to comment.