Repository navigation
Description
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-plainThe 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
.catchonreader.closed,writer.closed,raw.openedand the promise fromraw.close().- Handling rejection of the
writer.readychain; guarding the'error'emit. reader.cancel()beforeclose()on the destroy path.- Graceful half-close (
writer.close()after Terminate, letting PostgreSQL close its side): no rejection, but the read loop never saw end-of-stream, sosql.end()ran past its 3 s bound (possibly 🐛 Bug Report — TCP socket closes with its closed promise in pending state cloudflare/workerd#5653; see also 🐛 BUG: TCP Stream read() function gets stuck cloudflare/workerd#2366). Tried on Wrangler 4.123.0 only.
Next
- 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.
- 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 callraw.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 confirmsql.end()stays within its bound and the CPU job shows no extra CPU. - If neither works, report a minimal reproduction to cloudflare/workerd (a pending
reader.read()insidetry/catch, thensocket.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
- Cloudflare/workerd: every sql.end() leaves an unhandled rejection (Stream was cancelled) porsager/postgres#1202: every
sql.end()leaves "Stream was cancelled". - Cloudflare polyfill: unhandled rejection when a connection closes (read() emits 'error' after listeners are removed) porsager/postgres#1196: unhandled rejection in the Cloudflare polyfill on close.
- Fix unhandled rejections when Cloudflare sockets close porsager/postgres#1251: proposed fix; two of its three changes are already here.
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsIn progress
Activity
test(workerd): report each scenario's close-time rejections (#1)
test(workerd): report each scenario's close-time rejections (#1)
test(workerd): run both builds through Wrangler's createTestHarness a…
feat(workerd): ship one Workers build, over cloudflare:sockets
feat(workerd): ship one Workers build over cloudflare:sockets, and ga…
[-]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[/+]fix(workerd): end connections without an unhandled rejection on older…
fix(workerd): end connections without an unhandled rejection on older…
okomarov commentedon Oct 19, 2026
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:end-plain,timers,copy-out,subscribe,ssl-prefer-declined,end-tlsconnect-timeout,end-timeout,end-while-connectingThe 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
errorafterclose, which this fork guards; that is inferred from the code, not confirmed in production.[-]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[/+]fix(workerd): close the socket once Terminate is written, so sql.end(…
okomarov commentedon Oct 19, 2026
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 everysql.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, asdocs/api.mdnow 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.revert(workerd): close the socket once Terminate is written, so sql.e…