brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.26k stars 1.22k forks source link

unref setTimeout use in pg-pool #2078

Open gajus opened 4 years ago

gajus commented 4 years ago

In at least two cases, pg-pool creates a referenced timeout:

  1. https://github.com/brianc/node-postgres/blob/3f6760c62ee2a901d374b5e50c2f025b7d550315/packages/pg-pool/index.js#L292-L295
  2. https://github.com/brianc/node-postgres/blob/3f6760c62ee2a901d374b5e50c2f025b7d550315/packages/pg-pool/index.js#L171-L177

These timeouts are currently keeping the process from dying naturally.

Use setTimeout().unref() to prevent this.

Unless I am overlooking a need to keep the pool awake?

charmander commented 4 years ago

I think (1) is cleared by pool.end() (which is required for a process to end naturally). (2) is cleared by a successful connection, also required for a process to end naturally.

Have you seen a bug (in a minimal script) where pool timeouts are keeping the process running after ending the pool?

fluggo commented 3 years ago

It occurred to me that this could be useful. The http.Agent class in Node.js unrefs sockets that are not currently handling an HTTP request, so they stick around in memory, but they don't stop the process from exiting if they're not actively being used.

So the question is: can pg-pool unref sockets or timeouts if they're not currently in use? Can I create a pool that will let the process end without calling pool.end() first?

charmander commented 3 years ago

@fluggo Yep, the pool could do that.