Repository navigation
Add second cork buffer to uWebSockets for nested async handlers - #28615
Conversation
When an awaited HTTP handler resumes while another request holds the single cork buffer, the resumed request's writeHead+end were forced down the uncorked path (one syscall per write). With two independent cork slots, both the in-flight request and the resumed request can batch their writes. Slots are borrowed on cork(), stolen if empty (offset==0), and the LRU slot is force-flushed only when both hold data. Close paths clear slots to avoid dangling pointers in the drain loop.
|
Updated 11:48 PM PT - Mar 27th, 2026
❌ @Jarred-Sumner, your commit 31d9aaa has 3 failures in
🧪 To try this PR locally: bunx bun-pr 28615That installs a local version of the PR into your bun-28615 --bun |
|
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:
WalkthroughReplaced single per-loop cork buffer with two LRU-managed cork slots and refactored corking/write/uncork flows to use per-slot buffers; updated loop draining, HTTP/WebSocket flows, native bindings, and tests to use the new slot APIs and semantics. Changes
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/bun-uws/src/HttpResponse.h (1)
637-651:⚠️ Potential issue | 🟠 Major
HttpResponse::cork()still blocks the LRU fallback.Line 638 keeps the old
Super::canCork()gate, so this path skipsSuper::cork()exactly when both slots hold data. That leaves theHttpResponse::cork()/Bun.serveentry point on the uncorked slow path under contention.Suggested fix
- if (!Super::isCorked() && Super::canCork()) { + if (!Super::isCorked()) { LoopData *loopData = Super::getLoopData(); Super::cork();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/bun-uws/src/HttpResponse.h` around lines 637 - 651, The current HttpResponse::cork() condition uses both !Super::isCorked() and Super::canCork(), which lets the LRU fallback path skip corking when both slots hold data; remove Super::canCork() from the guard so corking is attempted whenever the response isn't already corked (i.e., change the if to only check !Super::isCorked()), then call Super::cork() as before and keep the LoopData::findCorkSlot/LoopData::getCorkSlot/INVALID_CORK_SLOT logic and upgrade-safe newCorkedSocket handling unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/bun.js/bindings/NodeHTTP.cpp`:
- Around line 600-603: The current guard prevents calling
AsyncSocket<isSSL>::cork() when canCork() is false, blocking the new
force-uncork progress path; remove the canCork() check so that in the block
where headersObjectValue.getObject() is used you only check that
response->uWS::template AsyncSocket<isSSL>::isCorked() is false and
response->getBufferedAmount() == 0 before calling response->uWS::template
AsyncSocket<isSSL>::cork(); this lets cork() perform its force-uncork/LRU
behavior under contention while keeping the existing isCorked and bufferedAmount
safeguards.
In `@test/js/node/http/node-http-nested-cork.test.ts`:
- Around line 65-95: The test starts a server with Bun.serve(...) and calls
server.stop(true) at the end, but if a request throws early the stop call may
never run; change the server lifetime to use a using/await using pattern so the
server is automatically disposed; specifically replace the ad-hoc creation of
server via Bun.serve(...) with an await using (or using) binding for the server
returned by Bun.serve so that when the test scope exits (even on error) the
server is stopped instead of relying on the explicit server.stop(true) call.
---
Outside diff comments:
In `@packages/bun-uws/src/HttpResponse.h`:
- Around line 637-651: The current HttpResponse::cork() condition uses both
!Super::isCorked() and Super::canCork(), which lets the LRU fallback path skip
corking when both slots hold data; remove Super::canCork() from the guard so
corking is attempted whenever the response isn't already corked (i.e., change
the if to only check !Super::isCorked()), then call Super::cork() as before and
keep the LoopData::findCorkSlot/LoopData::getCorkSlot/INVALID_CORK_SLOT logic
and upgrade-safe newCorkedSocket handling unchanged.
🪄 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: a271c957-afcc-4316-ac7d-03f4c87ba5cf
📒 Files selected for processing (8)
packages/bun-uws/src/AsyncSocket.hpackages/bun-uws/src/HttpResponse.hpackages/bun-uws/src/Loop.hpackages/bun-uws/src/LoopData.hpackages/bun-uws/src/WebSocket.hsrc/bun.js/bindings/NodeHTTP.cppsrc/deps/libuwsockets.cpptest/js/node/http/node-http-nested-cork.test.ts
| const server = Bun.serve({ | ||
| port: 0, | ||
| async fetch(req) { | ||
| count++; | ||
| const url = new URL(req.url); | ||
| if (pending) { | ||
| const prev = pending; | ||
| pending = Promise.withResolvers(); | ||
| prev.resolve(); | ||
| } else { | ||
| pending = Promise.withResolvers(); | ||
| } | ||
| if (count === 20) pending.resolve(); | ||
| await pending.promise; | ||
| return new Response("ok:" + url.pathname, { | ||
| headers: { "x-path": url.pathname }, | ||
| }); | ||
| }, | ||
| }); | ||
|
|
||
| const responses = await Promise.all( | ||
| Array.from({ length: 20 }, (_, i) => | ||
| fetch("http://localhost:" + server.port + "/" + i).then(async r => ({ | ||
| body: await r.text(), | ||
| header: r.headers.get("x-path"), | ||
| })) | ||
| ) | ||
| ); | ||
| console.log(JSON.stringify(responses.sort((a,b) => a.body.localeCompare(b.body)))); | ||
| server.stop(true); | ||
| `, |
There was a problem hiding this comment.
🛠️ Refactor suggestion | 🟠 Major
Use using for the child Bun.serve() lifetime.
If one of the requests throws before Line 94, the child process never reaches server.stop(true) and can hang with the server still open. using server = Bun.serve(...) makes cleanup deterministic here.
As per coding guidelines, Use \await using` or `using` to ensure proper resource cleanup for APIs like Bun.listen, Bun.connect, Bun.spawn, Bun.serve, etc.`
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@test/js/node/http/node-http-nested-cork.test.ts` around lines 65 - 95, The
test starts a server with Bun.serve(...) and calls server.stop(true) at the end,
but if a request throws early the stop call may never run; change the server
lifetime to use a using/await using pattern so the server is automatically
disposed; specifically replace the ad-hoc creation of server via Bun.serve(...)
with an await using (or using) binding for the server returned by Bun.serve so
that when the test scope exits (even on error) the server is stopped instead of
relying on the explicit server.stop(true) call.
There was a problem hiding this comment.
This is a well-described fix for a real throughput regression, but it's a non-trivial change to core networking infrastructure that warrants a human look before merging.
Extended reasoning...
Overview
The PR replaces the single shared uWebSockets cork buffer with two independent cork slots (each 16KB), touching 7 C++ files across the low-level HTTP/WebSocket send path: LoopData.h (data model), AsyncSocket.h (cork/uncork/getSendBuffer/write), HttpResponse.h (upgrade path + cork handler), Loop.h and libuwsockets.cpp (drain loops), WebSocket.h (large-message fast path), and NodeHTTP.cpp (writeHead).
Security risks
No direct auth or crypto changes. The UAF protection fix (unborrowCorkSlot called from all close/destroy paths) is a positive security improvement — the old single-slot code left a dangling pointer risk if a socket was freed while corked. The new code explicitly clears both slots on destruction.
Level of scrutiny
High. This is production-critical code that runs on every HTTP and WebSocket send. The LRU eviction path in AsyncSocket::cork() has a subtle edge case: if a depth-3+ nesting causes LRU eviction of the slot captured by HttpResponse::cork() before handler() returns, the post-handler uncork could operate on the wrong slot. The PR description acknowledges depth-3+ is rare, but the correctness argument for that case deserves explicit human verification. The drain loops (preCb, uws_res_clear_corked_socket) both iterate exactly twice, which is bounded correctly for two slots.
Other factors
- The only bug found is a trivial unused import (normalizeBunSnapshot) in the new test file, which has no runtime impact.
- New tests cover the core scenario (20 concurrent interleaved-await requests) for both node:http and Bun.serve.
- Existing test suites (serve.test.ts, node-http.test.ts, websocket-server.test.ts) reportedly pass.
- Memory cost is one additional 16KB buffer per event loop, which is negligible.
The canCork() guards prevented cork() from ever reaching its LRU force-uncork fallback — if both slots were full, we'd skip corking entirely instead of evicting. cork() now always makes progress, so callers just check !isCorked(). Also: use wtf/Assertions.h ASSERT instead of custom UWS_CORK_ASSERT, and only set needsUncork in App.h if cork() actually succeeded.
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/bun-uws/src/App.h`:
- Around line 358-360: The stored mutable lambda uses a persistent needsUncork
flag that isn't reset each iteration, so a previous true value can leak into the
next publish/subscriber call; update the lambda that operates on ws (the
AsyncSocket<SSL> pointer) to initialize/reset needsUncork = false at the start
of each invocation (before checking ws->isCorked()), ensuring cork() is only
considered acquired for the current pass and uncork() is only called when this
invocation actually set the cork; apply the same fix in the other similar lambda
block handling the DROPPED/LAST paths (the blocks around the existing
ws->isCorked() checks and AsyncSocket<SSL>::cork()/uncork() calls).
In `@packages/bun-uws/src/HttpResponse.h`:
- Around line 638-651: The current code uses LoopData::findCorkSlot(…) and
getCorkSlot(ourSlot)->socket after handler() to detect a replacement socket, but
because empty-slot stealing can rebind that slot to another request, this is
unsafe; instead introduce and check an explicit handoff signal on the response
object (e.g. a member like replacementCorkSocket or corkHandoffFlag on the
HttpResponse) that the upgrade path sets when it transfers the cork slot to the
new WebSocket; before calling handler() leave the corked slot associated as now,
call handler(), then read and clear this explicit replacementCorkSocket on this
object (and use that value) rather than using
LoopData::getCorkSlot(ourSlot)->socket or LoopData::findCorkSlot; also update
the upgrade code path to set that member when it takes ownership of the cork
slot.
- Around line 328-339: The cork slot is being queried after internalEnd() has
already uncorked the response, so findCorkSlot(this) returns
LoopData::INVALID_CORK_SLOT and the new WebSocket never inherits the cork; to
fix, capture the cork slot from Super::getLoopData()->findCorkSlot(this) before
calling internalEnd() in upgrade(), then pass that saved corkedSlot into the
code path that calls us_socket_context_adopt_socket(...) and subsequently call
LoopData::transferCorkSlot(savedCorkedSlot, webSocket, SSL) (checking against
LoopData::INVALID_CORK_SLOT) so the adopted WebSocket reacquires the original
cork.
🪄 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: 1ead8643-3ec9-4602-9099-b79e487744ad
📒 Files selected for processing (7)
packages/bun-uws/src/App.hpackages/bun-uws/src/AsyncSocket.hpackages/bun-uws/src/HttpResponse.hpackages/bun-uws/src/LoopData.hpackages/bun-uws/src/WebSocket.hsrc/bun.js/bindings/NodeHTTP.cppsrc/deps/libuwsockets.cpp
| if (!ws->isCorked()) { | ||
| ((AsyncSocket<SSL> *)ws)->cork(); | ||
| needsUncork = true; | ||
| needsUncork = ws->isCorked(); |
There was a problem hiding this comment.
Reset needsUncork for each iterator pass.
needsUncork lives in the stored mutable lambda, so a true from the previous publish/subscriber survives into the next one. When FIRST hits an already-corked socket, this branch never clears the flag, and the DROPPED / LAST path can uncork a slot this callback did not acquire.
🩹 Proposed fix
if (flags & TopicTree<TopicTreeMessage, TopicTreeBigMessage>::IteratorFlags::FIRST) {
+ needsUncork = false;
if (!ws->isCorked()) {
((AsyncSocket<SSL> *)ws)->cork();
needsUncork = ws->isCorked();
}
}
@@
if (needsUncork) {
((AsyncSocket<SSL> *)ws)->uncork();
needsUncork = false;
}
@@
if (needsUncork) {
((AsyncSocket<SSL> *)ws)->uncork();
+ needsUncork = false;
}Also applies to: 366-378
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/bun-uws/src/App.h` around lines 358 - 360, The stored mutable lambda
uses a persistent needsUncork flag that isn't reset each iteration, so a
previous true value can leak into the next publish/subscriber call; update the
lambda that operates on ws (the AsyncSocket<SSL> pointer) to initialize/reset
needsUncork = false at the start of each invocation (before checking
ws->isCorked()), ensuring cork() is only considered acquired for the current
pass and uncork() is only called when this invocation actually set the cork;
apply the same fix in the other similar lambda block handling the DROPPED/LAST
paths (the blocks around the existing ws->isCorked() checks and
AsyncSocket<SSL>::cork()/uncork() calls).
| /* Before we adopt and potentially change socket, check which cork slot | ||
| * we occupy so we can transfer it to the new WebSocket. */ | ||
| LoopData *loopData = Super::getLoopData(); | ||
| int corkedSlot = loopData->findCorkSlot(this); | ||
|
|
||
| /* Adopting a socket invalidates it, do not rely on it directly to carry any data */ | ||
| us_socket_t *usSocket = us_socket_context_adopt_socket(SSL, (us_socket_context_t *) webSocketContext, (us_socket_t *) this, sizeof(HttpResponseData<SSL>), sizeof(WebSocketData) + sizeof(UserData)); | ||
| WebSocket<SSL, true, UserData> *webSocket = (WebSocket<SSL, true, UserData> *) usSocket; | ||
|
|
||
| /* For whatever reason we were corked, update cork to the new socket */ | ||
| if (wasCorked) { | ||
| webSocket->AsyncSocket<SSL>::corkUnchecked(); | ||
| if (corkedSlot != LoopData::INVALID_CORK_SLOT) { | ||
| loopData->transferCorkSlot(corkedSlot, webSocket, SSL); |
There was a problem hiding this comment.
Capture the cork slot before internalEnd() releases it.
When upgrade() runs inside a corked response, internalEnd({nullptr, 0}, 0, false, false) reaches the zero-body completion path and uncorks the HTTP response. By the time this new findCorkSlot(this) runs, it usually returns INVALID_CORK_SLOT, so the adopted WebSocket never inherits/reacquires the caller's cork and immediate openHandler sends fall back to uncorked writes.
🩹 Proposed fix
- internalEnd({nullptr, 0}, 0, false, false);
+ LoopData *loopData = Super::getLoopData();
+ int corkedSlot = loopData->findCorkSlot(this);
+ internalEnd({nullptr, 0}, 0, false, false);
@@
- /* Before we adopt and potentially change socket, check which cork slot
- * we occupy so we can transfer it to the new WebSocket. */
- LoopData *loopData = Super::getLoopData();
- int corkedSlot = loopData->findCorkSlot(this);
-
/* Adopting a socket invalidates it, do not rely on it directly to carry any data */
us_socket_t *usSocket = us_socket_context_adopt_socket(SSL, (us_socket_context_t *) webSocketContext, (us_socket_t *) this, sizeof(HttpResponseData<SSL>), sizeof(WebSocketData) + sizeof(UserData));🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/bun-uws/src/HttpResponse.h` around lines 328 - 339, The cork slot is
being queried after internalEnd() has already uncorked the response, so
findCorkSlot(this) returns LoopData::INVALID_CORK_SLOT and the new WebSocket
never inherits the cork; to fix, capture the cork slot from
Super::getLoopData()->findCorkSlot(this) before calling internalEnd() in
upgrade(), then pass that saved corkedSlot into the code path that calls
us_socket_context_adopt_socket(...) and subsequently call
LoopData::transferCorkSlot(savedCorkedSlot, webSocket, SSL) (checking against
LoopData::INVALID_CORK_SLOT) so the adopted WebSocket reacquires the original
cork.
| if (!Super::isCorked()) { | ||
| LoopData *loopData = Super::getLoopData(); | ||
| Super::cork(); | ||
| /* Remember which slot we took so we can find our (possibly upgraded) | ||
| * replacement after the handler runs. */ | ||
| int ourSlot = loopData->findCorkSlot(this); | ||
| handler(); | ||
|
|
||
| /* The only way we could possibly have changed the corked socket during handler call, would be if | ||
| * the HTTP socket was upgraded to WebSocket and caused a realloc. Because of this we cannot use "this" | ||
| * from here downwards. The corking is done with corkUnchecked() in upgrade. It steals cork. */ | ||
| auto *newCorkedSocket = loopData->getCorkedSocket(); | ||
| * from here downwards. The upgrade path transfers the cork slot to the new WebSocket. */ | ||
| void *newCorkedSocket = ourSlot != LoopData::INVALID_CORK_SLOT | ||
| ? loopData->getCorkSlot(ourSlot)->socket | ||
| : nullptr; |
There was a problem hiding this comment.
Don't use slot ownership as the replacement-socket test.
With empty-slot stealing, ourSlot is no longer stable for the lifetime of this cork() scope. If the handler has not written yet, another request can rebind that slot before the handler returns; this block will then drive the later uncork/return path against that other socket. The only valid foreign replacement here is the upgraded socket from this response, so this needs an explicit handoff signal rather than rediscovering ownership from the slot table.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/bun-uws/src/HttpResponse.h` around lines 638 - 651, The current code
uses LoopData::findCorkSlot(…) and getCorkSlot(ourSlot)->socket after handler()
to detect a replacement socket, but because empty-slot stealing can rebind that
slot to another request, this is unsafe; instead introduce and check an explicit
handoff signal on the response object (e.g. a member like replacementCorkSocket
or corkHandoffFlag on the HttpResponse) that the upgrade path sets when it
transfers the cork slot to the new WebSocket; before calling handler() leave the
corked slot associated as now, call handler(), then read and clear this explicit
replacementCorkSocket on this object (and use that value) rather than using
LoopData::getCorkSlot(ourSlot)->socket or LoopData::findCorkSlot; also update
the upgrade code path to set that member when it takes ownership of the cork
slot.
9 test variants covering node:http and Bun.serve (buffered, pull stream, type:"direct", async generator) with multi-chunk writes and yields between chunks. Each response validated byte-for-byte so any foreign data from another request's cork slot fails the exact match.
10 attack vectors designed to trigger cross-socket data leakage: write-yield-write with slot held, yield-write with empty slot stolen, rapid microtask churn, burst-flush-burst, large chunks at buffer boundary. Covers node:http, Bun.serve buffered, type:direct, and async generator. 32 concurrent requests x 8 chunks each, byte-exact match.
| `); | ||
| }); | ||
|
|
||
| // Attack 2: yield BEFORE each write. Our slot is empty (stealable) at every | ||
| // yield point, then we try to write after someone may have taken it. | ||
| test("node:http — yield then write (empty slot stolen, must re-acquire)", async () => { | ||
| await run(` | ||
| import { createServer } from "node:http"; | ||
| const server = createServer(async (req, res) => { |
There was a problem hiding this comment.
🟡 The server.stop(true) call in the Bun.serve subprocess script is not wrapped in try/finally, violating the CLAUDE.md guideline to use await using or using for Bun.serve cleanup. If any of the 20 fetch() calls throws, Promise.all rejects and server.stop(true) is skipped; wrapping the fetch block in try/finally (or using await using server = Bun.serve({...})) would make cleanup unconditional.
Extended reasoning...
What the bug is
In the 'Bun.serve: nested async responses are all correct' test, the inline subprocess script (a string passed to bunExe() -e) creates a server with Bun.serve({...}) and calls server.stop(true) only after await Promise.all([...]) resolves successfully. There is no try/finally guard, so if any of the 20 concurrent fetch() calls throws (e.g., a network error, response parsing failure, or the server closing early), Promise.all rejects and server.stop(true) is never reached. This violates the CLAUDE.md guideline: 'Use await using or using to ensure proper resource cleanup. This works in most Bun APIs like Bun.listen, Bun.connect, Bun.spawn, Bun.serve, etc.'
The specific code path
Lines 88-95 of the subprocess script string:
Why existing code does not prevent it
The outer test correctly uses await using proc = Bun.spawn(...) for subprocess lifecycle management, but the inner subprocess script—being a string template evaluated by the spawned Bun process—has its own independent server whose cleanup is not tied to the outer using declaration. No try/finally block exists around the Promise.all call to guarantee server.stop(true) runs on rejection.
Addressing the refutation
One verifier correctly noted that Bun (like Node.js >= v15) treats unhandled promise rejections as fatal errors and exits the subprocess with a non-zero exit code rather than hanging. This is accurate: the test will fail with expect(exitCode).toBe(0) rather than timing out. The bug is therefore a coding-guidelines violation rather than a hang/timeout risk in normal operation. The severity is nit, consistent with what three confirmers concluded. CodeRabbit independently flagged this in the PR timeline at line 95.
Impact
In rare error scenarios (network hiccups, OS-level socket errors during the test), the subprocess exits with an unhandled rejection and leaves the server socket unclosed until process exit rather than invoking the clean server.stop(true) drain path. The test also produces a misleading error message (unhandled rejection) rather than a clear assertion failure.
Step-by-step proof
- Subprocess script starts; Bun.serve({...}) creates a server on a random port.
- 20 concurrent fetch() calls are launched via Promise.all.
- Suppose request Consider loading .env #7 encounters a transient network error; its .then(async r => ...) throws.
- Promise.all short-circuits and rejects with that error.
- The await Promise.all(...) expression throws; execution never reaches server.stop(true).
- Because no try/catch or try/finally is present, the rejection is unhandled.
- Bun's unhandled-rejection handler kills the process with exit code 1.
- The outer test's expect(exitCode).toBe(0) fails with exit code 1, and the server was never cleanly stopped.
How to fix
Since the subprocess is a string template, await using syntax is available (it is valid Bun/TypeScript). Replacing const server = Bun.serve({...}) with await using server = Bun.serve({...}) would cause the server to be deterministically stopped when the script scope exits, whether normally or via a thrown error—matching the CLAUDE.md guideline exactly. Alternatively, wrap the Promise.all block in try/finally { server.stop(true); }.
App.h: reset needsUncork at FIRST and after LAST in the pub/sub lambda. The mutable capture persisted across subscriber iterations, so if subscriber B was already corked (FIRST block skipped), it inherited A's needsUncork=true and got wrongly uncorked. HttpResponse.h: in cork(handler), check if we're still in a slot after handler() returns instead of reading whatever socket is in the slot we originally took. If our empty slot was stolen during an await, the old code would uncork the thief and return it cast to HttpResponse. Now we just return — our data (if any) was already flushed by whoever evicted us, and the upgrade case is handled by HttpContext or the drain loop.
internalEnd was uncorking before the upgrade path captured the cork slot, so the handshake went out in its own syscall and the WebSocket started uncorked. Now internalEnd takes a keepCorked flag that the upgrade path sets, letting the handshake batch with the first frames written in the open handler. The slot briefly holds a dangling old-this pointer between adopt_socket and transferCorkSlot, but nothing dereferences it in that window.
There was a problem hiding this comment.
Actionable comments posted: 2
♻️ Duplicate comments (1)
test/js/node/http/node-http-nested-cork.test.ts (1)
73-89:⚠️ Potential issue | 🟠 MajorGuarantee child-server teardown on failure paths.
Each inline harness only calls
close()/stop(true)afterawait validate(...). Ifvalidate()rejects or a handler throws first, the listener stays open andrun()can hang waiting onproc.exited. Please scope these servers withusing/await usingwhere supported, or move shutdown intofinally.As per coding guidelines,
Use \await using` or `using` to ensure proper resource cleanup for APIs like Bun.listen, Bun.connect, Bun.spawn, Bun.serve, etc.`Also applies to: 94-110, 115-132, 137-153, 159-176, 181-203, 208-230, 235-258, 263-282, 288-319
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/js/node/http/node-http-nested-cork.test.ts` around lines 73 - 89, The test's server created via createServer and started with .listen(...) can remain running if validate(...) rejects or the request handler throws, so ensure the listener is always closed: wrap the server lifecycle so that server.close() is invoked in a finally (or use await using where supported) after await validate(...). Specifically, update the async callback passed to run to scope the server variable and move server.close() into a finally block (or replace listen/close with an await using pattern) for the server created in this test (symbol: server) and apply the same change to the other inline harnesses referenced (the other server instances started in the listed ranges).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/bun-uws/src/AsyncSocket.h`:
- Around line 382-392: The zero-length branch in AsyncSocket::uncork() is
returning 'written' from the internal write(buffer, (int)offset, false, length)
call which double-counts corked bytes (those were already accounted for by the
original write), so change the length == 0 return to return {0, failed} instead
of {written, failed}; update the uncork() branch that checks 'if (length == 0)'
to return {0, failed} and keep a brief comment noting corked bytes were already
accounted for to prevent future regressions (references: AsyncSocket::uncork(),
write(buffer, (int)offset, false, length, variables: buffer, offset, length).
- Around line 154-163: The cork buffer check rejects exact fits because it uses
(currentOffset + size < LoopData::CORK_BUFFER_SIZE); update the condition to
allow exact fills by changing that comparison to <= so exact remaining==length
writes use the cork slot instead of falling back; adjust the condition in the
function containing existingBackpressure, corked, loopData->canCork(),
currentOffset, size and LoopData::CORK_BUFFER_SIZE so
acquireCorkSlot/getCorkSlot/touchCorkSlot paths are taken for exact-fit writes
and return the same SendBufferAttribute logic as before.
---
Duplicate comments:
In `@test/js/node/http/node-http-nested-cork.test.ts`:
- Around line 73-89: The test's server created via createServer and started with
.listen(...) can remain running if validate(...) rejects or the request handler
throws, so ensure the listener is always closed: wrap the server lifecycle so
that server.close() is invoked in a finally (or use await using where supported)
after await validate(...). Specifically, update the async callback passed to run
to scope the server variable and move server.close() into a finally block (or
replace listen/close with an await using pattern) for the server created in this
test (symbol: server) and apply the same change to the other inline harnesses
referenced (the other server instances started in the listed ranges).
🪄 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: 62d4b258-9a7d-4891-bc33-77b0d35e6f05
📒 Files selected for processing (2)
packages/bun-uws/src/AsyncSocket.htest/js/node/http/node-http-nested-cork.test.ts
| if (offset) { | ||
| /* Corked data is already accounted for via its write call */ | ||
| auto [written, failed] = write(buffer, (int) offset, false, length); | ||
|
|
||
| if (failed && optionally) { | ||
| /* We do not need to care for buffering here, write does that */ | ||
| return {0, true}; | ||
| } | ||
| if (length == 0) { | ||
| return {written, failed}; | ||
| } |
There was a problem hiding this comment.
Don't count already-accounted cork bytes from zero-arg uncork().
The length == 0 branch returns written from flushing buffer, but this method explicitly says cork-buffer bytes were already accounted for by the original write() call. Any caller that uses the return value for accounting/backpressure will double-count that flush.
🐛 Suggested fix
- if (length == 0) {
- return {written, failed};
- }
+ if (length == 0) {
+ return {0, failed};
+ }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/bun-uws/src/AsyncSocket.h` around lines 382 - 392, The zero-length
branch in AsyncSocket::uncork() is returning 'written' from the internal
write(buffer, (int)offset, false, length) call which double-counts corked bytes
(those were already accounted for by the original write), so change the length
== 0 return to return {0, failed} instead of {written, failed}; update the
uncork() branch that checks 'if (length == 0)' to return {0, failed} and keep a
brief comment noting corked bytes were already accounted for to prevent future
regressions (references: AsyncSocket::uncork(), write(buffer, (int)offset,
false, length, variables: buffer, offset, length).
There was a problem hiding this comment.
♻️ Duplicate comments (1)
packages/bun-uws/src/HttpResponse.h (1)
648-656:⚠️ Potential issue | 🟠 MajorDon't treat every missing slot as an upgrade handoff.
Line 655 now short-circuits whenever this response no longer owns a slot, but a normal
internalEnd()inside the handler also releases the slot at Lines 158-160 and Lines 223-225. In that case the close-on-finished-response block at Lines 668-679 is skipped, so aConnection: closeresponse that just uncorked can stay open. Only bypass the tail path on an explicit upgrade/handoff signal.Also applies to: 668-679
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/bun-uws/src/HttpResponse.h` around lines 648 - 656, The early return when loopData->findCorkSlot(this) == LoopData::INVALID_CORK_SLOT incorrectly treats all lost-cork-slot cases as upgrades; change the condition in HttpResponse so it only short-circuits when an explicit upgrade/handoff signal is present (rather than any missing slot). Concretely, keep the findCorkSlot check but also require the upgrade/handoff flag (e.g., an existing upgrade/handoff indicator on HttpResponse — add or use a method like isUpgraded()/wasHandedOff() or a boolean handoff flag) before returning; otherwise let the normal tail-path run so internalEnd() and the close-on-finished-response handling still execute for regular internalEnd() releases. Ensure the same guard is applied where the close-on-finished-response block is skipped.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@packages/bun-uws/src/HttpResponse.h`:
- Around line 648-656: The early return when loopData->findCorkSlot(this) ==
LoopData::INVALID_CORK_SLOT incorrectly treats all lost-cork-slot cases as
upgrades; change the condition in HttpResponse so it only short-circuits when an
explicit upgrade/handoff signal is present (rather than any missing slot).
Concretely, keep the findCorkSlot check but also require the upgrade/handoff
flag (e.g., an existing upgrade/handoff indicator on HttpResponse — add or use a
method like isUpgraded()/wasHandedOff() or a boolean handoff flag) before
returning; otherwise let the normal tail-path run so internalEnd() and the
close-on-finished-response handling still execute for regular internalEnd()
releases. Ensure the same guard is applied where the close-on-finished-response
block is skipped.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: e70a170f-5189-4237-9f10-903ff9af596b
📒 Files selected for processing (2)
packages/bun-uws/src/App.hpackages/bun-uws/src/HttpResponse.h
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@packages/bun-uws/src/AsyncSocket.h`:
- Around line 125-137: The post-eviction call to loopData->acquireCorkSlot(this,
SSL) assumes success but has no check; add an assertion that acquireCorkSlot
returns the expected success (or non-null) to document the invariant and catch
unexpected failures. Locate the eviction branch in AsyncSocket.h that uses
loopData->getLRUCorkSlot(), getCorkSlot(), uncork() on the victim (casting to
AsyncSocket<true>/AsyncSocket<false>) and then calls
loopData->acquireCorkSlot(this, SSL), and insert an assert (or equivalent check)
immediately after that call to validate the acquisition succeeded.
🪄 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: 2617a82c-d6c8-4cdc-aa0b-2d6a7e2af572
📒 Files selected for processing (1)
packages/bun-uws/src/AsyncSocket.h
| /* Both slots hold data from other sockets. Force-uncork the least | ||
| * recently used one to make room. */ | ||
| int victimSlot = loopData->getLRUCorkSlot(); | ||
| auto *vs = loopData->getCorkSlot(victimSlot); | ||
| void *victim = vs->socket; | ||
| bool victimSsl = vs->ssl; | ||
| if (victimSsl) { | ||
| ((AsyncSocket<true> *) victim)->uncork(); | ||
| } else { | ||
| ((AsyncSocket<false> *) victim)->uncork(); | ||
| } | ||
| loopData->acquireCorkSlot(this, SSL); | ||
| } |
There was a problem hiding this comment.
🧹 Nitpick | 🔵 Trivial
Consider asserting the post-eviction acquisition succeeds.
After force-uncorking the victim, acquireCorkSlot should always succeed since we just freed a slot. Adding an assertion would catch unexpected failures and document the invariant.
♻️ Suggested change
- loopData->acquireCorkSlot(this, SSL);
+ int acquired = loopData->acquireCorkSlot(this, SSL);
+ ASSERT(acquired != LoopData::INVALID_CORK_SLOT);
+ (void)acquired; /* silence unused-variable warning in release builds */🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@packages/bun-uws/src/AsyncSocket.h` around lines 125 - 137, The post-eviction
call to loopData->acquireCorkSlot(this, SSL) assumes success but has no check;
add an assertion that acquireCorkSlot returns the expected success (or non-null)
to document the invariant and catch unexpected failures. Locate the eviction
branch in AsyncSocket.h that uses loopData->getLRUCorkSlot(), getCorkSlot(),
uncork() on the victim (casting to AsyncSocket<true>/AsyncSocket<false>) and
then calls loopData->acquireCorkSlot(this, SSL), and insert an assert (or
equivalent check) immediately after that call to validate the acquisition
succeeded.
| /* Both slots hold data from other sockets. Force-uncork the least | ||
| * recently used one to make room. */ | ||
| int victimSlot = loopData->getLRUCorkSlot(); | ||
| auto *vs = loopData->getCorkSlot(victimSlot); | ||
| void *victim = vs->socket; | ||
| bool victimSsl = vs->ssl; | ||
| if (victimSsl) { | ||
| ((AsyncSocket<true> *) victim)->uncork(); | ||
| } else { | ||
| ((AsyncSocket<false> *) victim)->uncork(); | ||
| } | ||
| loopData->acquireCorkSlot(this, SSL); | ||
| } |
There was a problem hiding this comment.
🟡 In AsyncSocket::cork() (AsyncSocket.h:136), the second acquireCorkSlot() call after force-uncorking the LRU victim silently discards its return value. The first call at line 121 properly checks the return value; the post-eviction call does not. Adding ASSERT(loopData->acquireCorkSlot(this, SSL) != LoopData::INVALID_CORK_SLOT) would enforce the invariant and make any future regression immediately visible.
Extended reasoning...
What the bug is
In AsyncSocket::cork(), after force-uncorking the LRU victim to free a slot, the final acquireCorkSlot() call at line 136 silently discards its return value. This is inconsistent with the first call at line 121, which explicitly validates the result with: if (loopData->acquireCorkSlot(this, SSL) != LoopData::INVALID_CORK_SLOT) return;
The specific code path
After releaseCorkSlot() sets corkSlots[victimSlot].socket = nullptr inside uncork(), findBorrowableCorkSlot() will always find that freed slot. So acquireCorkSlot() is guaranteed to succeed here — but there is no assertion to enforce or document this guarantee.
Addressing the refutation
The single refuting verifier is correct that the failure scenario cannot occur in the current implementation: releaseCorkSlot() is called unconditionally at the very start of uncork() (before any write callbacks), so the slot is provably free when we return to the caller. The single-threaded event loop provides no opportunity for re-entry between the victim's uncork() return and the final acquireCorkSlot() call. All three confirming verifiers acknowledge the same thing.
The concern is not that the current code is broken — it is not. The concern is that this is entirely new code introduced by this PR, and the missing ASSERT leaves the invariant implicit. If a future refactor introduces a bug in releaseCorkSlot() (e.g., fails to clear the socket pointer) or adds a new code path, the failure at line 136 would be silent: the socket believes it is now corked, but isCorked() returns false, and all subsequent writes go uncorked without any diagnostic.
Why the inconsistency matters
Line 121 and line 136 call the same function under different conditions. Line 121 properly checks the result; line 136 does not. A reader maintaining this code must figure out from first principles why the post-eviction call is safe. An ASSERT makes the invariant self-documenting and catches any future regression immediately.
Step-by-step proof that the invariant holds today
- cork() calls getLRUCorkSlot() to identify victim slot V.
- cork() calls uncork() on the victim socket.
- uncork() immediately calls loopData->releaseCorkSlot(V), setting corkSlots[V].socket = nullptr.
- uncork() then writes buffered data (single-threaded, no JS callbacks fire).
- uncork() returns to cork().
- cork() calls acquireCorkSlot(this, SSL).
- findBorrowableCorkSlot() checks corkSlots[V].socket == nullptr -- true -- returns V.
- acquireCorkSlot() assigns this socket to slot V and returns V (not INVALID_CORK_SLOT).
The invariant holds. An ASSERT would cost nothing in release builds and catch any future regression immediately.
Suggested fix: int acquiredSlot = loopData->acquireCorkSlot(this, SSL); ASSERT(acquiredSlot != LoopData::INVALID_CORK_SLOT);
…-sh#28615) ## What Replaces uWebSockets' single shared cork buffer with two independent cork slots. ## Why When an async HTTP handler awaits and later resumes (e.g. another request resolves its promise), the resumed request's `writeHead()` + `end()` couldn't get the cork buffer because the newer in-flight request was holding it. This forced the resumed request down the uncorked path — one `write()` syscall per call instead of batching them. On the `bench/snippets/http-hello.node.mjs` interleaved-await pattern this dropped throughput from ~190k req/s to ~22k req/s. ## How - **Two independent slots**: each holds `{buffer, socket, offset, ssl}`. `cork()` grabs whichever slot is free; `uncork()` releases yours. No ordering constraints. - **Empty-slot stealing**: if both slots are claimed but one has `offset==0` (corked but not yet written), it's freely stolen — no flush needed. - **LRU eviction**: when both slots hold actual data (rare, depth 3+), force-uncork the least-recently-touched one. Tracked via a single bit. - **Entry-point re-cork**: `NodeHTTP writeHead` and `HttpResponse::cork(handler)` call `cork()` before each write sequence, so if your slot was stolen while you were awaiting, you transparently get a fresh one. - **UAF protection**: `uncorkWithoutSending()` (called from all close/destroy paths) now clears the socket from any slot via `unborrowCorkSlot()`, preventing the drain loop from dereferencing a freed pointer. - **Bounded drain**: `preCb` and `uws_res_clear_corked_socket` iterate at most twice (one per slot). Memory cost: one extra 16KB buffer per event loop. ## Tests - `test/js/node/http/node-http-nested-cork.test.ts` — 20 concurrent interleaved-await requests for both node:http and Bun.serve, asserts all responses are correct with no cross-socket bleed. - `serve.test.ts` (189 tests), `node-http.test.ts` (78 tests), `websocket-server.test.ts` all pass. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
…-sh#28615) ## What Replaces uWebSockets' single shared cork buffer with two independent cork slots. ## Why When an async HTTP handler awaits and later resumes (e.g. another request resolves its promise), the resumed request's `writeHead()` + `end()` couldn't get the cork buffer because the newer in-flight request was holding it. This forced the resumed request down the uncorked path — one `write()` syscall per call instead of batching them. On the `bench/snippets/http-hello.node.mjs` interleaved-await pattern this dropped throughput from ~190k req/s to ~22k req/s. ## How - **Two independent slots**: each holds `{buffer, socket, offset, ssl}`. `cork()` grabs whichever slot is free; `uncork()` releases yours. No ordering constraints. - **Empty-slot stealing**: if both slots are claimed but one has `offset==0` (corked but not yet written), it's freely stolen — no flush needed. - **LRU eviction**: when both slots hold actual data (rare, depth 3+), force-uncork the least-recently-touched one. Tracked via a single bit. - **Entry-point re-cork**: `NodeHTTP writeHead` and `HttpResponse::cork(handler)` call `cork()` before each write sequence, so if your slot was stolen while you were awaiting, you transparently get a fresh one. - **UAF protection**: `uncorkWithoutSending()` (called from all close/destroy paths) now clears the socket from any slot via `unborrowCorkSlot()`, preventing the drain loop from dereferencing a freed pointer. - **Bounded drain**: `preCb` and `uws_res_clear_corked_socket` iterate at most twice (one per slot). Memory cost: one extra 16KB buffer per event loop. ## Tests - `test/js/node/http/node-http-nested-cork.test.ts` — 20 concurrent interleaved-await requests for both node:http and Bun.serve, asserts all responses are correct with no cross-socket bleed. - `serve.test.ts` (189 tests), `node-http.test.ts` (78 tests), `websocket-server.test.ts` all pass. --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
What
Replaces uWebSockets' single shared cork buffer with two independent cork slots.
Why
When an async HTTP handler awaits and later resumes (e.g. another request resolves its promise), the resumed request's
writeHead()+end()couldn't get the cork buffer because the newer in-flight request was holding it. This forced the resumed request down the uncorked path — onewrite()syscall per call instead of batching them. On thebench/snippets/http-hello.node.mjsinterleaved-await pattern this dropped throughput from ~190k req/s to ~22k req/s.How
{buffer, socket, offset, ssl}.cork()grabs whichever slot is free;uncork()releases yours. No ordering constraints.offset==0(corked but not yet written), it's freely stolen — no flush needed.NodeHTTP writeHeadandHttpResponse::cork(handler)callcork()before each write sequence, so if your slot was stolen while you were awaiting, you transparently get a fresh one.uncorkWithoutSending()(called from all close/destroy paths) now clears the socket from any slot viaunborrowCorkSlot(), preventing the drain loop from dereferencing a freed pointer.preCbanduws_res_clear_corked_socketiterate at most twice (one per slot).Memory cost: one extra 16KB buffer per event loop.
Tests
test/js/node/http/node-http-nested-cork.test.ts— 20 concurrent interleaved-await requests for both node:http and Bun.serve, asserts all responses are correct with no cross-socket bleed.serve.test.ts(189 tests),node-http.test.ts(78 tests),websocket-server.test.tsall pass.