Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unref setTimeout use in pg-pool #2078

Open
gajus opened this issue Jan 18, 2020 · 3 comments
Open

unref setTimeout use in pg-pool #2078

gajus opened this issue Jan 18, 2020 · 3 comments

Comments

@gajus
Copy link
Contributor

gajus commented Jan 18, 2020

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

  1. tid = setTimeout(() => {
    this.log('remove idle client')
    this._remove(client)
    }, this.options.idleTimeoutMillis)
  2. const tid = setTimeout(() => {
    // remove the callback from pending waiters because
    // we're going to call it with a timeout error
    removeWhere(this._pendingQueue, (i) => i.callback === queueCallback)
    pendingItem.timedOut = true
    response.callback(new Error('timeout exceeded when trying to connect'))
    }, this.options.connectionTimeoutMillis)

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
Copy link
Collaborator

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
Copy link
Contributor

fluggo commented Jul 1, 2021

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
Copy link
Collaborator

@fluggo Yep, the pool could do that.

brianc pushed a commit that referenced this issue Jul 27, 2021
Based on the suggestion from #2078. This adds ref/unref methods to the
Connection and Client classes and then uses them to allow the process to
exit if all of the connections in the pool are idle. This behavior is
controlled by the allowExitOnIdle flag to the Pool constructor; it defaults
to the old behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants