Skip to content

client.end() hangs if called twice #2716

Closed
@cody-greene

Description

@cody-greene
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions