diff --git a/src/http/lib.rs b/src/http/lib.rs index 9f813f795f8c..91849102ddbf 100644 --- a/src/http/lib.rs +++ b/src/http/lib.rs @@ -4898,19 +4898,20 @@ impl<'a> HTTPClient<'a> { location = header.value(); } h if h == hash_header_const(b"Connection") => { - if response.status_code >= 200 && response.status_code <= 299 { - // HTTP headers are case-insensitive (RFC 7230) - if bun_core::strings::eql_case_insensitive_ascii_check_length( - header.value(), - b"close", - ) { - self.state.flags.allow_keepalive = false; - } else if bun_core::strings::eql_case_insensitive_ascii_check_length( + // `close` applies on any status (RFC 9112 §9.6); only an + // explicit `keep-alive` is gated on a 2xx success. + if bun_core::strings::eql_case_insensitive_ascii_check_length( + header.value(), + b"close", + ) { + self.state.flags.allow_keepalive = false; + } else if (200..=299).contains(&response.status_code) + && bun_core::strings::eql_case_insensitive_ascii_check_length( header.value(), b"keep-alive", - ) { - self.state.flags.allow_keepalive = true; - } + ) + { + self.state.flags.allow_keepalive = true; } } h if h == hash_header_const(b"Last-Modified") => { diff --git a/test/js/web/fetch/fetch-keepalive.test.ts b/test/js/web/fetch/fetch-keepalive.test.ts index d949b21df8de..83440ef8563e 100644 --- a/test/js/web/fetch/fetch-keepalive.test.ts +++ b/test/js/web/fetch/fetch-keepalive.test.ts @@ -73,6 +73,59 @@ test("fetch does not reuse a pooled TLS connection for a request with a differen expect(plain).not.toBe(overrideA); }); +// RFC 9112 §9.6: a server's `Connection: close` applies regardless of status +// code. Previously bun only honored it for 2xx, so a 4xx/5xx with +// `Connection: close` was pooled; a concurrent fetch that picked the pooled +// socket before the server's FIN landed failed with ECONNRESET. Subprocess +// so the pool can't leak into other tests. The server here keeps the socket +// open and answers every request on it, so the connection count is exactly +// how many times bun dialed (pooled reuse => 1, correct => 4). +test.concurrent.each([200, 400, 413, 500])("a %d response with Connection: close is not pooled", async status => { + await using proc = Bun.spawn({ + cmd: [ + bunExe(), + "-e", + ` + import net from "node:net"; + let connections = 0; + const server = net.createServer(sock => { + connections++; + sock.on("error", () => {}); + let buf = ""; + sock.on("data", d => { + buf += d.toString("latin1"); + while (buf.includes("\\r\\n\\r\\n")) { + buf = buf.slice(buf.indexOf("\\r\\n\\r\\n") + 4); + sock.write("HTTP/1.1 ${status} X\\r\\nContent-Length: 2\\r\\nConnection: close\\r\\n\\r\\nok"); + } + }); + }); + server.listen(0, "127.0.0.1"); + await new Promise(r => server.on("listening", r)); + const url = "http://127.0.0.1:" + server.address().port + "/"; + const statuses = []; + for (let i = 0; i < 4; i++) { + const res = await fetch(url, { keepalive: true }); + statuses.push(res.status); + await res.text(); + } + console.log(JSON.stringify({ statuses, connections })); + process.exit(0); + `, + ], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + const result = stdout.startsWith("{") ? JSON.parse(stdout.trim()) : { stdout, stderr }; + expect({ result, exitCode }).toEqual({ + result: { statuses: [status, status, status, status], connections: 4 }, + exitCode: 0, + }); +}); + // A reused keep-alive connection reset during a streaming PUT must reject with // ECONNRESET, not retry: the stream body is already consumed, and the retry // panicked in send_initial_request_payload. Subprocess: the panic aborts the process.