Repository navigation
node:http: resume the server socket on res.socket.end() so the unread body drains - #34356
Conversation
node:http pauses the accepted socket before dispatching the request handler so the request body buffers until user code attaches a listener. If the handler calls res.socket.end(), us_internal_socket_raw_shutdown masked the poll mask with the current events. A paused socket polls WRITABLE only, so the mask became 0 and is_paused stayed set: the unread body stalled TCP flow control, the peer's FIN never arrived, and server.close() never resolved. A shut-down socket's only remaining read purpose is draining to the peer's FIN so the is_shut_down close path in loop.c can fire, and HttpContext::onData already discards everything once shut down, so arm READABLE unconditionally and clear the pause. us_socket_pause becomes a no-op on a shut-down socket for the same reason.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughThe HTTP server socket end path now resumes paused, non-upgraded sockets. A regression test covers a 2MB ChangesSocket shutdown lifecycle
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
Updated 10:48 AM PT - Jul 16th, 2026
❌ @robobun, your commit b572554 has 2 failures in
🧪 To try this PR locally: bunx bun-pr 34356That installs a local version of the PR into your bun-34356 --bun |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/node/http/node-http.test.ts`:
- Around line 3938-3943: Remove the nested 10-second watchdog setTimeout blocks
in both affected shutdown paths, including the flow awaiting socketClosed and
serverClosed. Await serverClosed directly alongside the existing closure
condition, relying on the outer test timeout and await using lifecycle handling
without adding a shorter timer.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 6fde1e6c-1cf8-460c-be06-18b3fd67a1a9
📒 Files selected for processing (2)
packages/bun-usockets/src/socket.ctest/js/node/http/node-http.test.ts
Review pointed out the usockets-level change breaks node:net read backpressure for a client that calls .end() and then reads a large response: the net.ts data handler's socket.pause() became a no-op once shut down, so the whole response buffered in the JS Readable. The pause that causes the hang is node:http's own body-buffering pause (handle.pause() in onNodeHTTPRequest before the user handler), so undo it in the node:http end() path rather than at the usockets layer. us_socket_resume before the shutdown re-arms READABLE; HttpContext::onData discards everything once shut down, so the socket drains the unread body to the peer's FIN and closes.
There was a problem hiding this comment.
The earlier socket.c revision's node:net backpressure regression is resolved — usockets is untouched now, so us_socket_pause still applies flow control on half-closed net.Socket clients. Also checked that us_socket_resume on an already-resumed socket is a no-op (it just re-arms READABLE), so the unconditional call here is safe when the body was already being read.
Extended reasoning...
The author addressed the prior 🔴 finding by moving the fix out of us_socket_pause/us_internal_socket_raw_shutdown and into jsFunctionNodeHTTPServerSocketEnd, which is scoped to node:http server sockets only. packages/bun-usockets/src/socket.c is no longer in the diff, so the node:net half-close read-backpressure path is unchanged from main. The remaining inline nit (missing us_socket_resume declaration) is build hygiene, not a runtime concern. Not approving because this is subtle transport-layer teardown that deliberately diverges from Node.js behavior — worth a maintainer confirming the layer choice.
|
Review addressed:
Self-review concerns about CI#73961 (b572554): The diff is green; ready for a maintainer to merge. |
There was a problem hiding this comment.
Both earlier findings (the us_socket_pause guard breaking node:net backpressure, and the missing extern "C" declaration) are addressed; no new issues found this pass. The final change is small and us_socket_resume is idempotent/closed-safe, but this is socket shutdown ordering in the node:http server path and deliberately diverges from Node's behavior (drain-and-close vs. Node's hang), so it's worth a maintainer sign-off.
Checked: us_socket_resume early-returns on !is_paused and on closed sockets, so the unconditional call is safe when the body-buffering pause was already undone; the !upgraded guard keeps tunnel-mode read backpressure intact; the new tests assert on {stdout, stderr, exitCode} and drain both pipes concurrently.
Extended reasoning...
Overview
The PR adds a single us_socket_resume(thisObject->socket) call (guarded by socket && !upgraded) in jsFunctionNodeHTTPServerSocketEnd before the shutdown path, plus a local extern "C" declaration so the file compiles without unified-source bundling. It also adds six parameterized subprocess tests (net + fetch client × 1/2/8 MB body) to test/js/node/http/node-http.test.ts. An earlier revision changed packages/bun-usockets/src/socket.c directly; that was reverted after review flagged that it disabled read backpressure for node:net clients that half-close and then read.
Security risks
None identified. The change re-arms the readable poll on a server-side socket that the handler is already ending; the drained bytes are discarded by HttpContext::onData once us_socket_is_shut_down(s). No user-controlled input reaches new parsing or allocation.
Level of scrutiny
Higher than the diff size suggests. This is shutdown/pause/resume ordering on the node:http server socket — the exact area where the first revision of this PR introduced a real regression (unbounded buffering in a paused half-closed net.Socket client). The current revision is scoped to the one call site whose pause is unconditionally node:http's own body-buffering pause, and us_socket_resume at socket.c:850-858 is idempotent (returns if !is_paused), closed-safe, and already special-cases the shut-down state. The !upgraded guard is correct: upgraded tunnel sockets deliver data via functionToCallOnData where a pause reflects user backpressure, not the request-body buffering pause.
Other factors
- The PR description states Node.js also hangs on this repro and that draining-then-closing is a deliberate divergence. That's a reasonable choice given uWS's shut-down read path already discards, but it's a policy call a maintainer (cirospaciari / Jarred) should confirm.
- Tests follow repo conventions: subprocess-per-case with
await using, concurrent pipe drain viaPromise.all, combined{stdout, stderr, exitCode}assertion, watchdog inside the fixture (already litigated with CodeRabbit and kept for diagnostic value on regression). - Both prior inline findings from this reviewer are resolved in d1fe341 and e483f6d.
node-http.test.ts has a pre-existing localhost-resolution failure in the 'request via http proxy, issue#4295' test in this environment, which trips the fail-with-fix check even though it is unrelated to this change. A dedicated file gives a clean 5 fail / 0 fail proof on both the ASAN and release builds.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/node/http/node-http-server-socket-end-drain.test.ts`:
- Around line 41-46: Remove the child-process watchdog timer and matching
cleanup around the socket/server close await in
test/js/node/http/node-http-server-socket-end-drain.test.ts:41-46, and remove
the equivalent fetch-client timer and cleanup at
test/js/node/http/node-http-server-socket-end-drain.test.ts:83-88. Preserve the
existing await conditions and rely on the test runner timeout and await using
proc for hang protection and subprocess cleanup.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 352c06d7-46c7-4f50-9c9f-a68ff92ce420
📒 Files selected for processing (1)
test/js/node/http/node-http-server-socket-end-drain.test.ts
Six concurrent sanitizer-instrumented subprocesses on the 2-vCPU x64-asan lane starved each other past the explicit 15 s per-test timeout, which also overrode the runner's 270 s ASAN default. The 2 MB case alone proves the fix (it sits between the first recv and the kernel send buffer, so it hangs deterministically on an unpatched build), and two subprocesses fit the 2-vCPU budget.
There was a problem hiding this comment.
No issues found in this revision — all three concerns from earlier passes (the over-broad us_socket_pause guard, the missing extern "C" decl, and the per-test timeout) have been addressed. Deferring to a maintainer because this touches the node:http server socket lifecycle and deliberately diverges from Node.js (which also hangs on this repro); that trade-off is worth a human sign-off.
Checked: us_socket_resume is safe to call pre-shutdown (early-returns if not paused/closed), and the subsequent raw_shutdown mask (events & READABLE) now keeps READABLE armed. The !upgraded guard correctly excludes tunnel-mode sockets, and the socket null check is defensive on top of the isClosed() early return. The test file no longer sets a per-test timeout and is trimmed to the 2 MB case.
Extended reasoning...
Overview
The final diff is 7 lines in src/jsc/bindings/node/JSNodeHTTPServerSocketPrototype.cpp (an extern "C" declaration for us_socket_resume and a guarded call to it in jsFunctionNodeHTTPServerSocketEnd before the shutdown path) plus a new 95-line test file spawning two subprocesses that reproduce the server.close() hang after res.socket.end() with an unconsumed upload body.
This PR has been through three rounds of my inline review, each addressed by a follow-up commit: d1fe341 reverted the over-broad usockets-level change and scoped the fix here; e483f6d added the local extern declaration so the file builds without unified-source leakage; 0ecfe6e dropped the explicit 15_000 per-test timeout and trimmed the test matrix to a single body size after the ASAN lane flaked. The bug hunting system found nothing in the current revision.
Security risks
None. The change resumes a socket that node:http itself paused, immediately before shutting it down; HttpContext::onData discards all bytes on a shut-down socket, so no user callback sees the drained data. No auth, crypto, or permission surface.
Level of scrutiny
Medium-high. The C++ change is tiny and mechanically sound — us_socket_resume early-returns when the socket isn't paused or is closed, and calling it before shutdown means raw_shutdown's events & READABLE mask keeps READABLE armed instead of zeroing the poll. But this is HTTP server socket lifecycle code, and the pause/resume/shutdown interaction in usockets is subtle enough that the first iteration of this PR introduced a real node:net backpressure regression. The current scoping to JSNodeHTTPServerSocket::end() avoids that, and the author verified the net-client backpressure probe matches 1.4.0.
Other factors
The PR description explicitly notes that Node.js also hangs on this repro and that matching Node would leak the connection. Choosing to drain-and-close instead is a reasonable call given uWS's shut-down read path is already designed to discard, but it's a deliberate compatibility divergence a maintainer should confirm. Combined with the critical-path nature of the change, that tips this to defer rather than approve.
There was a problem hiding this comment.
No further findings — the earlier concerns (over-broad us_socket_pause guard, missing extern "C" decl, explicit test timeout) are all addressed in the current diff. Leaving final sign-off to a maintainer since this touches native socket lifecycle / poll-mask semantics on the node:http server path and deliberately diverges from Node's (hanging) behavior.
Checked this pass:
!thisObject->upgradedguard matches the existing pattern at JSNodeHTTPServerSocket.cpp:80/258/292/425/450 (WebSocket-upgraded sockets have different ext data).us_socket_resumeextern signature matches libusockets.h:634.- Ruled out: test's
stderr: ""assertion flaking under ASAN; test passing on release-without-fix invalidating the regression gate.
Extended reasoning...
Overview
The PR adds a us_socket_resume() call in jsFunctionNodeHTTPServerSocketEnd (JSNodeHTTPServerSocketPrototype.cpp) before the shutdown path, plus a local extern "C" declaration, and a new two-case regression test file. The resume undoes the body-buffering pause that onNodeHTTPRequest applies before dispatching to the user handler, so that after res.socket.end() the socket keeps reading (and discarding, via HttpContext::onData's shut-down early return) until the peer's FIN arrives and the close path runs.
Prior review history
I raised three inline findings on earlier revisions: (1) the original usockets-level us_socket_pause guard broke node:net read backpressure after half-close — the author reverted socket.c and scoped the fix to the node:http server end path; (2) us_socket_resume was only visible via unified-source leakage — the author added the local extern; (3) the explicit 15_000 per-test timeout — the author dropped it and trimmed the matrix to the 2 MB case after CI showed it overriding the ASAN runner default. All three threads are resolved and reflected in the current diff.
Security risks
None identified. The change is a resume call on a server-owned socket immediately before shutdown; no user input is parsed, no new allocation, no auth/crypto surface. The !upgraded guard prevents the call from touching WebSocket-upgraded sockets whose ext data is a different type.
Level of scrutiny
Medium-high. Seven native lines, but in the HTTP server socket lifecycle where the interaction between is_paused, poll masks, raw_shutdown, and the loop's drain-to-FIN path is subtle — the first revision's over-broad approach demonstrably regressed node:net backpressure. The current scoping is narrow and matches sibling guards in the same class, and the mechanism is well-traced in the PR description, but this is exactly the kind of change a maintainer familiar with the uWS/usockets layer should confirm.
Other factors
The PR explicitly notes Node.js also hangs on this repro, so the fix is a deliberate divergence toward the behavior Bun's own await using server test already expects. That's a reasonable call but a design decision, not a mechanical fix. The bug-hunting pass found nothing; two candidate concerns (empty-stderr assertion, release-build gate) were examined and ruled out by verifiers.
On macOS the fetch client may pool the connection after the upload fails, so the server drains the body but never receives the peer's FIN and server.close() waits on it. That is a client-side connection- lifecycle concern, not the server-side drain this change covers. The net client sends its FIN deterministically (c.end() on 'end') and passed on every lane in #73919, so it is the regression proof on its own.
There was a problem hiding this comment.
No new issues found — all three earlier findings (the over-broad us_socket_pause guard, the missing extern "C" for us_socket_resume, and the explicit per-test timeout) have been addressed, and the current diff is scoped to the node:http server-socket end() path only. Deferring to a maintainer because this is native HTTP server socket lifecycle code and the PR explicitly deviates from Node.js behaviour (Node also hangs on the repro), which is a compat-layer call worth a human sign-off.
Checked: us_socket_resume is idempotent (!is_paused early-return) and closed-safe, so the unconditional call is harmless when the body was already resumed; the !upgraded guard keeps WebSocket/tunnel sockets out of this path; the resume runs before shutdownAfterResponseDrains() so both SSL and non-SSL reach raw_shutdown with READABLE armed. socket.c is untouched, so the earlier node:net read-backpressure regression no longer applies.
Extended reasoning...
Overview
Two files: a 7-line addition to jsFunctionNodeHTTPServerSocketEnd in src/jsc/bindings/node/JSNodeHTTPServerSocketPrototype.cpp (one extern "C" declaration + a guarded us_socket_resume call before the shutdown sequence), and a new 54-line subprocess test at test/js/node/http/node-http-server-socket-end-drain.test.ts. The fix undoes node:http's own body-buffering pause on res.socket.end() so the unread request body drains and the peer's FIN reaches the server, letting server.close() complete instead of parking in FIN-WAIT-2.
Security risks
None identified. The change adds a resume on a server-owned socket in a path already gated by isClosed(); us_socket_resume itself early-returns on not-paused and closed sockets. No user input reaches the new code beyond what already reached end(). The resumed reads are discarded by HttpContext::onData once shut down, so there is no new data-delivery surface.
Level of scrutiny
Medium-high. This is production native code on the HTTP server request/connection lifecycle, and the interaction between pause/resume and shutdown is subtle enough that the first iteration of this PR (at the usockets layer) introduced a real node:net backpressure regression before being re-scoped here. The current change is narrowly targeted, well-reasoned in the description, and has a regression test that fails on the debug build without the fix. That said, the PR states Node.js itself hangs on this reproduction — so this is a deliberate deviation from Node behaviour in the compat layer, justified as "matching Node would mean leaking the connection." That's a reasonable position but is exactly the kind of compat-vs-correctness call a maintainer should confirm.
Other factors
All three prior review threads from claude[bot] and the CodeRabbit threads are resolved with code changes or reasoned pushback. The test was iterated through two CI failures (ASAN lane starvation from six concurrent subprocesses; macOS fetch-client pooling nondeterminism) and trimmed to one deterministic net-client case. The subprocess watchdog pattern was debated and kept for diagnostic value on regression; that's consistent with similar tests in node-http.test.ts. The bug hunting system found nothing on this pass. The change is small enough that I'd be comfortable approving on mechanics alone, but the explicit Node-deviation and the native socket-lifecycle location tip it toward a human look.
On kqueue the close-after-res.socket.end() path on main relies on the one-shot EVFILT_WRITE that us_socket_pause left registered: the W->0 transition in kqueue_change emits no kevent, and after shutdown(SHUT_WR) that leftover filter fires with EV_EOF, which the is_shut_down branch in loop.c turns into a close. Resuming before the shutdown transitioned W -> R|W -> R, and the R|W -> R step deletes EVFILT_WRITE, so macOS lost its close signal and node-http-halfclose-midupload.test.ts and test-http-should-not-emit-or-throw-error-when-writing-after-socket.end started hanging there. Resuming after the shutdown transitions W -> 0 -> R: the 0 -> R step adds EVFILT_READ without touching the leftover one-shot WRITE, so kqueue keeps its EV_EOF close signal and epoll gets READABLE to drain the unread body.
There was a problem hiding this comment.
No issues found in the current revision, and all three earlier findings (the over-broad us_socket_pause guard, the missing extern "C" decl, and the per-test timeout) have been addressed. Deferring to a human because this is core node:http server socket lifecycle with platform-specific poll ordering — the last commit reorders the resume relative to the shutdown specifically for kqueue's one-shot EVFILT_WRITE semantics, which is subtle enough to want maintainer eyes.
Checked: the resume is now scoped to the node:http end() path only, so node:net read backpressure after .end() is unaffected (socket.c is untouched); the !thisObject->upgraded guard keeps CONNECT tunnels from being spuriously resumed; the deferred-shutdown paths (shutdownAfterResponseDrains(), bufferedSize > 0) were examined and ruled out as still-hanging; the test's combined {stdout, stderr, exitCode} assertion matches the harness convention and passed on the ASAN lane per the evidence block.
Extended reasoning...
Overview
The PR adds a us_socket_resume() call in jsFunctionNodeHTTPServerSocketEnd (src/jsc/bindings/node/JSNodeHTTPServerSocketPrototype.cpp) immediately after the buffered-write-that-shuts-down, plus a local extern "C" declaration for the function. It also adds a new subprocess-based regression test at test/js/node/http/node-http-server-socket-end-drain.test.ts. The fix undoes onNodeHTTPRequest's body-buffering pause on the res.socket.end() path so the unread request body drains, the peer's FIN arrives, and server.close() resolves.
Security risks
None identified. The change adds a resume call on a socket the server already owns; it does not touch validation, auth, TLS, or any user-controlled input parsing.
Level of scrutiny
This warrants human review. It's a small diff (7 lines of production code) but sits at the intersection of node:http request lifecycle, uWS/usockets pause/shutdown state, and platform-specific poll semantics. The PR went through three substantive revisions in response to review: the original usockets-level change broke node:net read backpressure after half-close (my first finding), the fix was re-scoped to JSNodeHTTPServerSocket::end(), and then the resume was moved from before to after the shutdown in the final commit (b572554) with a comment explaining a kqueue one-shot EVFILT_WRITE / EV_EOF ordering constraint. That last change in particular — a W→R|W→R step deleting the write filter that would have delivered EV_EOF on SHUT_WR — is the kind of platform-sensitive event-loop reasoning a maintainer familiar with the usockets kqueue backend should confirm.
Other factors
- All three of my earlier inline findings have been addressed and the threads are resolved.
- The bug-hunting system raised and refuted two candidates on the current revision: (1) that the resume only fires on the
bufferedSize == 0path so deferred-shutdown paths still hang, and (2) that the test assertsstderris exactly empty. Both were verified as non-issues. - The evidence block shows the new test fails on main (debug/ASAN) and passes with the fix on both debug/ASAN and release, and the author reports
node-http.test.ts,serve.test.ts,node-net.test.ts, and relatedtest-http-*files unchanged in pass/fail counts. - CI build #73942 was reported green for this test on every lane; #73961 for the final ordering commit is building.
- The PR description notes Node.js also hangs on this repro, so the change is a deliberate deviation from Node behavior in favor of not leaking connections — a design call worth a maintainer's ack.
|
This test went red on macOS (arm64 and x64) since it landed: the server's own |
…n socket-end-drain (#34678) Fixes `test/js/node/http/node-http-server-socket-end-drain.test.ts` going red on macOS (both arm64 and x64) since #34356 landed. ### Failure ``` expect(received).toEqual(expected) { - exitCode: 0, - stderr: "", - stdout: "closed destroyed=true\n", + exitCode: 1, + stderr: "EPIPE: broken pipe, write\n syscall: \"write\", errno: -32, ...", + stdout: "", } ``` Reproduced on a macOS arm64 box at 39/50 runs with the exact child script. ### Cause On kqueue, the server socket is paused (poll = `WRITABLE` only) when the request handler runs. `res.socket.end()` then does: 1. `us_internal_socket_raw_shutdown`: `us_poll_change(W & R = 0)`. `kqueue_change` going `W -> 0` does **not** `EV_DELETE` the one-shot `EVFILT_WRITE` (the `!readable && !writable` branch only adds, never deletes). Stored events become `0`; kqueue still has `EVFILT_WRITE`. 2. `bsd_shutdown_socket` (`SHUT_WR`): macOS now reports `EV_EOF` on that `EVFILT_WRITE`. 3. `us_socket_resume`: `us_poll_change(0 -> R)` adds `EVFILT_READ`. The next `kevent64()` returns `EVFILT_WRITE|EV_EOF` and `EVFILT_READ` for the same poll. The coalescing pass (`epoll_kqueue.c:241,252`) ORs `eof=1` from the write filter into the dispatched event. `events &= us_poll_events(poll)` masks `WRITABLE` out, but `eof` passes through, so `loop.c:829` sees `eof && us_socket_is_shut_down` and closes the socket as "got FIN back after sending it". The server fd is closed with unread body bytes in the receive buffer, so macOS RSTs the client, and the client's next write fails with `EPIPE`/`ECONNRESET`. That error is swallowed by `c.on("error", () => {})`, but `once(c, "close")` also listens for `'error'` and **rejects** with it; the rejection reaches the top-level `await Promise.all` and becomes the unhandled error on stderr. The #34356 code comment already names the kqueue path ("kqueue's one-shot EVFILT_WRITE (which delivers EV_EOF on SHUT_WR) is not deleted"), so the server closing early on macOS is the intended release mechanism there; only the test's wait was wrong. ### Fix Wait for the client's `'close'` with a plain listener instead of `once()`, so the expected EPIPE cannot reject `Promise.all`. The assertion (`server.close()` fires, server socket destroyed, client closes) is unchanged and still exercises the #34356 code path: without the `us_socket_resume` the child times out on Linux and (most runs) on macOS. Also moved the client's error handler ahead of the writes for clarity. ### Verification - darwin-aarch64, main build 75566 binary: **100/100** `closed destroyed=true` - darwin-aarch64, 1.3.14 (no #34356): 4/5 `timeout` (fail-before holds) - linux x64 `bun bd test`: pass - linux x64 `USE_SYSTEM_BUN=1 bun test`: fail (timeout) Culprit PR: #34356. <!-- robobun:evidence:begin --> --- **[stamp-90s]** gate passed · iteration 0 · 1 files touched <details><summary>passes on PR (with fix)</summary> ```console Test-only change. Debug/ASAN (expected pass): $ bun bd test 'test/js/node/http/node-http-server-socket-end-drain.test.ts' $ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test test/js/node/http/node-http-server-socket-end-drain.test.ts info: syncing channel updates for nightly-2026-05-06-x86_64-unknown-linux-gnu info: latest update on 2026-05-06 for version 1.97.0-nightly (e95e73209 2026-05-05) info: component rust-src is up to date info: checking for self-update (current version: 1.29.0) bun test v1.4.0 (6d450e2) test/js/node/http/node-http-server-socket-end-drain.test.ts: (pass) server.close() completes after res.socket.end() with a 2 MB upload in flight [2948.87ms] 1 pass 0 fail 1 expect() calls Ran 1 test across 1 file. [5.17s] Exit: 0 ``` </details> <details><summary>diff hotspot</summary> ``` .../js/node/http/node-http-server-socket-end-drain.test.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) ``` </details> **gate history** · 1 passed · 0 rejected · iteration 0 <details><summary>evidence per changed file</summary> ``` file reads edits tests …/js/node/http/node-http-server-socket-end-drain.test.ts 1 1 0 ``` </details> **self-review** · no surviving concerns 26 concerns were raised and did not survive verification. <!-- robobun:evidence:end -->
…erSocket::halfClose() (#44243) ### Problem - The native `end()` of the `node:http` server socket holds its half-close sequence inline (`jsFunctionNodeHTTPServerSocketEnd`, `src/jsc/bindings/node/JSNodeHTTPServerSocketPrototype.cpp:313`). - The next PR of this stack calls the same sequence from the drain gate of uWS, for a `socket.end()` that waited for buffered bytes. A second copy can drift from the first. ### Fix - Move the sequence into `JSNodeHTTPServerSocket::halfClose()`. The move is verbatim: pause the reads, write with `ended` set (this sends the FIN), resume the reads. - `jsFunctionNodeHTTPServerSocketEnd` calls it and returns its value, as before. - Remove the `us_socket_pause` and `us_socket_resume` declarations from the prototype file. They have no user there now. - Verified: the existing suites, unchanged, on a release build of this commit. The Notes block has the list and the counts. Behaviour change: none ### Background - A half-close sends the FIN and keeps the socket open for reads. `socket.end()` in Node is a half-close. - The reads must be armed after the shutdown. If they are not, the server does not see the FIN of the peer, and the socket never closes (#34356). - This is PR 1 of 2. #44244 is PR 2. The alternative was a second inline copy of the sequence for the new caller. ### Downsides - None found. Checked the release builds of main 9f70da0 and of this commit (Linux x64). The compiler inlines `halfClose()`: `jsFunctionNodeHTTPServerSocketEnd` goes from 633 to 671 bytes, and from 192 to 191 instructions. `.text` (58148853 bytes) and the binary (80832072 bytes) do not change. <details><summary>Notes</summary> All runs are on Linux x64, on release builds of main 9f70da0 and of this commit 7c98ad5. | Suite | Result on this commit | |---|---| | `test/js/node/http/node-http-server-socket-end-drain.test.ts` | 9 pass | | `test/js/node/http/node-http-connect.test.ts` | 61 pass, 2 todo | | `test/js/node/http/node-http-backpressure.test.ts` | 83 pass | | `test/js/node/http/node-http-pinned-write.test.ts` | 10 pass | | `test/js/node/http/node-http-server-close-drain.test.ts` | 28 pass | | `test/js/node/http/node-http.test.ts` | 263 pass, 1 skip | | 43 files of `test/js/node/test/parallel/` (`test-http-*`, `test-https-*`) that start a server and end, pipeline or write on its socket | 43 exit 0 (main: 43) | A debug build with ASAN of this commit alone did not run. The same suites ran on a debug build with ASAN of the second PR of the stack, which contains this change. They pass there, apart from three timeouts from the load of the host. Each of those three tests passes alone. #44244 has the list. Method for the sizes: `llvm-nm -S` and `llvm-objdump -d` on `bun-profile`, `llvm-size -A` for `.text`. Every other function of the socket binding and of `HttpContext` has the same bytes in both builds. </details>
Related: #34331 (Windows sibling of the same test, different mechanism).
Reproduction
On Linux with the installed 1.4.0:
server.close()never resolves;ssshows the server socket parked inFIN-WAIT-2with the request body stuck in the kernel receive queue and the client inLAST-ACKwith its FIN queued behind unsent body bytes. With a 10 MB body the close happens to complete because the blocked upload provokes an RST that reaches the server viaEPOLLERR, so the existingtest-http-should-not-emit-or-throw-error-when-writing-after-socket.end.ts(which uses 10 MB andawait using server) passes only by accident.Cause
onNodeHTTPRequestcallshandle.pause()before emitting'request'so the request body buffers until user code attaches a listener.us_socket_pausesetsis_paused = 1and re-arms the poll forWRITABLEonly. The handler'sres.socket.end()reachesus_internal_socket_raw_shutdownsynchronously:so the socket ends up polling neither direction with
is_pausedstill set. On epoll nothing ever reads from the fd, so TCP flow control closes the receive window, the client's FIN sits behind its unsent body bytes, and theis_shut_downclose path inloop.cnever runs. kqueue happens to close because theW -> 0transition inkqueue_changeemits no kevent, leaving the one-shotEVFILT_WRITEregistered, and aftershutdown(SHUT_WR)it fires withEV_EOF.Fix
JSNodeHTTPServerSocket::end()now callsus_socket_resumeafter the shutdown. The pause it is undoing is node:http's own body-buffering pause, andHttpContext::onDatadiscards everything onceus_socket_is_shut_down(s)(it returns before the parser runs), so the resumed reads are pure drain-to-FIN: the recv loop discards the unread body,recv() == 0setseof, andloop.ctakes the existingis_shut_downclose path. Resuming after the shutdown transitionsW -> 0 -> R, which addsEVFILT_READwithout touching the leftover one-shotEVFILT_WRITE, so kqueue keeps itsEV_EOFclose signal (resuming before the shutdown transitionedW -> R|W -> R, and theR|W -> Rstep deletes that filter, which regressednode-http-halfclose-midupload.test.tson macOS).An earlier revision put the change in
us_internal_socket_raw_shutdown/us_socket_pause; review pointed out that disablesnode:netread backpressure for a client that calls.end()and then reads a large response, so the fix is scoped to the node:http server socket end path instead.Node.js also hangs on this repro; matching Node would mean leaking the connection. The Bun test that exercises
res.socket.end()with an in-flight upload expectsawait using serverto return, and uWS's shut-down read path is already designed to discard, so draining and closing is the behaviour the surrounding code was written for.Verification
test/js/node/http/node-http-server-socket-end-drain.test.tsspawns a subprocess with a 2 MBnode:netupload (the size that deterministically reproduces the hang) so the pre-fix hang shows up as a watchdog exit. Fails without thesrc/change and passes with it on both the ASAN and release builds.node-http.test.ts,serve.test.ts,node-net.test.ts,socket.test.ts,node-http-halfclose-midupload.test.ts, and the relatedtest/js/node/test/parallel/test-http-*files have the same pass/fail counts asmain. Anode:netclient that calls.end()and then reads a large response with the Readable paused buffers 0 bytes in the JS Readable after this change, same as the installed 1.4.0.[review] gate passed · iteration 5 · 2 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 4 passed · 1 rejected · iteration 5
evidence per changed file