Repository navigation
uws: uncork in uws_res_close_if_done_and_marked, and call it after three ends outside a uWS handler - #48486
uws: uncork in uws_res_close_if_done_and_marked, and call it after three ends outside a uWS handler#48486robobun wants to merge 5 commits into
Conversation
…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.
|
Updated 8:42 AM PT - Nov 13th, 2026
✅ @robobun, your commit 1701d98d790422e611a0d572981c8258dd441343 passed in 🧪 To try this PR locally: bunx bun-pr 48486That installs a local version of the PR into your bun-48486 --bun |
|
Status How it was reproduced, on
With this PR each connection closes right after the response. The tests are in The first version of this PR closed the socket inside 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.
…less-end-gates-itself
Problem
Connection: close, HTTP/1.0,server.stop()). No FIN comes until the idle timeout, none withidleTimeout: 0.uws_res_close_if_done_and_marked(src/uws_sys/libuwsockets.cpp) left a corked response toHttpResponse::cork(). That returns early when another socket'scork()took the slot (HttpResponse.h:968).Fix
internalEnd()does.RequestContext::end_without_bodyalready calls it before its microtask drain.500 Build Failedand the dev server's 405 and 500 now call the gate.bun-server.test.ts,bun-serve-html.test.tsandserve.test.tsfail on main d0f4fdd. Self-reviewed: 29 concerns raised, 26 addressed.Background
cork()flushes the oldest.uws_res_end_without_bodyfreed theDevServerunderDevServer::defer_request. A gate atcork()'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
send(): 5 TCP segments for 8 HEAD responses, was 3..textstays 179,756 bytes.DevServerunder 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 completedConnection: 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, andawait server.stop()waits for it.Repro. Four
Connection: closePOSTs whose bodies never arrive, answered 304 by an async handler that all four leave in one microtask drain:On the 1.4.3 canary with
idleTimeout: 0, two of the four connections are still open after 30 s. WithidleTimeout: 4the 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_bodyends the response insideresp.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 thirdcork()flushes the least recently used slot. When the firstcork()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
idleTimeoutsays:500 Build Failedof a production build that failed (HTMLBundle.rs).StaticRoute::on_with_method).[serve.static]plugins failed to load (DeferredRequest::abort).What each caller of the gate sees now.
RequestContext::end_without_body, insidecork()after anawait: 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 toonData, asinternalEnd()does for a JS-produced response.FileResponseStream::end_sendfile: no change.uws_res_prepare_for_sendfileuncorks before the firstsendfile().onDatais done (staticHeadandstatic304still take 1 segment for 8 responses).Designs.
uws_res_end_without_body(first version of this PR)panic: Address is poisoned:deinit_if_we_candrops theDevServerunderDevServer::defer_requestuws_res_end_without_body. Bun.serve: close idle connections on graceful stop(), declare closeIdleConnections() #37074 put a gate in that shim and took it out before it merged. http, net, tls: consolidate the open fetch, Bun.serve, node:http, node:http2, HTTP/3, node:net and node:tls pull requests #46528 restates the note.cork()(uws: run the close gate in HttpResponse::cork() when the cork slot was lost #37446) runs for every response with a body that follows anawait. Since WebSocket: real closing handshake, one failure path,wsrewritten on native transports (consolidates the open WebSocket PRs) #45223server.upgrade()runs insideresp.corked, sothiscan be a live WebSocket at that return.Loop::preCbandLoopData, and it is not built.#46528. That branch moves the bodyless end into
HttpResponse::endWithoutBodyand 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 the500 Build Failedinend_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: closeon the request, HTTP/1.0,Connection: closeon the response,server.stop(), TLS) and 2 cases where two WebSockets or two streaming responses take the slot. The servers useidleTimeout: 0, so only the server's own close can end the connection. Each test also assertsserver.pendingRequestsat 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 withended: false.serve.test.ts, "share one send() until a handler runs JavaScript": new rows for HEAD and 304.mixedHeadis 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.
libuwsockets.cpp) compiled with the release flags, without LTO:.text179,756 bytes on both sides. 550 of 551 functions are instruction-identical.uws_res_close_if_done_and_markedis 135 bytes on both sides, 47 and 52 instructions.isCorked()), 70 here (30 in the shim and 40 inuncorkCompletedResponse()).TCP_INFOin theserve.test.tstest.bun_runtime, and dynamic instruction counts.perf,valgrind,straceandbloatyare not in this container, and a release build ofbun_runtimedoes not fit in its memory.Builds.
bun bddoes not compile main d0f4fdd:BunPlugin.cpp:3546: no member named 'SuspendExceptionScope' in namespace 'JSC'. Both debug builds here had the two#includelines 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. Inserve.test.tstwo 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_candrops theDevServerin the frame that closes the last connection or completes the last request of a stopped server. On main the error page reaches that fromcheck_route_failuresand 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 insiderespond_for_html_bundle. That frame does not use theDevServerafterwards, 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.
uws_res_abandon, the node:http change and its test.idleTimeout: 0, the round trip in the HTML fixture, thependingRequestsassertions, the release from a task of its own, the row for the dev server's 500, the 304 row for trailing work.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