Refactor pg-pool to avoid potential memory leak

Reduce the closure scope captured by the "release once" lambda function
in _acquireClient so that it does not retain the pendingItem promise.
This commit is contained in:
Johan Levin 2020-04-15 11:46:15 +02:00
parent 2ef5550373
commit 7de8b49ad7

View File

@ -252,16 +252,7 @@ class Pool extends EventEmitter {
this.emit('acquire', client)
let released = false
client.release = (err) => {
if (released) {
throwOnDoubleRelease()
}
released = true
this._release(client, idleListener, err)
}
client.release = this._releaseOnce(client, idleListener)
client.removeListener('error', idleListener)
@ -287,6 +278,20 @@ class Pool extends EventEmitter {
}
}
// returns a function that wraps _release and throws if called more than once
_releaseOnce(client, idleListener) {
let released = false
return (err) => {
if (released) {
throwOnDoubleRelease()
}
released = true
this._release(client, idleListener, err)
}
}
// release a client back to the poll, include an error
// to remove it from the pool
_release(client, idleListener, err) {