Skip to content

Do not return broken clients to the pool #2083

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 5 commits into from
Jan 28, 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
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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()
})
})