Skip to content

uws: uncork in uws_res_close_if_done_and_marked, and call it after three ends outside a uWS handler - #48486

Draft
robobun wants to merge 5 commits into
mainfrom
robobun/7d2c027c/bodyless-end-gates-itself
Draft

robobun wants to merge 5 commits into
mainfrom
robobun/7d2c027c/bodyless-end-gates-itself

Conversation

@robobun

@robobun robobun commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

Problem

  • A response without a body (304, HEAD) can leave open a connection marked to close (Connection: close, HTTP/1.0, server.stop()). No FIN comes until the idle timeout, none with idleTimeout: 0.
  • uws_res_close_if_done_and_marked (src/uws_sys/libuwsockets.cpp) left a corked response to HttpResponse::cork(). That returns early when another socket's cork() took the slot (HttpResponse.h:968).
  • Three ends outside a uWS handler never called it.

Fix

  • The gate now uncorks a completed response, as internalEnd() does. RequestContext::end_without_body already calls it before its microtask drain.
  • An HTML import's 500 Build Failed and the dev server's 405 and 500 now call the gate.
  • A JS-produced response without a body now leaves when it completes (Bun.serve: send a response that JavaScript produced when it completes #42815 did bodies).
  • Verified: 12 tests in bun-server.test.ts, bun-serve-html.test.ts and serve.test.ts fail on main d0f4fdd. Self-reviewed: 29 concerns raised, 26 addressed.

Background

  • uWS batches writes in two cork slots per loop. A third cork() flushes the oldest.
  • The close gate closes a connection whose response is complete, marked to close and fully sent.
  • Considered: a close inside uws_res_end_without_body freed the DevServer under DevServer::defer_request. A gate at cork()'s early return (uws: run the close gate in HttpResponse::cork() when the cork slot was lost #37446) can run on an upgraded WebSocket.

Downsides

  • A pipelined JS-produced response without a body takes its own send(): 5 TCP segments for 8 HEAD responses, was 3.
  • Each such response runs about 70 instructions in the gate, was 29. The uWS unit's .text stays 179,756 bytes.
  • On a stopped dev server the 405 can drop the DevServer under its own frame (ASAN clean), as main's error page does.
Notes

Origin. From the review of #37074: "HttpResponse::cork() early return skips the close gate ... a completed Connection: close / HTTP/1.0 response on a non-parsing socket is never shut". No user has reported it. Every stranded response is self-framed (304, HEAD, Content-Length: 0), so a client that honours framing is not stuck. What stays open is the connection: the server holds it, and await server.stop() waits for it.

Repro. Four Connection: close POSTs whose bodies never arrive, answered 304 by an async handler that all four leave in one microtask drain:

async fetch(req) {
  await release.promise;
  return new Response(null, { status: 304 });
}

On the 1.4.3 canary with idleTimeout: 0, two of the four connections are still open after 30 s. With idleTimeout: 4 the idle sweep closes them. Node v26.3.0 sends FIN right after the 304 for the same request, alone.

How a response loses its cork slot. RequestContext::end_without_body ends the response inside resp.corked(...) and then rejects the unread request body. The rejection drains microtasks, so the next handler resumes and completes its response inside that drain, in a cork slot of its own. With both slots in use, a third cork() flushes the least recently used slot. When the first cork() gets control back, its socket holds no slot, and it returns before its gate (HttpResponse.h:968).

The ends outside a uWS handler. The first request to an HTML import waits for the bundle and is answered from an event-loop task. The response is not corked, so the old gate would have closed it, but these three ends did not call the gate. The socket kept the uWS default timeout of 10 s, whatever idleTimeout says:

  • 500 Build Failed of a production build that failed (HTMLBundle.rs).
  • 405 of the dev server (StaticRoute::on_with_method).
  • 500 of the dev server after its [serve.static] plugins failed to load (DeferredRequest::abort).

What each caller of the gate sees now.

  • RequestContext::end_without_body, inside cork() after an await: the gate sends the response and closes a marked connection before the drain.
  • RequestContext::end_without_body, in the read that delivered the request: the gate sends the response and leaves the close to onData, as internalEnd() does for a JS-produced response.
  • FileResponseStream::end_sendfile: no change. uws_res_prepare_for_sendfile uncorks before the first sendfile().
  • A static route's HEAD, 304 or 405 in the read that delivered it: no change. The response stays corked until onData is done (staticHead and static304 still take 1 segment for 8 responses).

Designs.

Design Hang tests Stopped dev server, 413 on the last connection
main fail clean
Close inside uws_res_end_without_body (first version of this PR) pass panic: Address is poisoned: deinit_if_we_can drops the DevServer under DevServer::defer_request
The gate uncorks, callers gate (this PR) pass clean

#46528. That branch moves the bodyless end into HttpResponse::endWithoutBody and keeps it non-closing. At its head 9e655b6 the gate shim still tests !isCorked() (libuwsockets.cpp:943, :951), and the dev server's 405 and 500 have no gate call. It gates the 500 Build Failed in end_build_failed. The same change applies there: the two conditions in the gate shim and two gate calls. A maintainer reproduced the hang on main and on that branch and picked the order: this PR is cut again on top of #46528 after that PR merges. It stays a draft until then.

Tests. All fail on main d0f4fdd and pass with this change, on debug ASAN builds of both.

  • bun-server.test.ts, "a response without a body closes a connection that is marked to close": 5 rows of four concurrent requests (Connection: close on the request, HTTP/1.0, Connection: close on the response, server.stop(), TLS) and 2 cases where two WebSockets or two streaming responses take the slot. The servers use idleTimeout: 0, so only the server's own close can end the connection. Each test also asserts server.pendingRequests at the time the body is rejected, to show that the responses complete inside one another.
  • bun-serve-html.test.ts, "the request that waited for an html route's bundle gets its connection closed": the three ends above. The fixture checks for the FIN after one more round trip, so that the 10 s default timeout cannot close the connection for the server. On main each row fails in about 2 s with ended: false.
  • serve.test.ts, "share one send() until a handler runs JavaScript": new rows for HEAD and 304. mixedHead is 3 segments on main and 5 here. The four others are the same on both.
  • serve.test.ts, "sends a response without a body before the JavaScript that runs after it": a 304 reaches the client before the handler's trailing work. On main the trailing work times out first.

Measurements.

  • uWS unit (libuwsockets.cpp) compiled with the release flags, without LTO: .text 179,756 bytes on both sides. 550 of 551 functions are instruction-identical. uws_res_close_if_done_and_marked is 135 bytes on both sides, 47 and 52 instructions.
  • Instructions along the path of a JS-produced response without a body that completes in its read, counted in that disassembly, calls to usockets accessors not counted: 29 on main (the shim returns at isCorked()), 70 here (30 in the shim and 40 in uncorkCompletedResponse()).
  • TCP segments come from TCP_INFO in the serve.test.ts test.
  • Not measured: the size of the three new call sites in bun_runtime, and dynamic instruction counts. perf, valgrind, strace and bloaty are not in this container, and a release build of bun_runtime does not fit in its memory.

Builds. bun bd does not compile main d0f4fdd: BunPlugin.cpp:3546: no member named 'SuspendExceptionScope' in namespace 'JSC'. Both debug builds here had the two #include lines of #48722 applied locally. They are not committed.

Also run on the debug build of this branch. bun-serve-static, bun-serve-file, bun-serve-routes, serve-directory-routes, request-smuggling, serve-close-delimited-framing, bun-serve-headers, tls-keepalive, websocket-server, test/bake/deinitialization, test/bake/serve-plugins-dev-server, test/bake/dev/html, test/bake/dev/plugins, node-http-req-complete, node-http-server-close-drain: no failure. In serve.test.ts two tests fail that this change does not reach: "should use correct error when using a root range port(#7187)" (the container runs as root, it fails on the canary too) and "should be able to abort a sendfile response and streams" (500 TLS requests in a 10 s budget: 9.1 s on the main build, 10.1 s here, 7.7 to 8.3 s alone).

The stopped dev server. deinit_if_we_can drops the DevServer in the frame that closes the last connection or completes the last request of a stopped server. On main the error page reaches that from check_route_failures and panics in a debug build (ThreadLock: ... already held). With this PR the dev server's 405 can reach it in one corner: a pipelined read fills the 128 KB cork buffer, so the socket is uncorked while it is parsed, and the 405 closes the last connection inside respond_for_html_bundle. That frame does not use the DevServer afterwards, and the run is clean under ASAN. The requests that waited for a bundle are safe, because the bundle holds a pending request across its answers. The teardown needs its own fix.

Self-review. 29 concerns.

  • Changed the design: the close inside the end is gone, and so are uws_res_abandon, the node:http change and its test.
  • Changed the tests: idleTimeout: 0, the round trip in the HTML fixture, the pendingRequests assertions, the release from a task of its own, the row for the dev server's 500, the 304 row for trailing work.
  • Changed comments that described the old gate.
  • Not addressed here (3): the teardown of a stopped dev server and tests for it. Main has it too.

Earlier attempts. #35555 (closed) and #37446 (closed by the stale sweep).


no test proof · iteration 1 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/http/serve.test.ts, test/js/bun/http/bun-server.test.ts

…at is marked to close

uws_res_end_without_body marked the response done and left the uncork and
the close gate to whoever held the socket's cork. Every other end runs both
itself. A bodyless end (a 304, a HEAD response, the bodyless error
responses) that completed outside the read of its request could therefore
leave a connection open that `Connection: close`, HTTP/1.0 or a graceful
server.stop() had marked to close: when another socket's cork() flushed its
cork slot first, and whenever the caller ran no gate of its own (the 500 of
an HTML import whose build failed, the 405 of the dev server).

The shim now completes through HttpResponse::finishResponseInCorkBuffer(),
the uncork and close gate that internalEnd() ends on. A response that
JavaScript produced is sent when it completes, with or without a body.

node:http res.destroy() ended its response through the same shim. It drops
what is still corked and closes the socket itself, so it gets
uws_res_abandon, which does what the shim did before.
@robobun

robobun commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author
Updated 8:42 AM PT - Nov 13th, 2026

✅ @robobun, your commit 1701d98d790422e611a0d572981c8258dd441343 passed in Build #141100! 🎉


🧪   To try this PR locally:

bunx bun-pr 48486

That installs a local version of the PR into your bun-48486 executable, so you can run:

bun-48486 --bun

@robobun

robobun commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator Author

Status

How it was reproduced, on main (d0f4fdd, debug ASAN build) and on the 1.4.3 canary:

  • Four Connection: close POSTs whose bodies never arrive, answered 304 by an async Bun.serve handler, all in one microtask drain. With idleTimeout: 0, two of the four connections get no FIN. On the canary they are still open after 30 s.
  • The same response while two corked WebSockets, or two streaming responses, take its cork slot.
  • The request that waited for an HTML import's bundle: the 500 Build Failed of a production build, the dev server's 405, and the dev server's 500 after a plugin failed to load. No FIN comes before the 10 s default timeout.

With this PR each connection closes right after the response. The tests are in test/js/bun/http/bun-server.test.ts, test/js/bun/http/bun-serve-html.test.ts and test/js/bun/http/serve.test.ts.

The first version of this PR closed the socket inside uws_res_end_without_body. That version is replaced: on a stopped dev server its close freed the DevServer under DevServer::defer_request.

PR: #48486. It stays a draft: a maintainer asked for it to be cut again on top of #46528 after that PR merges.

CI at 1701d98: every lane that ran passed. The four macOS x64 test jobs expired with no free agent.

… closes

uws_res_end_without_body is as on main again: it marks the response done
and runs no close gate. uws_res_abandon is gone.

uws_res_close_if_done_and_marked skipped a corked response and left it to
the cork() wrapper. That wrapper returns early when another socket's
cork() took the response's cork slot, so the connection stayed open. The
gate now uncorks as internalEnd does, then closes.

Three ends that run outside a uWS handler call the gate: the 500 of an
HTML import whose production build failed, the dev server's 405, and the
dev server's 500 after its plugins failed to load.

A close inside uws_res_end_without_body freed the DevServer under
DevServer::defer_request when a stopped dev server answered 413 on its
last connection.

The tests use idleTimeout: 0 or a later round trip, so that no idle sweep
can close the connection for the server.
@robobun robobun changed the title uws: make uws_res_end_without_body uncork and run the close gate uws: uncork in uws_res_close_if_done_and_marked, and call it after three ends outside a uWS handler Nov 13, 2026
Comment thread src/runtime/server/RequestContext.rs Resolved
Comment thread src/runtime/server/StaticRoute.rs Resolved
Comment thread src/uws_sys/Response.rs Resolved
Comment thread src/uws_sys/libuwsockets.cpp Resolved
Comment thread src/uws_sys/libuwsockets.cpp Resolved

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants