Skip to content

client.end() hangs if called twice #2716

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

Closed
cody-greene opened this issue Mar 6, 2022 · 0 comments · Fixed by #2717
Closed

client.end() hangs if called twice #2716

cody-greene opened this issue Mar 6, 2022 · 0 comments · Fixed by #2717

Comments

@cody-greene
Copy link
Contributor

cody-greene commented Mar 6, 2022

const client = new pg.Client()
await client.connect()
await client.end() // ok
await client.end() // uh-oh! This never resolves!

Ideally we should just not try to end the client twice in a row. But in using other packages, that wrap node-postgres, I've encountered a situation where I need to access the pg Client instance and end() it, before returning it to some wrapper class which may also try to end() it at some point. Arguably this wrapper class should be smarter and avoid calling end() if it sees the "end" event. But just the same client.end() shouldn't hang either.

After a quick look at the source:

return new this._Promise((resolve) => {
  this.connection.once('end', resolve)
})

When the client has already ended, we just wait for a "end" event which never arrives. I imagine the fix here is to check for a private field like this._ended (as a compliment to this._ending) and return/resolve early.

brianc pushed a commit that referenced this issue Mar 6, 2023
* fix: double client.end() hang

fixes #2716

`client.end()` will resolve early if the connection is already dead,
rather than waiting for an "end" event that will never arrive.

* fix: client.end() resolves when socket is fully closed
thijs pushed a commit to thijs/node-postgres that referenced this issue Aug 1, 2023
* fix: double client.end() hang

fixes brianc#2716

`client.end()` will resolve early if the connection is already dead,
rather than waiting for an "end" event that will never arrive.

* fix: client.end() resolves when socket is fully closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant