Skip to content

[FIXED in 4.4.0] A throwing user callback breaks connection state #13

Closed

Description

@okomarov

Every user callback runs synchronously inside driver internals, so a bug in user code turns into an unrelated failure. Each callback below threw new Error('callback sentinel'), against PostgreSQL 17.11 on Node 24.21.0 in the disposable test container, at 030dc57:

Callback First query Next query on the same pool Error surfaced?
onnotice (during raise notice) rejects with the callback's error succeeds only as the query's rejection
onparameter (during set application_name) rejects with the callback's error rejects too: ParameterStatus also arrives at startup, so every new session fails only as query rejections
debug rejects with the callback's error hangs: the connection is wedged only as the query's rejection
onclose (backend terminated, query queued behind a reservation) queued query is still served succeeds yes, as an uncaught exception from the socket close event

The cause is the same throughout. onnotice, onparameter and onnotify run inside handle(), where a throw becomes a protocolError, and errored() rejects whichever query is in flight with the user's error. debug runs while a query is built, so a throw lands in submit()'s catch, the porsager#1082 path. onnotify was not probed.

No upstream issue reports this. Related: porsager#461 (instrumentation hooks) and porsager#1100 (an idle hook for Vercel).

Proposal

Apply one rule to every callback: call it synchronously, catch, and rethrow in a microtask (queueMicrotask(() => { throw e })), as Node's diagnostics_channel does with subscriber errors.

  • Driver state is never touched by a user error: no query is rejected with someone else's error, and no session fails or wedges.
  • The bug surfaces as an uncaught exception instead of masquerading as a query error.
  • try/catch costs nothing on the non-throwing path.
  • The call stays synchronous, so async context (for example Vercel's request context) is kept.

The onidle option added for porsager#1100 introduces this guard as a single helper. On that path an unguarded throw is swallowed silently, because no query is in flight. This issue applies the same helper to the existing callbacks, with a regression test per callback. No API change.

Activity

  1. added a commit that references this issue on Oct 11, 2026
  2. moved this to Shipped in Roadmapon Oct 15, 2026
  3. added
    area: connections
    bug
    on Oct 16, 2026
  4. changed the title [-]A throwing user callback breaks connection state[/-] [+][FIXED in 4.4.0] A throwing user callback breaks connection state[/+] on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: connectionsbug

    Type

    No type

    Projects

    • Status
      Shipped

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions