usockets: close a TLS socket whose send() keeps failing instead of spinning the writable dispatch - #34510
usockets: close a TLS socket whose send() keeps failing instead of spinning the writable dispatch#34510robobun wants to merge 8 commits into
Conversation
us_socket_raw_write (packages/bun-usockets/src/socket.c) has no errno classification: any send() -1 sets last_write_failed and re-arms the writable poll. The SSL spill-drain path (us_internal_ssl_on_writable -> ssl_drain_spill -> us_socket_raw_write) therefore loops as long as the kernel keeps the fd writable and send() keeps failing. Fixture arms fault injection to return -1 EPIPE for N consecutive send() calls on a TLS client fd after the handshake completes, issues one write (which spills), and waits for the server to receive the data. On main, all N failures are silently consumed by the writable re-arm loop and the server receives; the socket never errors or closes. N=50000: SPIN received=true errored=false closed=false N=50000 cpu=2305ms wall=1342ms Same result for ECONNRESET and EINVAL. Plain TCP (node:net) does not spin because its write path uses us_socket_write_check_error, which has bounded retry plus peer-gone classification.
… cannot spin on a dead fd us_socket_raw_write (and us_socket_write / us_socket_raw_writev / us_socket_write2 / us_socket_ipc_write_fd) treated any -1 from bsd_send as a partial write: set last_write_failed, re-arm the writable poll, return 0. The SSL ciphertext spill path (us_internal_ssl_on_writable -> ssl_drain_spill -> us_socket_raw_write) therefore re-entered on every writable event for as long as send() kept failing and the kernel kept the fd writable, pinning a core at 100%. Observed on macOS as idle long-lived processes spinning in us_internal_ssl_on_writable -> __sendto with the kernel logging from inside every sosend, consistent with a Network Extension content filter returning an errno on a socket it has not closed. Reproduced on Linux and macOS via fault injection (50000 consecutive EPIPE failures were silently retried and the data eventually delivered once the fault disarmed; the socket never errored or closed). Fix: factor the errno classification from us_socket_write_check_error into us_internal_on_short_write() and call it from every write helper. A peer-gone errno, or an unclassified errno that persists past the existing bounded retry window, latches fatal_send_errno on the socket; loop.c closes the socket with that errno after the writable dispatch instead of re-arming for another retry. Would-block, transient ENOBUFS/ENOMEM, and bounded unclassified errnos still re-arm. fatal_send_errno lives in the pad-to-pointer gap before group, so struct us_socket_t does not grow.
|
Updated 10:26 PM PT - Jul 17th, 2026
❌ @robobun, your commit 9dc972c has 1 failures in
🧪 To try this PR locally: bunx bun-pr 34510That installs a local version of the PR into your bun-34510 --bun |
WalkthroughChangesThe socket layer now uses unified pending actions for deferred closure, shutdown, and detachment. POSIX send failures are classified with bounded retries, fatal writes close during writable dispatch, TLS paths adopt the shared state, and skipped tests cover fault scenarios. Socket pending actions and send handling
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
Comment |
|
Found 2 issues this PR may fix:
🤖 Generated with Claude Code |
Review follow-up: us_internal_on_short_write is only called on written != length, so its counter reset never ran for a full write and the counter measured lifetime (not consecutive) unclassified errnos on the TLS spill path. Reset it on written == length in each write helper, matching us_socket_write_check_error. Also refactor us_socket_write_check_error's POSIX errno branch to use the shared us_internal_classify_send_error so the classification lives in one place. New test asserts 50 rounds of one-off EPROTOTYPE each recovered by a successful drain leave the socket open (it closed at round 33 before the reset).
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 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/bun/net/socket-syscall-fault.test.ts`:
- Around line 241-269: Update tlsPair to pre-register cleanup immediately after
creating the server and socket, then wrap listening, secureConnect, and fd
validation in try/catch; on setup failure, clear fault, destroy the socket,
close the server, and rethrow the original error. Preserve the existing
Symbol.dispose cleanup for successful setup.
- Around line 295-307: Update the unclassified-errno retry test around the
existing “unclassified errno” case to cover both sides of the 32-failure
boundary: add a case where exactly 32 consecutive EINVAL send failures recover
and deliver the bytes, and a case where the 33rd consecutive failure closes the
socket with EINVAL. Replace the broad repeat: 200 assertion with exact failure
counts and verify both closure/error behavior and server delivery.
- Around line 319-328: Bound the receive-polling loop in the test around
pair.serverReceived() with a finite maximum number of event-loop turns; if the
expected 8 * (i + 1) bytes are not received and sawClose remains false, fail the
test while reporting the expected and actual byte counts. Preserve the existing
early exit on sawClose and the per-round errno-then-success behavior.
🪄 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: 72a27781-ba76-4742-b239-f7cea78abaa7
📒 Files selected for processing (5)
packages/bun-usockets/src/context.cpackages/bun-usockets/src/internal/internal.hpackages/bun-usockets/src/loop.cpackages/bun-usockets/src/socket.ctest/js/bun/net/socket-syscall-fault.test.ts
|
CI on 9dc972c (build #74980): the new Since the last update: 32441db folds the latch into the existing deferred-close state (one Ready for re-review. |
The comment said the write helpers stop re-arming on fatal, but us_internal_on_short_write re-arms once so loop.c's check delivers the close. Match the (correct) socket.c comment.
There was a problem hiding this comment.
No further findings — the two earlier rounds (counter-reset on full success, classifier dedup, stale header comment) are all addressed in 1e38d8e / 7ab3ec2. Deferring to a maintainer since this touches the write-error path shared by every TCP/TLS socket; worth a human eye on the close-from-writable-dispatch shape.
What was reviewed: errno classification in us_internal_classify_send_error / us_internal_on_short_write and its five call sites; the fatal_send_errno latch-then-close ordering in loop.c (runs after the writable handler, so one bounded retry then close — no spin); fatal_send_errno = 0 initialization at all four socket-creation sites; struct layout (byte in the existing pad gap, no growth); the send_errno > 2 clamp matches the existing poll-error branch's CloseCode-collision guard.
Extended reasoning...
Overview
Factors send()-errno classification out of us_socket_write_check_error into a shared us_internal_classify_send_error helper and applies it (via us_internal_on_short_write) to all five us_socket_*write* helpers in packages/bun-usockets/src/socket.c. Adds a one-byte fatal_send_errno field to struct us_socket_t (in the existing pad-to-pointer gap) and a check in loop.c's writable dispatch that closes the socket with that errno. Initializes the field at the four socket-creation sites (accept, connect, listen, from_fd). Three new fault-injection tests cover peer-gone errno, sustained unclassified errno, and sporadic errnos that recover.
Security risks
None identified. This is defensive error handling on the send path; no new attack surface. The errno set in us_internal_send_errno_is_peer_gone is unchanged (moved, not modified). No user-controlled input reaches the classification.
Level of scrutiny
High — this is the low-level write path underneath every TCP/TLS socket in Bun (Bun.serve, fetch, node:net/tls/http2, WebSocket, SQL drivers). The behavior change is that sockets whose send() persistently fails now close instead of spinning, which is correct but is a semantic change on a hot shared path. A maintainer should confirm the design choice of checking fatal_send_errno after us_internal_ssl_on_writable/us_dispatch_writable returns (rather than before), and that us_internal_socket_close_raw from inside the writable branch is the right teardown shape here.
Other factors
Two prior review rounds from me were addressed: the missing unclassified_send_failures reset on fully-successful sends (1e38d8e, with a dedicated regression test), the classifier duplication in write_check_error (1e38d8e), and the stale fatal_send_errno field comment (7ab3ec2). CodeRabbit's three test-shape comments were declined with reasoning matching file-local convention and test/CLAUDE.md, and CodeRabbit withdrew them. The author's CI report on 1e38d8e shows the new tests passing on every lane with only pre-existing/flaky failures elsewhere. The fault-injection tests demonstrate fail-before/pass-after and cover the counter-reset regression the first review round caught.
Jarred-Sumner
left a comment
There was a problem hiding this comment.
This seems like the wrong place to put it? Do we really need to store this?
|
The constraint is that the fatal errno surfaces from inside the write helpers, and one of The byte sits in the existing pad-to-pointer gap before If the objection is to storing the errno value at all, I can switch to a single bit in the spare half of the |
us_socket_t had grown four independent "do X later" fields — ssl_shutdown_after_spill, ssl_close_after_spill, ssl_pending_detach plus ssl_pending_close_code, and fatal_send_errno. They are mutually exclusive by construction (a close outranks a shutdown, and a close that must wait for the SSL call on the stack to unwind outranks all), so they collapse into one 3-bit pending_action enum ordered by priority plus a single payload byte, latched through us_internal_socket_set_pending which never downgrades a parked close. Impossible combinations (shutdown && close both latched, a close code with nothing pending) are now unrepresentable. Also drop ssl_is_server: written once in ssl_attach and read nowhere; the role lives in BoringSSL (SSL_set_accept_state / connect_state) and in the Rust TLS_SERVER_ROLE flag.
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 `@packages/bun-usockets/src/internal/internal.h`:
- Around line 334-340: Update us_internal_socket_set_pending so an equal-action
relatch with code zero does not overwrite an existing nonzero pending_code;
continue allowing higher actions to replace both fields and preserve normal
updates when the new code is nonzero. This must retain FAST_SHUTDOWN/RESET
payloads when DETACH(0) is later re-latched.
🪄 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: 773d8c18-f7ed-4fd4-b310-901dbed19875
📒 Files selected for processing (6)
packages/bun-usockets/src/context.cpackages/bun-usockets/src/crypto/openssl.cpackages/bun-usockets/src/internal/internal.hpackages/bun-usockets/src/loop.cpackages/bun-usockets/src/socket.csrc/runtime/socket/socket_body.rs
us_internal_socket_set_pending with action == pending_action overwrote pending_code unconditionally. The SNI-abort path (openssl.c:2267, :2452) re-latches DETACH(0) after a JS callback may already have parked DETACH(RESET) via us_internal_socket_close_raw, which dropped the close code. Equal-action re-latch now only writes the code when the new code is nonzero or no code was parked. Drops the inline workaround at us_internal_ssl_detach that did the same thing manually. Fixes the tls.Server destroySoon test that 32441db regressed.
The field was folded into pending_action in 32441db.
There was a problem hiding this comment.
No bugs found, but this reworks the usockets deferred-close state machine (folding four ssl_* latches into a priority-ordered pending_action enum across openssl.c/socket.c/loop.c) on top of the send-errno fix — worth a human look at the ordering/supersede semantics before merge.
What was reviewed:
- errno classification factored into
us_internal_on_short_writeand applied to all five write helpers;us_socket_write_check_errornow shares the same classifier on POSIX pending_actioninit at every socket-creation site (accept, connect, listen, from_fd);ssl_attachdeliberately does not reset itus_internal_socket_set_pendingsupersede rule and the DETACH(0)-after-DETACH(code) case fixed in d3dd45e- removed
ssl_is_server/ssl_*_after_spill/ssl_pending_*have no remaining references
Extended reasoning...
Overview
The PR fixes #24845 (100% CPU spin when send() on a TLS socket keeps returning a fatal errno while the kernel still reports the fd writable) by extending the existing errno classification from us_socket_write_check_error to every us_socket_*write* helper. On top of that, commit 32441db folds four separate SSL deferred-action bitfields (ssl_shutdown_after_spill, ssl_close_after_spill, ssl_pending_detach, ssl_pending_close_code) plus the new fatal-errno latch into a single enum us_socket_pending_action + pending_code byte on struct us_socket_t, with a priority-ordered us_internal_socket_set_pending() helper (higher action supersedes; equal action preserves a nonzero code). Touches internal.h, socket.c, loop.c, context.c, openssl.c, plus a comment in socket_body.rs and three new fault-injection tests.
Security risks
None identified. This is send-error handling on already-open sockets; no auth/crypto/parsing of untrusted input is affected. The errno set classified as peer-gone matches libuv's, and the bounded-retry window for unclassified errnos is unchanged from the pre-existing us_socket_write_check_error behavior.
Level of scrutiny
High. usockets is the C foundation under every TCP/TLS/HTTP/WebSocket connection in Bun. The core errno-classification fix is a mechanical extension of existing code, but the accompanying pending_action refactor is a state-machine redesign: it collapses four independent flags into one totally-ordered enum whose supersede semantics already produced one regression during this PR (32441db broke tls.Server socket destroySoon, fixed in d3dd45e). The interleaving cases — e.g. a CLOSE_RAW latched by a fatal send while DETACH is already parked (lower is ignored, DETACH's us_socket_close covers it), or SHUTDOWN parked then CLOSE arrives (CLOSE supersedes, which is correct) — look right to me, but a maintainer should sign off on the ordering and on us_internal_ssl_attach no longer resetting the deferred-close latch.
Other factors
All prior inline findings from earlier runs (stale comment text, contradicting re-arm wording) were addressed and the threads resolved. CodeRabbit's findings were addressed or withdrawn. CI on the earlier revision passed the new tests on every lane. The removed ssl_is_server bitfield has no remaining references. No human has reviewed yet; the author's own comment at 00:00Z offered an alternative single-bit design, and the enum approach was chosen without explicit maintainer input — that design choice is the main thing I'd want a human to confirm.
There was a problem hiding this comment.
No bugs found this pass, but this touches the core usockets write path and TLS state machine and folds four independent deferred-close bits into a single precedence-ordered enum — worth a human look before merge.
What was reviewed:
- errno classification in
us_internal_on_short_writeand its call sites across all five write helpers;unclassified_send_failuresreset on full-success sends (fixed in 1e38d8e) pending_actionprecedence vs the old four-flag semantics: CLOSE_RAW correctly outranks a parked SHUTDOWN/CLOSE when the spill-drain send goes fatal; DETACH still wins over CLOSE_RAW so ssl_in_use deferral is preservedus_internal_socket_set_pendingequal-action re-latch keeps a nonzero code (d3dd45e);ssl_is_serverremoval is dead-code cleanup (write-only field, no remaining readers)- init sites for the new fields cover accept/connect/listen/from_fd;
ssl_attachdeliberately does not resetpending_action(comment explains adopt_tls case)
Extended reasoning...
Overview
Fixes #24845 (100% CPU spin when send() on a TLS socket keeps returning a non-EAGAIN errno on an fd the kernel still reports writable). Two intertwined changes across packages/bun-usockets/:
-
Errno classification for all write helpers. Factors the existing bounded-retry logic from
us_socket_write_check_errorintous_internal_classify_send_error()+us_internal_on_short_write(), and applies it tous_socket_write,us_socket_raw_write,us_socket_raw_writev,us_socket_write2, andus_socket_ipc_write_fd. A peer-gone errno or an unclassified errno past the 32-retry ceiling now parksUS_PENDING_ACTION_CLOSE_RAW;loop.c's writable dispatch closes the socket with that errno on the next event instead of retrying forever. -
Deferred-close state refactor. Folds four separate
us_socket_tbitfields (ssl_shutdown_after_spill,ssl_close_after_spill,ssl_pending_detach,ssl_pending_close_code) into onepending_action : 3enum +pending_codebyte, with a precedence rule (us_internal_socket_set_pending: higher supersedes; equal keeps a nonzero code). The newCLOSE_RAWaction slots into this. Also drops the write-onlyssl_is_serverbit.
Plus: field init at all four socket-creation sites, a one-line comment update in socket_body.rs, and three new fault-injection tests in socket-syscall-fault.test.ts.
Security risks
None identified. No new user-facing surface, no parsing of untrusted input; the change tightens error handling on an internal write path. The errno is truncated to unsigned char before storage, and loop.c clamps values ≤2 to ECONNRESET so they can't collide with the libus close-code enum.
Level of scrutiny
High. This is hot-path C in the core networking layer — every TLS and plain-TCP write in Bun routes through the touched helpers, and loop.c's writable dispatch runs on every writable event. The four-bitfield → single-enum refactor is a semantic transformation whose correctness depends on the precedence ordering matching every combination the old independent flags allowed. One iteration of this PR (32441db) already regressed tls.Server socket destroySoon until d3dd45e fixed the equal-action re-latch rule, which demonstrates the interaction space is subtle. There's also a design question the author explicitly left open for a maintainer (whether to store the errno byte vs. a single bit + SO_ERROR fallback) that was resolved by the enum refactor without visible maintainer sign-off.
Other factors
- All prior inline findings (mine and CodeRabbit's) are addressed and resolved; the current head (9dc972c) is a comment-only fix on top of d3dd45e.
- CI on 1e38d8e was green for the new tests on all lanes per the robobun report; the enum refactor landed after that report, so a fresh CI run on the head commit should be confirmed.
- Minor behavior alignment I did not flag as a bug:
us_socket_write2now setslast_write_failedon a short write (previously only re-armed) — this matches every other write helper and looks intentional. - Grepped for stragglers: no remaining references to the four removed field names or
ssl_is_serveranywhere in the tree.
…ge https response after peer FIN (#35109) ### Problem ```js import https from 'node:https'; const server = https.createServer({ key, cert }, (req, res) => { res.writeHead(200, { 'content-length': String(8 * 1024 * 1024) }); res.write(Buffer.alloc(8 * 1024 * 1024, 'a')); res.end(); }); // raw tls client: socket.end('GET / HTTP/1.1\\r\\nHost: x\\r\\n\\r\\n') // Node: body bytes 8388608 // Bun: body bytes 2637824 (truncated at the first kernel-accepted batch) ``` A node:https server responding with a body that backpressures truncates at the first kernel-accepted write when the client half-closes right after its request. Same for `res.end(bigBuffer)`. Node.js delivers the full body. ### Cause Two server-side layers and one Windows-only client-side layer: * **TLS spill not counted at the close gates**: `us_internal_ssl_write()` seals plaintext into 16 KB TLS records and flushes them to the kernel in ~128 KB batches. A partial kernel write parks the remainder of the batch in the loop's `ssl_spill` slot and returns the full plaintext count as written, so `AsyncSocket::getBufferedAmount()` (which reads only `AsyncSocketData::buffer`) reports 0 while up to one batch of ciphertext is still in userspace. The `shouldCloseConnection()` close gates in `HttpResponse::internalEnd` / `HttpResponse::cork` / `HttpContext::onData` / `HttpContext::onWritable` key on `getBufferedAmount() == 0` and so fire early; `us_internal_ssl_close(code=0)` does one best-effort drain and frees the rest. * **`allow_half_open` gated on `!SSL` for node:http**: `HttpContext::onOpen` only set `allow_half_open` for non-TLS `IsNodeHttp` sockets, so a client FIN on a node:https connection made `us_internal_ssl_on_end` force-close the socket right after dispatching `onEnd`, discarding the buffered response. `onEnd<IsNodeHttp>`'s existing defer was never reached. #35034 fixed the plain-TCP case; the TLS side was left gated out because the spill made the close gates unsafe. * **Windows eof-drain** (surfaced by the new test's client side): `poll_cb` (libuv.c) maps AFD `UV_DISCONNECT` to the eof hint for a socket whose write side we already shut down. AFD reports DISCONNECT while the tail of the peer's stream is still queued in the kernel, but the Windows branch of `loop.c`'s read loop only did one extra `recv()` (the RST probe) before falling through to the `is_shut_down` raw-close, discarding the rest. Reproduces on released bun against a Node.js server: a half-closed `net.Socket`/`tls` client intermittently loses the end of a large response on Windows only. ### Fix * **`us_socket_ssl_spill_pending()`** (openssl.c / socket.c / libusockets.h): ciphertext bytes already sealed for this socket and reported as written by `us_socket_write()`, still waiting on a writable event. Returns 0 for plain-TCP sockets. * **`AsyncSocket::hasFullyDrained()`**: `buffer.length() == 0 && spill_pending == 0`. The HTTP close-after-drain gates (`HttpResponse::internalEnd` / `cork`, `HttpContext::onData` tail / `onWritable` / `onEnd<IsNodeHttp>`) switch from `getBufferedAmount() == 0` to this. `getBufferedAmount()` itself is unchanged so WebSocket's `maxBackpressure` policy and the JS-exposed `bufferedAmount` stay a plaintext count. The spill is bounded (≤ one 128 KB batch) and `us_internal_ssl_on_writable` drains it before dispatching the user-level writable, so the existing drain loops terminate: once `AsyncSocketData::buffer` empties, the next writable event drains the final spill and the close gate fires with nothing pending. * **`HttpContext::onOpen<IsNodeHttp>`**: drop the `!SSL` guard on `allow_half_open`. `us_internal_ssl_on_end` already honours the flag; `onEnd<IsNodeHttp>`'s `hasQueuedOutgoing` now accounts for the spill, and `onWritable`'s zero-progress-after-FIN close is not gated on `!SSL`. * **`us_internal_ssl_on_writable`**: release a zero-progress spill once the peer's readable side has ended, so a FIN-then-RST client does not wedge the writable dispatch before the close gate is reached (the drain would otherwise re-arm writable on a send() that keeps failing). #34510 is the general fix for stuck TLS sends; this is the narrow case the new `allow_half_open` path opens. * **`loop.c` Windows read loop**: drain on the eof hint like the POSIX branch already does (`recv()` returning 0 or `WSAEWOULDBLOCK` ends the loop, bounded by the kernel receive buffer). ### Relation to #35088 \#35088 is the `Bun.serve` (`!IsNodeHttp`) sibling and is currently gated on `!SSL` because the spill was invisible to its `onEnd` defer's `doneButBuffered` check and to `internalEnd`'s close gate (its scope note says so). With `hasFullyDrained()` at those gates that PR's `onEnd` defer is accurate for TLS too, so it can drop its `!SSL` gates (switching its `getBufferedAmount() > 0` to `!hasFullyDrained()`). ### Verification New `describe('https')` in `test/js/node/http/node-http-backpressure.test.ts` mirrors the existing plain-HTTP half-close tests over TLS for `res.write()+res.end()`, `res.end(payload)` (the `optional=false` `internalEnd` buffer path), and `httpAllowHalfOpen` with `res.end()` after drain; each receives ~2.6 MB on main and the full 8 MiB with the fix, matching Node.js. Looped 5× per case so the on_writable drain cycle is exercised past the first kernel-accepted write; the loop also covers the Windows client-side eof-drain. A fourth test half-closes then destroys the client after first data and asserts the server-side socket `'close'` fires (would wedge on a stuck spill). Verified on linux-x64 (14/14) and windows-x64 (14/14, 5 consecutive runs of the new tests). `node-http-backpressure.test.ts`, `node-http-pinned-write.test.ts`, `node-http-server-socket-end-drain.test.ts`, `bun-serve-ssl.test.ts`, `node-tls-connect.test.ts`, `node-https-checkServerIdentity.test.ts`, `serve.test.ts`, `socket.test.ts` pass (pre-existing container-only / debug-timeout failures unchanged from main). <!-- robobun:evidence:begin --> --- **[review]** gate passed · iteration 1 · 9 files touched <details><summary>fails on main (without fix)</summary> ```console ASAN without fix: 3 FAILED $ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/http/node-http-backpressure.test.ts bun test v1.4.0 (41c2fbb) test/js/node/http/node-http-backpressure.test.ts: (pass) backpressure > should handle backpressure [588.62ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the client requested the close [400.31ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the server sets Connection: close on a keep-alive request [190.66ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the whole body is passed to res.end() [216.37ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() then res.end() [179.05ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() without res.end() [136.15ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still ... (truncated) release without fix: all passed bun test v1.4.0-canary.1 (3d1c56b) test/js/node/http/node-http-backpressure.test.ts: (pass) backpressure > should handle backpressure [32.49ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the client requested the close [22.78ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the server sets Connection: close on a keep-alive request [18.46ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the whole body is passed to res.end() [11.04ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() then res.end() [9.60ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() without res.end() [7.83ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() then res.end() after drain, httpAllowHalfOpen [6.33ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still ... (truncated) ``` </details> <details><summary>passes on PR (with fix)</summary> ```console ASAN with fix: all passed $ BUN_DEBUG_QUIET_LOGS=1 bun scripts/build.ts --profile=debug --quiet test "--reporter=junit" "--reporter-outfile=/tmp/mechgate.xml" test/js/node/http/node-http-backpressure.test.ts bun test v1.4.0 (41c2fbb) test/js/node/http/node-http-backpressure.test.ts: (pass) backpressure > should handle backpressure [517.49ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the client requested the close [423.69ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the server sets Connection: close on a keep-alive request [187.06ms] (pass) backpressure > Connection: close does not truncate a response that is still flushing > when the whole body is passed to res.end() [172.00ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() then res.end() [215.92ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still flushing > res.write() without res.end() [158.36ms] (pass) backpressure > a client FIN right after the request does not truncate a response that is still ... (truncated) release with fix: all passed $ bun scripts/build.ts --profile=release [configured] bun-profile → bun (stripped) in 738ms (unchanged) ninja: Entering directory `/workspace/bun/build/release' [0/25] cargo bun_bin → libbun_rust.a (--target x86_64-unknown-linux-gnu) nightly-2026-07-20-x86_64-unknown-linux-gnu unchanged - rustc 1.99.0-nightly (9f36de775 2026-07-19) �[1m�[92m Compiling�[0m bun_core v0.0.0 (/workspace/bun/src/bun_core) �[1m�[92m Compiling�[0m bun_errno v0.0.0 (/workspace/bun/src/errno) �[1m�[92m Compiling�[0m bun_ptr v0.0.0 (/workspace/bun/src/ptr) �[1m�[92m Compiling�[0m bun_boringssl_sys v0.0.0 (/workspace/bun/src/boringssl_sys) �[1m�[92m Compiling�[0m bun_safety v0.0.0 (/workspace/bun/src/safety) �[1m�[92m Compiling�[0m bun_zlib_sys v0.0.0 (/workspace/bun/src/zlib_sys) �[1m�[92m Compiling�[0m bun_cares_sys v0.0.0 (/workspace/bun/src/cares_sys) �[1m�[92m Compiling�[0m bun_zstd v0.0.0 (/workspace/bun/src/zstd) �[1m�[92m Compiling�[0m bun_picohttp v0.0.0 (/workspace/bun/src/picohttp) �[1m�[92m Compiling�[0m bun_output v0.0.0 (/workspace/bun/src/output) �[1m�[92m Compiling�[0m bun_clap v0.0.0 (/workspace/bun/src/clap) �[1m�[92m Compiling�[0m bun_valkey v ... (truncated) ``` </details> <details><summary>diff hotspot</summary> ``` packages/bun-usockets/src/crypto/openssl.c | 27 +++++++ packages/bun-usockets/src/internal/internal.h | 1 + packages/bun-usockets/src/libusockets.h | 5 ++ packages/bun-usockets/src/loop.c | 13 ++++ packages/bun-usockets/src/socket.c | 7 ++ packages/bun-uws/src/AsyncSocket.h | 15 ++++ packages/bun-uws/src/HttpContext.h | 21 +++--- packages/bun-uws/src/HttpResponse.h | 6 +- test/js/node/http/node-http-backpressure.test.ts | 95 ++++++++++++++++++++++++ 9 files changed, 176 insertions(+), 14 deletions(-) ``` </details> **gate history** · 3 passed · 0 rejected · iteration 1 <details><summary>evidence per changed file</summary> ``` file reads edits tests packages/bun-usockets/src/crypto/openssl.c 9 4 0 packages/bun-usockets/src/internal/internal.h 1 1 0 packages/bun-usockets/src/libusockets.h 1 1 0 packages/bun-usockets/src/loop.c 4 1 0 packages/bun-usockets/src/socket.c 6 1 0 packages/bun-uws/src/AsyncSocket.h 5 3 0 packages/bun-uws/src/HttpContext.h 4 7 0 packages/bun-uws/src/HttpResponse.h 5 3 0 test/js/node/http/node-http-backpressure.test.ts 4 8 0 ``` </details> <!-- robobun:evidence:end -->
…3957) Follow-up to #43947, which is merged. Rebased on main. ### Problem - After `shutdown()` or `end()` during a handshake the socket still seals handshake records. `us_socket_raw_write` sends nothing after a FIN, so `ssl_flush_write_batch` (`packages/bun-usockets/src/crypto/openssl.c`) parks them as a spill that can never drain. - `us_internal_ssl_close` waits for that spill, so the socket never closes. The spill holds the loop's one spill slot, so the next half-closed handshake never finishes. - No report is linked. 1.4.2 reaches it through `socket.shutdown()`, node:tls since #42181 (not released). ### Fix - `ssl_flush_write_batch` and the unbatched path of `BIO_s_custom_write` drop records that the socket can never send. - One predicate, `us_internal_socket_can_raw_write`, gates the raw writes, the raw shutdown and both drops. - Correct because these records can never reach the peer. Node completes the same handshakes. - Verified: 12 new tests, 10 fail without this change when run alone. Node v26.3.0 passes 34 of 34. Self-reviewed: 16 concerns raised, 14 addressed. ### Background - A spill is the part of a batch that the kernel did not take. The loop has one spill slot. - While the slot is taken, every socket writes record by record. - Considered a close that ignores the spill, as #43946 does for a forceful close. The slot stays taken until the close. ### Downsides - A slow reader still takes the spill slot. This PR removes only the owner that never lets go. - The peer never gets the dropped records. Nor does it on the base or on Node. - Release `.text`: 58154908 to 58153884 B. Each flush costs 1 more direct call. <details><summary>Notes</summary> **Cases, each test run alone, on the head of #43947 before its merge (771b423) and with this change** Each socket calls `shutdown()` or `end()` while its handshake runs. The peer keeps the connection open. | Case | #43947 | This PR | |---|---|---| | `Bun.listen`, `requestCert`, `rejectUnauthorized: true`, TLS 1.2, untrusted client | `handshake(true, authorized=false, error)`, the socket never closes | same report, then close | | Same, trusted client (control) | stays open until the client ends the connection, then both close | same | | Both rows, while another socket waits for a slow reader | the handshake never reports | same as the two rows above | | Two `Bun.connect` clients, TLS 1.3 | `close` never fires | both report the handshake, both close | | `Bun.connect` client, TLS 1.2, ended after the server's first flight | `close` never fires | `handshake(false)`, close | | `Bun.connect` client that writes to a second TLS socket from its handshake callback | `close` never fires | the write arrives, close | | Two `tls.connect` clients, the first (TLS 1.3 or TLS 1.2) stays open | the second never emits `secureConnect` | it emits it, as on Node v26.3.0 | | `tls.createServer`, TLS 1.2, `end()` in the handshake, untrusted client (control) | both sockets close | same | | Four `tls.connect` sockets after one such handshake in the same process | flight and first write leave in 2 segments | 1 segment | Run in one process, more tests fail on #43947 than the table shows: a test that times out leaves the socket that holds the spill slot. That is also the only way in which `TLS 1.3 client sees 'end', not ECONNRESET, when the server tears down right after rejecting its certificate (#40653)` failed. No CI run has shown the lost batching. The two-client probe gives the same result on the released 1.4.2 (`1.4.2+744846f84`): the second client never reports its handshake. **Mechanism** - With batching the handshake records go into the batch, and `ssl_flush_write_batch` sends them. After a FIN `us_socket_raw_write` returns 0. The flush parked all of it as a spill and took the spill slot. A spill drains from the writable event, and a socket that sent its FIN gets none. - `us_internal_ssl_close` defers a close with code 0 or 2 while the socket has a spill. - Batching needs a free slot (`hs_batching` in `us_internal_ssl_on_data`, `batching` in `us_internal_ssl_write`). With the slot taken, `BIO_s_custom_write` writes each record at once. After a FIN that write returns 0, the BIO asks for a retry, and `SSL_do_handshake` answers `SSL_ERROR_WANT_WRITE` until the socket closes. - Three places turn a short raw write into a wait for a writable event: the unbatched write, the flush, and `ssl_drain_spill`. The first two now drop. `ssl_drain_spill` has no guard on purpose: `us_internal_ssl_shutdown` defers the FIN behind a spill, and a close releases the spill, so a socket that sent its FIN has no spill to drain. **Why a drop is correct** - SSL counts a sealed record as written. It cannot be sent again, and after the FIN it cannot be sent at all. - The base already reports a completed handshake for the first half-closed socket on a loop: the batch takes the records and the report runs before the flush. The drop makes the second socket, and a socket next to a slow reader, behave like the first. - `us_internal_ssl_write` refuses application data after a FIN, and `ssl_handle_shutdown` sends no close_notify after one. So only handshake records and alerts reach the new branches. **Not in this PR** - A slow reader takes the spill slot like before. The slot is state of the loop, and a spill per connection is a different design. - A close on an open socket whose peer never reads still waits for its spill. That spill can drain. - A `send` that the kernel refuses with `ECONNRESET` or `EPIPE` still parks a spill. That needs the error of the raw write (#42336, #34510), not the state of the socket. - An `allowHalfOpen` socket that shut down during its handshake stays open after the peer's close_notify until the peer's FIN (since 1.4.1, #40384). Main does the same, and it does not reach the new branches. - #43946 changes what a forceful close does with a batch that is still held. It does not cover records that are sealed after a FIN. - `end()` from the `'connect'` listener sends the FIN before the ClientHello. Node sends the ClientHello first. **Self-review** Rejected, with the reason: - Move the tests that fail by a timeout into child processes. With this change no test times out. `afterAll` now releases what a timed-out test left, so the tests after the block are not affected. - A test for the case with no relay. The peer has to keep the connection open after our FIN, and a relay is how a test decides that. **Measurements** Call counts are gdb breakpoint hit counts on debug builds with the debug info stripped. Sizes are from release builds of both trees. - Healthy handshake, 100 in-process `Bun.listen` / `Bun.connect` connections: `SSL_do_handshake` 600 and 600, `BIO_s_custom_write` 700 and 700, `ssl_flush_write_batch` 300 and 300, `bsd_send` 600 and 600, `bsd_recv` 600 and 600, `us_internal_socket_close_raw` 200 and 200. - Both new branches are behind a write that the wire did not take in full. A socket that can write pays one more compare there, and nothing on a full write. - Release `.text`: 58154908 to 58153884 B (`size -A`). Stripped `bun`: 80844320 B both. `ssl_flush_write_batch` grows from 214 to 254 B and is no longer inlined into its callers: `BIO_s_custom_write` 683 to 512 B, `us_internal_ssl_close` 1195 to 999 B, `us_internal_ssl_on_data` 2651 to 2240 B, `us_internal_ssl_shutdown` 608 to 412 B. So each flush is one direct call more in a release build, 3 per healthy connection pair. - The shared predicate changes no size: `us_socket_raw_write` 178 B, `us_socket_raw_writev` 356 B and `us_internal_socket_raw_shutdown` 85 B before and after. - Allocations: the change adds none, and a dropped flight saves the `us_malloc` of its spill. **Tests** - `test/js/bun/net/socket.test.ts`: 8 new tests, 17 with "while the handshake runs" in the name. Run alone on #43947, 7 of the 8 fail, and the trusted TLS 1.2 client is the control. - `test/js/node/tls/node-tls-duplex-end-verify.test.ts`: 3 new tests. Run alone on #43947, 2 fail, and the TLS 1.2 server is the control. Node v26.3.0 passes 34 of 34 (`node --test`). - `test/js/node/tls/node-tls-connect.test.ts`: 1 new test, the one-segment flight. It fails on #43947 with 3 chunks for each connection. - The block was run 5 times on this change: 5 passes. - `test/js/node/tls/` and `test/js/bun/net/` together (728 tests): no failure is only on this change. Failures on the base too: `SNICallback runs even when the requested servername matches the bind hostname`, `should not call drain before handshake`, and tests that wait for a garbage collection. - After the rebase on main (ecf3490, with #43947 merged) the two directories have 802 tests, and 6 fail: 4 tests that wait for a garbage collection or for workers, a cluster test, and `should not call drain before handshake`. Each of them fails on main on the test machine too. Before that rebase, on e375701, the two directories had 765 tests. 6 fail, the same 6 as on #43947 at that base. In one process, #43947 at that base fails 8 of the 17 tests with "while the handshake runs" in the name, and the one-segment test. - The vendored `test-tls-*`, `test-https-*`, `test-http2-*` and `test-net-*` files: 660 pass, and the 3 that fail do so on #43924 too. This is a check for regressions only: no vendored file changes its result with this PR. </details> --------- Co-authored-by: Dylan Conway <dylan.conway567@gmail.com>
Fixes #24845
What does this PR do?
Problem
us_socket_raw_write(andus_socket_write,us_socket_raw_writev,us_socket_write2,us_socket_ipc_write_fd) treated any-1frombsd_sendas a partial write: setlast_write_failed, re-arm the writable poll, return 0. No errno classification. The SSL ciphertext spill path routes throughus_socket_raw_write:so a
send()that keeps returning an errno on a socket the kernel still reports writable pins a core at 100%.Observed on macOS as idle long-lived processes (holding a TLS keepalive connection) simultaneously spinning in
us_internal_ssl_on_writable -> __sendto, with the kernel logging (sosend -> _os_log_internal) inside every send call. That is consistent with a Network Extension content filter returning an errno on a flow it has not closed:send()fails,SS_CANTSENDMOREis never set, so noEV_EOFarrives to end it.The plain-TCP node:net write path was already bounded because it uses
us_socket_write_check_error, which has the classification this PR extends to the other helpers.Reproduction
Fault-injection repro (Linux and macOS, debug build): arm
sendto return-1 EPIPEforever on a TLS client fd after the handshake, issue one write.macOS
sampleof the spin (ciabatta, debug build)Process at 128-131% CPU; a bounded run of 500000 injected failures was silently retried in 2.7s and the data delivered once the fault disarmed; the socket never errored or closed.
Fix
Factor the errno classification from
us_socket_write_check_errorintous_internal_on_short_write()and call it from everyus_socket_*write*helper. A peer-gone errno (EPIPE,ECONNRESET,ENOTCONN,ETIMEDOUT, ...), or an unclassified errno that persists past the existing bounded retry window, latches a pending close on the socket; the writable dispatch inloop.ccloses the socket with that errno after the handler returns. Would-block, transientENOBUFS/ENOMEM, and bounded unclassified errnos still re-arm as before.The deferred close is carried by the existing pending-action mechanism (previously four separate
ssl_shutdown_after_spill/ssl_close_after_spill/ssl_pending_detach/ssl_pending_close_codefields, folded here into onepending_actionenum +pending_codebyte), sostruct us_socket_tdoes not grow.Relation to #34487 / #34498
Same code neighborhood (socket's writable poll never settling) but a different bug: those address half-open TLS shutdown / Windows level-triggered DISCONNECT re-arm; neither touches send-errno handling in the raw write helpers.
How did you verify your code works?
New tests in
test/js/bun/net/socket-syscall-fault.test.tsinject a sustainedEPIPE, a sustained unclassifiedEINVAL, and 50 sporadic one-offEPROTOTYPEs on a TLS client fd after the handshake and assert the socket closes with the errno for the sustained cases (and stays open for the sporadic-with-recovery case).Ran
test/js/node/tls/+test/js/bun/net/with the fix vs without; the delta is exactly the new tests, no regressions. Also passing: the existing h2/WS/Bun.serve fault-injection suites.Possibly related: #32600 (plain HTTP on Linux, profiler points at JS microtask churn rather than the C-level writable spin; not claiming it here).
no test proof · iteration 3 · Platform-specific test(s) that do not run on this machine. Deferring to CI, which covers all platforms: test/js/bun/net/socket-syscall-fault.test.ts