Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
2 changes: 1 addition & 1 deletion packages/bun-uws/src/AsyncSocketData.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ struct AsyncSocketData {
* that can still reach a handler counts these. */
bool filteredAccept = false;
/* Whether it has fired the filter with -3 (HttpResponse::setNodeHttpTunnelIdle).
* +3 undoes it, and an idle socket fires -4 in place of -2. */
* +3 or +4 undoes it, and an idle socket fires -4 in place of -2. */
bool filteredIdleTunnel = false;
};

Expand Down
10 changes: 6 additions & 4 deletions packages/bun-uws/src/HttpResponse.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,18 @@ struct HttpResponse : public AsyncSocket<SSL> {
return true;
}

/* node:http: an idle tunnel is at read EOF and has nothing left to send. Like a libuv handle in that state, it does not hold
* the event loop. The filter hears -3 when a tunnel becomes idle and +3 when it has bytes to send again. */
void setNodeHttpTunnelIdle(bool idle) {
/* node:http: an idle tunnel does not read (its reads are stopped or at EOF) and has nothing left to send. Like a libuv handle in
* that state, it does not hold the event loop: https://github.com/nodejs/node/blob/v26.3.0/deps/uv/src/unix/stream.c#L1473-L1485.
* The filter hears -3 when a tunnel becomes idle. When it is not idle any more, the filter hears +3 at read EOF (it has bytes to
* send) and +4 otherwise (it reads or has bytes to send). */
void setNodeHttpTunnelIdle(bool idle, bool readEnded) {
HttpResponseData<SSL> *httpResponseData = getHttpResponseData();
if (!httpResponseData->filteredAccept || httpResponseData->filteredIdleTunnel == idle) {
return;
}
httpResponseData->filteredIdleTunnel = idle;
for (auto &f : HttpContext<SSL>::getSocketContextDataS((us_socket_t *) this)->filterHandlers) {
f(this, idle ? -3 : 3);
f(this, idle ? -3 : readEnded ? 3 : 4);
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/jsc/bindings/node/JSNodeHTTPServerSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ void JSNodeHTTPServerSocket::readStop()
}
tunnelReadsStopped = true;
applyTunnelReads();
updateTunnelIdle();
}

void JSNodeHTTPServerSocket::readStart()
Expand All @@ -212,6 +213,7 @@ void JSNodeHTTPServerSocket::readStart()
}
tunnelReadsStopped = false;
applyTunnelReads();
updateTunnelIdle();
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
}

void JSNodeHTTPServerSocket::didDeliverQueuedTunnelBytes(size_t length)
Expand Down Expand Up @@ -884,14 +886,14 @@ extern "C" bool Bun__NodeHTTPServerSocket__writeBehindResponse(us_socket_t* sock

void JSNodeHTTPServerSocket::updateTunnelIdle()
{
if (!tunnelReadEnded || upgraded || isClosed()) {
if (!isTunnel(this)) {
return;
}
Comment on lines +902 to 904

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟣 pre-existing, not blocking: pre-existing: on an Upgrade request that carries a body, bytes a listener writes before the body completes neither hold the event loop nor get flushed, so behind server.unref() the process exits with them unsent and socket.end() after them never sends its FIN. updateTunnelIdle() at JSNodeHTTPServerSocket.cpp:902 returns while isConnectRequest is still false (HTTP_NODE_TUNNEL_AFTER_BODY), so no +5 fires, and HttpContext.h:940 skips onSocketDrain for the same reason, so the writable event drops its interest with streamBuffer still full. Fix: treat a HTTP_NODE_TUNNEL_AFTER_BODY socket as a tunnel for drain and sending/idle accounting, so the rule "bytes left to send hold the loop" covers this sibling path too.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

An 'upgrade' listener for a request with Content-Length > 0 calls socket.write(data) larger than the kernel takes while the body is still arriving. jsFunctionNodeHTTPServerSocketWrite (JSNodeHTTPServerSocketPrototype.cpp:281) parks the rest in streamBuffer, then calls updateTunnelIdle(), which returns at JSNodeHTTPServerSocket.cpp:902 because isTunnel() reads isConnectRequest and that is only set at HttpContext.h:672 when the body's fin is parsed; so setNodeHttpTunnelSending(true) never runs and sending_tunnel_ref (mod.rs:1633) is never taken. The writable event reaches HttpContext::onWritable, but :940 gates onSocketDrain on isConnectRequest, so JSNodeHTTPServerSocket::onDrain never flushes streamBuffer and loop.c:665 drops the writable interest. With server.unref() and nothing else holding the loop, the process exits with the bytes unsent; without unref, a later socket.end() returns at JSNodeHTTPServerSocketPrototype.cpp:317-320 waiting for a drain that never comes, so the FIN is never sent. The base branch behaves the same on this path.

Verification: pre-existing. An 'upgrade' listener for an Upgrade request that carries a body writes more than the kernel takes while the body is still arriving. updateTunnelIdle() returns at JSNodeHTTPServerSocket.cpp:902, so no +5 fires. HttpContext::onWritable skips onSocketDrain at HttpContext.h:940, and loop.c:665-666 then drops writable polling. Behind server.unref() the process exits with the bytes unsent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stays as is, and the description lists it under "Not changed by this PR": the same output on 1.4.3, on a canary of main, on 5af7e9d and here, for http and https. Taking +5 alone there would be worse than today, because uWS does not flush that buffer until the body has ended: a body that never ends would hold the process for good. It needs the drain path changed first, so it is a fix of its own.


Generated by Claude Code

const bool sent = streamBuffer.bufferedSize() == 0;
const bool idle = (tunnelReadEnded || tunnelReadsStopped) && streamBuffer.bufferedSize() == 0;
if (is_ssl) {
reinterpret_cast<uWS::HttpResponse<true>*>(socket)->setNodeHttpTunnelIdle(sent && reinterpret_cast<uWS::AsyncSocket<true>*>(socket)->hasFullyDrained());
reinterpret_cast<uWS::HttpResponse<true>*>(socket)->setNodeHttpTunnelIdle(idle && reinterpret_cast<uWS::AsyncSocket<true>*>(socket)->hasFullyDrained(), tunnelReadEnded);
} else {
reinterpret_cast<uWS::HttpResponse<false>*>(socket)->setNodeHttpTunnelIdle(sent && reinterpret_cast<uWS::AsyncSocket<false>*>(socket)->hasFullyDrained());
reinterpret_cast<uWS::HttpResponse<false>*>(socket)->setNodeHttpTunnelIdle(idle && reinterpret_cast<uWS::AsyncSocket<false>*>(socket)->hasFullyDrained(), tunnelReadEnded);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/jsc/bindings/node/JSNodeHTTPServerSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class JSNodeHTTPServerSocket : public JSC::JSDestructibleObject {
void readStop();
void readStart();
bool tunnelReadsPaused() const { return tunnelReadsStopped || tunnelReadsQueuedFull; }
/* Tells uWS whether this tunnel is idle: at read EOF with nothing left to send. See HttpResponse::setNodeHttpTunnelIdle. */
/* Tells uWS whether this tunnel is idle: it does not read (readStop() or read EOF) and has nothing left to send. See HttpResponse::setNodeHttpTunnelIdle. */
void updateTunnelIdle();
/* uWS still holds bytes of an HTTP response on this connection. A raw write has to go through the same buffer, or it reaches the wire first. */
bool hasUnsentResponseBytes() const;
Expand Down
19 changes: 12 additions & 7 deletions src/runtime/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ bitflags::bitflags! {
pub struct ServerFlags: u8 {
const DEINIT_SCHEDULED = 1 << 0;
const TERMINATED = 1 << 1;
/// `server.unref()` with no `server.ref()` behind it.
const UNREFED_BY_USER = 1 << 2;
}
}

Expand Down Expand Up @@ -267,7 +269,7 @@ pub(crate) struct NewServer<const SSL: bool, const DEBUG: bool> {
/// ([`NewServer::is_drained`]); for Bun.serve it also holds the
/// graceful-stop promise open ([`NewServer::is_closed`]).
pub(crate) active_connection_count: core::cell::Cell<u32>,
/// The node:http tunnels in `active_connection_count` that are at read EOF and have nothing left to send.
/// The node:http tunnels in `active_connection_count` that do not read (stopped or at EOF) and have nothing left to send.
pub(crate) idle_tunnel_count: core::cell::Cell<u32>,
/// Live `ServerWebSocket` count. Lives on the server (not the websocket
/// context) so a reload's context swap cannot reset it, and sits in a
Expand Down Expand Up @@ -506,7 +508,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
/// uWS filter: `+2` at TCP accept (before any TLS handshake), `-2` on
/// `HttpContext::onClose` / `HttpResponse::upgrade()` — see
/// `AsyncSocketData::filteredAccept`. Feeds [`Self::active_connection_count`].
/// `-3` / `+3`: a node:http tunnel becomes idle / has bytes to send again. `-4`: it closes idle.
/// `-3`: a node:http tunnel becomes idle. `+3` / `+4`: it is not idle any more, at read EOF / not at it. `-4`: it closes idle.
extern "C" fn on_connection_filter(
_socket: *mut uws_sys::us_socket_t,
opened: i32,
Expand All @@ -525,11 +527,14 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
this.note_connection_opened();
return;
}
3 => {
3 | 4 => {
this.note_tunnel_idle(false);
// With a listener open, the loop ref is the user's to drop (`server.unref()`).
if !this.has_listener() {
// SAFETY: `this` is not used again, and a tunnel write never runs inside `app.close()`.
// Behind `server.unref()` reads do not hold the loop, and only at read EOF is it sure that the tunnel has bytes to send.
if !this.has_listener()
&& (opened == 3 || !this.flags.contains(ServerFlags::UNREFED_BY_USER))
Comment thread
dylan-conway marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
{
// SAFETY: `this` is not used again, and a tunnel write or read start never runs inside `app.close()`.
unsafe { &mut *user_data.cast::<Self>() }.ref_();
}
return;
Expand Down Expand Up @@ -1606,7 +1611,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {

fn note_tunnel_idle(&self, idle: bool) {
let count = self.idle_tunnel_count.get();
// `AsyncSocketData::filteredIdleTunnel` pairs every -3 with one +3 or -4.
// `AsyncSocketData::filteredIdleTunnel` pairs every -3 with one +3, +4 or -4.
debug_assert!(idle || count > 0);
self.idle_tunnel_count.set(if idle {
count + 1
Expand All @@ -1615,7 +1620,7 @@ impl<const SSL: bool, const DEBUG: bool> NewServer<SSL, DEBUG> {
});
}

/// An idle tunnel is open but, like a libuv handle at EOF with no write pending, does not hold the loop.
/// An idle tunnel is open but, like a libuv handle that does not read and has no write pending, does not hold the loop.
fn has_loop_holding_connections(&self) -> bool {
self.active_connection_count.get() > self.idle_tunnel_count.get()
}
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/server/server_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2478,6 +2478,7 @@ where
callframe: &CallFrame,
) -> JsResult<JSValue> {
let this_value = callframe.this();
self.flags.remove(ServerFlags::UNREFED_BY_USER);
self.ref_();
Ok(this_value)
}
Expand All @@ -2489,6 +2490,7 @@ where
callframe: &CallFrame,
) -> JsResult<JSValue> {
let this_value = callframe.this();
self.flags.insert(ServerFlags::UNREFED_BY_USER);
self.unref();
Ok(this_value)
}
Expand Down
53 changes: 53 additions & 0 deletions test/js/node/http/node-http-connect-proxy-pipes-fixture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

151 changes: 151 additions & 0 deletions test/js/node/http/node-http-connect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,157 @@ test("a half-open tunnel with bytes left to send keeps the process alive until t
}
});

// A socket that nothing reads stops the reads of its handle when its buffer is full. In Node.js a
// handle that does not read and has nothing to send does not keep the process alive.
async function runToExit(...args: string[]) {
await using proc = Bun.spawn({
cmd: [bunExe(), ...args],
// A child that hangs exits with the test process.
env: { ...bunEnv, BUN_FEATURE_FLAG_NO_ORPHANS: "1" },
stdout: "pipe",
stderr: "pipe",
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
return { stdout, stderr, exitCode, signalCode: proc.signalCode };
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
const exitedWith = (result: object) => ({
stdout: JSON.stringify(result) + "\n",
stderr: "",
exitCode: 0,
signalCode: null,
});

test.concurrent("a CONNECT proxy by pipes exits after server.close() when its upstream went away", async () => {
// The client's socket is still open, so the proxy does not emit 'close'.
expect(await runToExit(join(import.meta.dir, "node-http-connect-proxy-pipes-fixture.js"))).toEqual(
exitedWith({ clientSocketIsPaused: true }),
);
});

// `listener` is the body of the listener for `event`. `peer` runs when `client` has the reply. The process prints `result`.
function runTunnel(proto: string, event: string, listener: string, peer = "client.write(full);") {
const request =
event === "connect"
? "CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n"
: "GET / HTTP/1.1\r\nHost: a\r\nConnection: Upgrade\r\nUpgrade: raw\r\n\r\n";
return runToExit(
"-e",
`const server = require("node:${proto}").createServer(${JSON.stringify(proto === "https" ? tlsCert : {})});
const reply = "HTTP/1.1 200 OK\\r\\n\\r\\n";
// What a socket buffers before it stops its reads.
const full = Buffer.alloc(65536, "a");
const result = {};
let client;
server.on("${event}", (req, socket) => {
${listener}
});
server.listen(0, "127.0.0.1", () => {
client = require("node:${proto === "https" ? "tls" : "net"}").connect({
port: server.address().port,
host: "127.0.0.1",
rejectUnauthorized: false,
allowHalfOpen: true,
});
// Only the server's side of the tunnel may keep the process alive.
client.unref();
client.write(${JSON.stringify(request)});
client.once("data", () => {
${peer}
});
});
process.on("exit", () => console.log(JSON.stringify(result)));`,
);
}
// More than a loopback socket takes in one write.
const large = 8 * 1024 * 1024;

describe.concurrent.each([
["http", "connect"],
["https", "connect"],
["http", "upgrade"],
["https", "upgrade"],
])("%s '%s': a tunnel whose reads stopped, after server.close()", (proto, event) => {
test("does not keep the process alive behind the listener's end()", async () => {
// No writable event comes for a socket behind its FIN.
const listener = `
server.close();
socket.end(reply);
process.on("beforeExit", () => (result.buffered = socket.readableLength));`;
expect(await runTunnel(proto, event, listener)).toEqual(exitedWith({ buffered: 65536 }));
});
Comment thread
dylan-conway marked this conversation as resolved.
Resolved

test("keeps the process alive again while it sends and when it reads", async () => {
const listener = `
server.close();
socket.write(reply);
Object.assign(result, { sent: false, serverReceived: 0, clientReceived: 0 });
// 'beforeExit': nothing keeps the process alive. It has to wait for what each step starts.
const steps = [
() => socket.write(Buffer.alloc(${large}, "x"), () => (result.sent = true)),
() => socket.on("data", chunk => (result.serverReceived += chunk.length)).on("end", () => socket.end()),
];
process.on("beforeExit", () => steps.shift()?.());`;
const peer = `
client.write(full);
client.on("data", chunk => {
if ((result.clientReceived += chunk.length) === ${large}) client.end(Buffer.alloc(${large}, "b"));
});`;
expect(await runTunnel(proto, event, listener, peer)).toEqual(
exitedWith({ sent: true, serverReceived: 65536 + large, clientReceived: large }),
);
});
});

// The server holds the event loop for its sockets, so server.unref() gives it up for their reads too.
// In Node.js a socket holds it by itself: there the process of the first test does not exit.
describe.concurrent.each(["http", "https"])("%s: a tunnel behind server.unref() and server.close()", proto => {
// `hold` keeps the process alive until the reads have stopped. Then the tunnel gets a reader.
const readsAgain = (ref: string, whenRead: string) => `
socket.write(reply);
server.unref();
${ref}
server.close();
const hold = setInterval(() => {
if (socket.readableLength === full.length) clearInterval(hold);
}, 1);
result.received = 0;
process.once("beforeExit", () => {
socket.on("data", chunk => {
if ((result.received += chunk.length) === full.length) {
${whenRead}
}
});
socket.on("end", () => {
result.end = true;
socket.end();
});
});`;

test("does not keep the process alive when it reads again", async () => {
// The peer stays connected and sends nothing more.
expect(await runTunnel(proto, "connect", readsAgain("", ""))).toEqual(exitedWith({ received: 65536 }));
});

test("keeps the process alive when it reads again behind server.ref()", async () => {
expect(await runTunnel(proto, "connect", readsAgain("server.ref();", "client.end();"))).toEqual(
exitedWith({ received: 65536, end: true }),
);
});

test("keeps the process alive while it sends at the end of the stream", async () => {
const listener = `
socket.write(reply);
server.unref();
server.close();
const hold = setInterval(() => {}, 1000);
socket.on("end", () => clearInterval(hold)).resume();
process.once("beforeExit", () => socket.end(Buffer.alloc(${large}, "x"), () => (result.sent = true)));`;
expect(await runTunnel(proto, "connect", listener, "client.end(); client.resume();")).toEqual(
exitedWith({ sent: true }),
);
});
});

// A listener with nothing to send ends its side in the listener. It still reads: an http.Server
// socket has allowHalfOpen. Every expectation below is Node v26.3.0's.
describe("a tunnel behind a request head that took more than one read", () => {
Expand Down
Loading