Skip to content

Cloudflare Workers: closing a connection reports an unhandled rejection on compatibility dates before 2026-03-03 #1

Closed

Description

@okomarov

On Cloudflare Workers, closing a connection can report an unhandled rejection, Error: Stream was cancelled. on older workerd and TypeError: This socket has been closed. on current workerd, although the driver catches that error a moment later. This is upstream porsager#1202 and the likely cause of porsager#1196.

Who sees it

On current workerd (1.20261012.1), it depends on the Worker's compatibility date.

Compatibility date What happens on close
2026-10-12 or later Nothing. Zero rejections in local workerd and on Cloudflare's network.
Earlier One rejection on most closes, graceful sql.end() included.

Queries, sql.end() and timeouts still work: every scenario passes. In production it did not show in Workers Logs until the Worker registered an unhandledrejection listener.

Workers ships one build, the cf/ polyfill over cloudflare:sockets (#159; workerd's node:net costs more CPU per connection, see #15). So this has to be fixed in the polyfill, or the older dates accepted as unsupported.

Why it happens

cf/polyfills.js keeps a reader.read() pending at all times (read()). end() writes Terminate and then calls raw.close(). Cloudflare's TCP sockets docs say close() closes both sides, so the pending read rejects, by design.

The read is awaited inside try/catch, and the catch does handle it. On older compatibility dates, workerd dispatches unhandledrejection first:

WORKER_UNHANDLED_REJECTION Error: Stream was cancelled.
DIAG_READ_CAUGHT Stream was cancelled.

That is why adding more .catch handlers never changed the count. node-postgres hit the same thing once its socket started calling close() itself (brianc/node-postgres#3805, bisected to brianc/node-postgres#3747).

Evidence

Close-time rejections per job of 15 scenarios:

Where Build 2025-01-01 2025-09-15 2026-10-12
Local workerd 1.20261012.1 (run) cf/ polyfill 25 25 0
Local workerd 1.20261012.1 node:net (rejected) 3 3 0
Cloudflare's network, Neon (run) cf/ polyfill 26 not run 0
Cloudflare's network, Neon node:net (rejected) 1 not run 0

In production, a Worker (upstream 3.4.9, compatibility date 2026-05-14, Supavisor without TLS) logged one This socket has been closed. per Durable Object alarm that closed its client: 16 in 6 minutes, once a global unhandledrejection listener was added. Its cron ticks logged none.

Reproduce

With a PostgreSQL listening on port 55432:

WORKERD_COMPAT_DATE=2025-01-01 npm run test:workerd -- . 55432 end-plain

The runner's log has one WORKER_UNHANDLED_REJECTION line per closed connection, from read in cf/polyfills.js. Every scenario that closes a socket shows it: end-plain, end-timeout, end-while-connecting, connect-timeout and timers.

Tried, no change

Next

  1. Find which compatibility flag, enabled by 2026-10-12, removes the rejection. If it is a single flag, the docs can tell users on older dates to set just that flag rather than move their whole date.
  2. Try the remaining change from upstream's proposed fix Fix unhandled rejections when Cloudflare sockets close porsager/postgres#1251: on a graceful end(), don't call raw.close() after Terminate and let the server close. It reports 60 rejections down to 0 on Hyperdrive. Gate it on the local workerd matrix and the edge scenarios at 2025-01-01, and confirm sql.end() stays within its bound and the CPU job shows no extra CPU.
  3. If neither works, report a minimal reproduction to cloudflare/workerd (a pending reader.read() inside try/catch, then socket.close()), and document 2026-10-12 as the date at which closes are clean.

Done when

The edge scenarios show zero close-time rejections at the oldest tested compatibility date, or the README names the flag or date that avoids them and the older dates are documented as noisy. The README's Cloudflare paragraph already says "on older dates most closes still report one".

Related upstream

Activity

  1. moved this to Later in Roadmapon Oct 15, 2026
  2. added
    area: runtimes
    bug
    upstream
    on Oct 16, 2026
  3. moved this from Later to In progress in Roadmapon Oct 18, 2026
  4. changed the title [-]Cloudflare workerd: "Stream was cancelled" reported as unhandled rejection on every socket close[/-] [+]Cloudflare Workers: closing a connection reports an unhandled rejection on compatibility dates before 2026-10-12[/+] on Oct 19, 2026
  5. added 2 commits that reference this issue on Oct 19, 2026
    064e57e
    8e1827b
  6. okomarov commented on Oct 19, 2026

    @okomarov
    Author

    Fixed in 8e1827b (#160) for graceful closes; forced closes on older dates are documented.

    Cause. workerd before compatibility date 2026-03-03 reports a rejection as unhandled before the code awaiting it has handled it (cloudflare/workerd#6020, fixed behind unhandled_rejection_after_microtask_checkpoint). In the local workerd matrix, turning only that flag on at 2025-01-01 took close-time rejections from 27 to 0, and turning it off at 2026-10-12 brought 26 back.

    Fix. On sql.end() the polyfill now writes Terminate and lets PostgreSQL close the socket instead of closing it straight away, so no read is pending when the socket closes. Without the flag, at 2025-01-01 and 2025-09-15:

    Scenarios Before After
    Graceful: end-plain, timers, copy-out, subscribe, ssl-prefer-declined, end-tls 15 0
    Forced: connect-timeout, end-timeout, end-while-connecting 10 10

    The workerd runner now fails any graceful scenario that logs one, and the matrix runs 2025-01-01 with the flag as well.

    Not fixed. A close the driver forces (connect timeout, an expired sql.end({ timeout }), ending while connecting) still logs one on dates before 2026-03-03. Cancelling the pending read first did not change that. The README and Cloudflare Workers support say to move to 2026-03-03 or add the flag.

    A production Worker logged one per Durable Object alarm at 2026-05-14, where the flag is already on, on upstream 3.4.9. The likely cause is upstream's polyfill emitting error after close, which this fork guards; that is inferred from the code, not confirmed in production.

  7. changed the title [-]Cloudflare Workers: closing a connection reports an unhandled rejection on compatibility dates before 2026-10-12[/-] [+]Cloudflare Workers: closing a connection reports an unhandled rejection on compatibility dates before 2026-03-03[/+] on Oct 19, 2026
  8. okomarov commented on Oct 19, 2026

    @okomarov
    Author

    Closed by decision, not by 8e1827b's fix.

    8e1827b made end() wait for PostgreSQL to close its side, so no read was rejected. On Cloudflare's network that added a full round trip to every sql.end(): wall time rose by 122 to 135 ms per request against the Supabase pooler and Neon, with CPU unchanged.

    PR #161 goes back to closing once Terminate is written. Wall time matches the latest release again. The cost is the log line this issue is about: on compatibility dates before 2026-03-03 without unhandled_rejection_after_microtask_checkpoint, each close logs one rejection that the driver has already handled. Queries and errors are unaffected, and the fix on the Worker's side is the compatibility date or the flag, as docs/api.md now says.

    The same PR checks a request that never calls sql.end(): workerd closes the socket when the request finishes, the server's backend is gone by the next request (locally and on the edge against Neon), and on old dates the teardown logs the same single rejection.

  9. modified the milestone: 4.6.0 on Oct 20, 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: runtimesbugupstream

    Type

    No type

    Projects

    • Status
      In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions