Skip to content

[FIXED in 4.6.0] Every graceful close builds a CONNECTION_CLOSED error that nothing reads #167

Closed

Description

@okomarov

Every graceful close builds a CONNECTION_CLOSED error, with a stack trace, that nothing reads.

enterClosed(err = Errors.connection('CONNECTION_CLOSED', options, socket), reported = err) in src/connection.js takes its default when a connection closes normally: from the Closing phase after sql.end() (socketClose without an error, closeTimeout, terminate) and from terminate while idle. The error only matters when something is still waiting on the connection: a COPY stream, a final callback, the current or a sent query, an acquisition, a reserved owner, or queries the pool rejects in closedWhileEnding. On a normal sql.end() none of these exist.

On Cloudflare Workers a client is created and ended per request, so every request pays for it. On connect, select 1, end under the local workerd profiler, Errors.connection is 1.4-2% of samples.

Proposal

Build the error only when one of those receivers exists, and leave the close itself unchanged. Errors that do reach a caller keep their code, message, address and stack.

Measure

Edge cpu job at 150 rounds against npm latest, judged by median and p95 with the rank test. Expected to be below the resolution of cpuTimeMs (whole ms) on its own.

Same pattern, not in this issue: per-client parseOptions and mergeUserTypes (about 3% together on the same profile).

Activity

  1. added
    enhancement
    area: connections
    area: performance
    on Oct 20, 2026
  2. moved this to In progress in Roadmapon Oct 20, 2026
  3. added a commit that references this issue on Oct 20, 2026
    9ff3b83
  4. okomarov commented on Oct 20, 2026

    @okomarov
    Author

    Landed on main in 9ff3b83 (PR #169); ships in the next release.

    A graceful close now builds its CONNECTION_CLOSED error only when a COPY stream, a final callback, the current or a sent query, an acquisition or a reserved owner will receive it. The pool builds its own in closedWhileEnding() when a reconnect closes gracefully with queries still queued. The new closing-no-error startup case counts Errors.connection calls across sql.end() and sql.end({ timeout: 0 }): two before, none now. A first version also replaced a null error thrown by a socket factory; the Deno job caught it, and the check now applies only to undefined.

    Measured CPU per request (connect, select 1, end):

    • Local workerd profiler, 200 rounds (run 39428234607): errors.js falls from 1.3% (fetch_types off) and 1.0% (on) of samples to 0.0%; per-request medians unchanged at 2.53 and 3.46 ms (p = 0.078 and 0.49).
    • Node.js 24, same run: median 1.91 → 1.85 ms with fetch_types off (p = 0.00062), 2.39 → 2.34 ms on (p = 0.019).
    • Edge, A/B against main, 150 rounds, Supabase pooler (run 39437582660): median 6 and 6 ms, costs less p = 0.12. Below what cpuTimeMs resolves, as expected.

    Not covered here, same pattern: per-client parseOptions and mergeUserTypes (about 3% of samples together).

  5. added this to the 4.6.0 milestone on Oct 20, 2026
  6. moved this from In progress to Shipped in Roadmapon Oct 21, 2026
  7. okomarov commented on Oct 21, 2026

    @okomarov
    Author

    Shipped in 4.6.0 (npm install @steadybrew/[email protected]), unchanged from 4.6.0-rc1. The tests that cover it passed in the tag run.

  8. changed the title [-]Every graceful close builds a CONNECTION_CLOSED error that nothing reads[/-] [+][FIXED in 4.6.0] Every graceful close builds a CONNECTION_CLOSED error that nothing reads[/+] on Oct 21, 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: connectionsarea: performanceenhancement

    Type

    No type

    Projects

    • Status
      Shipped

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions