Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/http/Signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ impl Signals {
.map(bun_ptr::BackRef::from)
.is_some_and(|a| a.load(Ordering::Acquire) == BodyReceiveMode::Paused as u8)
}

#[inline]
pub fn is_receive_ignored(self) -> bool {
self.body_receive_mode
.map(bun_ptr::BackRef::from)
.is_some_and(|a| a.load(Ordering::Acquire) == BodyReceiveMode::Ignore as u8)
}
}

pub struct Store {
Expand Down
20 changes: 16 additions & 4 deletions src/http/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@

pub const END_OF_CHUNKED_HTTP1_1_ENCODING_RESPONSE_BODY: &[u8] = b"0\r\n\r\n";

/// Wall-clock cap on the `BodyReceiveMode::Ignore` discard-drain (not re-armed
/// per read in `on_data`).
Comment thread
robobun marked this conversation as resolved.
Resolved
pub const IGNORE_DRAIN_TIMEOUT_SECONDS: c_uint = 5;

/// HTTP-thread-only scratch buffer for building NUL-terminated hostnames.
pub static TEMP_HOSTNAME: bun_core::RacyCell<[u8; 8192]> = bun_core::RacyCell::new([0; 8192]);

Expand Down Expand Up @@ -3818,7 +3822,9 @@

if self.proxy_tunnel.is_some() {
// if we have a tunnel we dont care about the other stages, we will just tunnel the data
self.set_timeout(&socket);
if !self.signals.is_receive_ignored() {
self.set_timeout(&socket);
}
self.proxy_tunnel_mut().unwrap().receive(incoming_data);
return;
}
Expand All @@ -3837,7 +3843,7 @@
self.handle_on_data_headers::<IS_SSL>(incoming_data, ctx, socket);
}
ResponseStage::Body => {
if !self.state.flags.receive_paused {
if !self.state.flags.receive_paused && !self.signals.is_receive_ignored() {
Comment thread
robobun marked this conversation as resolved.
Resolved
self.set_timeout(&socket);
}

Expand All @@ -3855,7 +3861,7 @@
}
}
ResponseStage::BodyChunk => {
if !self.state.flags.receive_paused {
if !self.state.flags.receive_paused && !self.signals.is_receive_ignored() {
self.set_timeout(&socket);
}

Expand Down Expand Up @@ -4042,7 +4048,13 @@
// timeout disabled and the body promise pending forever.
let _ = socket.resume_stream();
bun_core::scoped_log!(fetch, "resume receive {}", self.async_http_id);
self.set_timeout(&socket);
if self.signals.is_receive_ignored() {
// Not re-armed in `on_data` while ignored: absolute drain deadline.
// Proxy tunnels never pause so never reach this arm.
Comment thread
robobun marked this conversation as resolved.
Resolved
socket.set_timeout(IGNORE_DRAIN_TIMEOUT_SECONDS);

Check warning on line 4054 in src/http/lib.rs

View check run for this annotation

Claude / Claude Code Review

5s Ignore-drain deadline is a no-op when flags.disable_timeout is set

The 5 s "absolute drain deadline" is a no-op for `fetch(url, { timeout: false })`: `resume_receive` arms `socket.set_timeout(5)` directly, but `on_timeout` at lib.rs:2105 early-returns on `self.flags.disable_timeout`, and since `on_data` no longer re-arms in Ignore mode the socket is left with no timer at all. Not a regression (pre-PR `resume_receive` → `self.set_timeout()` → `set_timeout(0)` was equally indefinite), but the comment's invariant doesn't hold for this case — consider having `on_ti
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
Comment thread
robobun marked this conversation as resolved.
Resolved
} else {
self.set_timeout(&socket);
}
}

pub fn drain_response_body<const IS_SSL: bool>(&mut self, socket: HttpSocket<IS_SSL>) {
Expand Down
28 changes: 26 additions & 2 deletions src/runtime/webcore/fetch/FetchTasklet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2502,7 +2502,31 @@ impl FetchTasklet {
}
}

/// Drain-to-reuse cap for a GC-abandoned body (undici's `Readable.dump` is 128 KiB).
const ABANDONED_RESPONSE_DRAIN_MAX_BYTES: usize = 256 * 1024;

impl FetchTasklet {
/// Scenario 2b/3 of [`on_response_finalize`]. Runs inside a Weak finalizer:
/// no `JSCell` may be touched, so the close arm inlines `abort_task` minus
/// `tracker.did_cancel`.
Comment thread
robobun marked this conversation as resolved.
Resolved
fn abandon_response_body_from_finalizer(&mut self) {
// `body_size` is written by HTTP-thread `callback()` under `self.mutex`.
self.mutex.lock();
let body_size = self.body_size;
self.mutex.unlock();
// `Content-Length` is wire bytes; `scheduled_response_buffer` is decompressed.
let drain = matches!(
body_size,
http::BodySize::ContentLength(n) if n <= ABANDONED_RESPONSE_DRAIN_MAX_BYTES
);
if !drain && !self.signal_store.aborted.swap(true, Ordering::Relaxed) {
if let Some(http_) = self.http.as_mut() {
http::http_thread().schedule_shutdown(http_);
}
}
self.ignore_remaining_response_body(true);
}

#[bun_uws::uws_callback(export = "Bun__FetchResponse_finalize", no_catch)]
pub(crate) fn on_response_finalize(&mut self) {
bun_output::scoped_log!(FetchTasklet, "onResponseFinalize");
Expand Down Expand Up @@ -2531,11 +2555,11 @@ impl FetchTasklet {
if let Some(promise) = locked.promise {
if promise.is_empty_or_undefined_or_null() {
// Scenario 2b.
this.ignore_remaining_response_body(true);
this.abandon_response_body_from_finalizer();
}
} else {
// Scenario 3.
this.ignore_remaining_response_body(true);
this.abandon_response_body_from_finalizer();
}
}
}
Expand Down
111 changes: 111 additions & 0 deletions test/js/web/fetch/fetch-response-abandoned-drain.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// When a fetch() Response is dropped without the body being read, the Weak
// finalizer on the JS Response wrapper reaches
// FetchTasklet::on_response_finalize (scenario 3: Locked body, no stream, no
// promise). Previously that path unconditionally drained the remaining body so
// the connection could be pooled, with no upper bound: an abandoned 32 MB
// response was read in full and the socket reused. Now the drain is capped at
// 256 KiB of remaining Content-Length; anything larger (or chunked/unknown)
// closes the connection instead.

import { describe, expect, test } from "bun:test";
import { bunEnv, bunExe, isWindows } from "harness";

// The server writes the body in 16 KiB chunks, pausing on backpressure so
// `bodyWritten` tracks what the client actually pulled plus at most one kernel
// send buffer. The client fetches, drops the Response, forces GC so the Weak
// finalizer runs, and then waits until the server has either sent the whole
// body or seen the socket close.
function fixture(contentLength: number) {
return /* js */ `
const net = require("node:net");

const contentLength = ${contentLength};
const chunk = Buffer.alloc(16 * 1024, 0x61);
let bodyWritten = 0;
let conn;
const { promise: done, resolve: onDone } = Promise.withResolvers();

const server = net.createServer(socket => {
conn = socket;
socket.on("error", () => {});
socket.on("close", onDone);
let replied = false;
socket.on("data", () => {
if (replied) return;
replied = true;
socket.write(
"HTTP/1.1 200 OK\\r\\n" +
"Content-Length: " + contentLength + "\\r\\n" +
"Connection: keep-alive\\r\\n" +
"\\r\\n",
);
const writeMore = () => {
while (bodyWritten < contentLength && !socket.destroyed) {
const n = Math.min(chunk.length, contentLength - bodyWritten);
const ok = socket.write(n === chunk.length ? chunk : chunk.subarray(0, n));
bodyWritten += n;
if (!ok) return socket.once("drain", writeMore);
}
onDone();
};
writeMore();
});
});
await new Promise(r => server.listen(0, "127.0.0.1", r));
const port = server.address().port;

let collected = false;
const reg = new FinalizationRegistry(() => { collected = true; });
async function once() {
const res = await fetch("http://127.0.0.1:" + port + "/");
if (res.status !== 200) throw new Error("status " + res.status);
reg.register(res, 0);
// Drop the Response without touching .body / .text() etc.
}
await once();
while (!collected) {
Bun.gc(true);
await new Promise(r => setImmediate(r));
}

await done;
await new Promise(r => setImmediate(r));

console.log(bodyWritten + " " + (conn.destroyed ? "closed" : "open"));
conn.destroy();
server.close();
`;
}

async function run(contentLength: number) {
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", fixture(contentLength)],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect({ stderr, exitCode }).toEqual({ stderr: expect.any(String), exitCode: 0 });
const [bodyWritten, state] = stdout.trim().split(" ");
return { bodyWritten: Number(bodyWritten), state };
}

// Platform-independent code path; collectContinuously-style GC tests are slow
// on Windows CI so keep this off there (matches fetch-response-finalizer-sweep).
describe.skipIf(isWindows)("fetch: abandoned Response body drain policy", () => {
test("small body (64 KiB) is drained so the connection stays poolable", async () => {
const { bodyWritten, state } = await run(64 * 1024);
expect({ bodyWritten, state }).toEqual({ bodyWritten: 64 * 1024, state: "open" });
}, 60_000);

Check warning on line 99 in test/js/web/fetch/fetch-response-abandoned-drain.test.ts

View check run for this annotation

Claude / Claude Code Review

64 KiB test likely does not exercise the finalizer drain arm it claims to guard

The 64 KiB test almost certainly does not exercise `abandon_response_body_from_finalizer()`: on loopback the server writes headers + 4×16 KiB synchronously, the HTTP thread sees the complete body in one `on_data()`, `to_body_value()` returns `InternalBlob` (not `Locked`), and `on_response_finalize` early-returns at :2546 — the connection is pooled via normal completion, not the drain arm. Your own before/after table shows the 64 KiB row is identical on `main` and this branch, which is what you'd
Comment thread
robobun marked this conversation as resolved.
Resolved

test("large body (32 MiB) is closed instead of drained in full", async () => {
const total = 32 * 1024 * 1024;
const { bodyWritten, state } = await run(total);
// The server front-loads one kernel send buffer before the client ever
// pauses, so `bodyWritten` includes that window on top of the 256 KiB cap.
// Without the cap the full 32 MiB is drained; a quarter of that is well
// above any loopback send buffer and well below the unbounded drain.
expect(bodyWritten).toBeLessThan(total / 4);
expect(state).toBe("closed");
}, 60_000);
});
Loading