porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PostgresJS Prevents Natural Process Exit

granthusbands opened this issue · comments

On Node, if PostgresJS has been used at all, the process won't exit naturally, as it has some number of pooled connections still open. It seems that people are expected to call sql.end() or set a low connection timeout. However, I think there's a simpler solution for end users. If PostgresJS can call soc.unref() on idle connections entering the pool and call soc.ref() when they're taken back out of the pool, the idle connections will no longer keep the process alive.

To be clear, unref() tells node that the socket is not important enough to keep the process alive, and ref() tells it that the socket is again important enough. The documentation implies that it's a toggle, rather than a counter, so it should be low risk. Also note that it's probable that some timers (for idle timeout and such) will also need unref called on the object returned from setTimeout or setInterval. They can probably just stay unref.