Skip to content

Commit 7de8b49

Browse files
author
Johan Levin
committed
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.
1 parent 2ef5550 commit 7de8b49

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

packages/pg-pool/index.js

+15-10
Original file line numberDiff line numberDiff line change
@@ -252,16 +252,7 @@ class Pool extends EventEmitter {
252252

253253
this.emit('acquire', client)
254254

255-
let released = false
256-
257-
client.release = (err) => {
258-
if (released) {
259-
throwOnDoubleRelease()
260-
}
261-
262-
released = true
263-
this._release(client, idleListener, err)
264-
}
255+
client.release = this._releaseOnce(client, idleListener)
265256

266257
client.removeListener('error', idleListener)
267258

@@ -287,6 +278,20 @@ class Pool extends EventEmitter {
287278
}
288279
}
289280

281+
// returns a function that wraps _release and throws if called more than once
282+
_releaseOnce(client, idleListener) {
283+
let released = false
284+
285+
return (err) => {
286+
if (released) {
287+
throwOnDoubleRelease()
288+
}
289+
290+
released = true
291+
this._release(client, idleListener, err)
292+
}
293+
}
294+
290295
// release a client back to the poll, include an error
291296
// to remove it from the pool
292297
_release(client, idleListener, err) {

0 commit comments

Comments
 (0)