Skip to content

Hard to handle idle-in-transaction errors #2852

Open
@mgabeler-lee-6rs

Description

@mgabeler-lee-6rs

I've setup my application's pool with an on('error') hook to log errors that happen outside queries.

However, this hook is not always active between queries. If I acquire a connection from the pool to run a transaction, and that transaction falls afoul of the idle_in_transaction_session_timeout, the error thrown from there doesn't happen during a query, and so if I don't take extra steps to add an on('error') listener for each such connection, my app dies.

But I also have to make sure to manually remove those, since releasing the client back to the pool doesn't do so!

Is there a recommended way to do this "nicely"?

I see some possible ways to make this more ergonomic, which might be applied in combination:

  1. Apply that idle error listener from the pool even when the client is acquired
    • This probably would need to be combined with some logic to avoid calling it if a "per-acquire" error listener is attached
  2. Automatically clear client error listeners on release to the pool
    • e.g. call client.removeAllListeners('error') just before re-adding the idle listener here: https://github.com/brianc/node-postgres/blob/master/packages/pg-pool/index.js#L329
    • This still leaves a race window between when the idle listener is removed acquiring a client connection and when the calling code can add its custom listener.
    • This race may be avoidable when using the callback style, but it's pretty hard to avoid when using Promises.

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