node-postgres icon indicating copy to clipboard operation
node-postgres copied to clipboard

Calling `.end` on a client (or pool) does not wait for connections to close

Open JamesMcNee opened this issue 6 months ago • 3 comments

Hi there, it seems that there is a bug around client.end / pool.end in that when awaited (or using callbacks) the promise can resolve before the underlying connection / stream has disconnected.

Digging through the code a bit, it seems like the issue is in this bit of code: https://github.com/brianc/node-postgres/blob/54eb0fa216aaccd727765641e7d1cf5da2bc483d/packages/pg/lib/connection.js#L189-L199

I think here we should hook into the callback from the sockets end event: https://nodejs.org/api/net.html#socketenddata-encoding-callback and ensure that the original promise does not resolve (or callback fire) before observing this.

JamesMcNee avatar Jul 31 '24 15:07 JamesMcNee