Skip to content

Workers: sql.end() closes once Terminate is written; test a request that never ends its client - #161

Merged
okomarov merged 1 commit into
mainfrom
fix/workers-end-at-once
Oct 20, 2026
Merged

okomarov merged 1 commit into
mainfrom
fix/workers-end-at-once

Conversation

@okomarov

Copy link
Copy Markdown

Draft for CI. Reverts the end() wait from 8e1827b (one extra round trip per sql.end()), runs the workerd matrix with close rejections expected on dates before 2026-03-03 without the flag, and adds a no-end scenario that leaves a client open when the request finishes and checks how long its backend stays in pg_stat_activity, locally and on the edge against Neon.

…nd() no longer waits a round trip

Partially reverts 8e1827b: end() closes the socket as soon as Terminate is
written again, as in 4.5.1. The rest of that commit stays: the handled close
promise, the read loop closing on end of stream and the flagged matrix entry.

8e1827b made end() write Terminate and wait for PostgreSQL to close its
side, so the read loop would see end of stream instead of a rejected read.
That removed the unhandled-rejection log on compatibility dates before
2026-03-03, but every sql.end() then waited a full round trip for the
server's close. On Cloudflare's network wall time per request rose by
122 to 135 ms against the Supabase pooler and Neon, with CPU unchanged.

end() goes back to writing Terminate and closing once the write
completes. Wall time is back to the latest release's (median 790 vs
791 ms on the Supabase pooler, 1008 vs 1008 ms on Neon over TLS). On
dates before 2026-03-03 without unhandled_rejection_after_microtask_checkpoint
each close logs one rejection that the read loop handles; queries are
unaffected. The read loop still closes the socket when the server ends
the stream, and the close promise stays handled.

The workerd matrix marks the entries that log close rejections with
WORKERD_CLOSE_REJECTIONS=logged and fails any close rejection elsewhere.

A new no-end scenario returns from the request without ending the
client and polls pg_stat_activity until its backend is gone. Locally and
on the edge against Neon, the backend was gone by the first check (about
0.3 s and 1 s), and on old dates the teardown logs one close rejection.
The Workers docs now show await sql.end(), say that ctx.waitUntil costs
the same, and that leaving the client open also works.

Addresses #1
@okomarov
okomarov force-pushed the fix/workers-end-at-once branch from f5c1415 to ebc9ab2 Compare October 20, 2026 10:53
@okomarov
okomarov merged commit ebc9ab2 into main Oct 20, 2026
@okomarov
okomarov deleted the fix/workers-end-at-once branch October 20, 2026 10:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant