diff --git a/src/runtime/server/NodeHTTPResponse.rs b/src/runtime/server/NodeHTTPResponse.rs index 0a8c89431153..4ddf2d0514ac 100644 --- a/src/runtime/server/NodeHTTPResponse.rs +++ b/src/runtime/server/NodeHTTPResponse.rs @@ -611,6 +611,10 @@ impl NodeHTTPResponse { // and will have its own lifecycle management let vm = self.server.global_this().bun_vm().as_mut(); self.poll_ref.with_mut(|r| r.unref(vm)); + // Once uWS adopts the socket, a body that is still pending never arrives. + if self.body_read_state.get() == BodyReadState::Pending { + self.body_read_ref.with_mut(|r| r.unref(vm)); + } // S008: `WebSocketUpgradeContext` is an `opaque_ffi!` ZST — safe deref // (`upgrade_ctx` checked non-null above). let ctx = bun_opaque::opaque_deref_mut(upgrade_ctx); @@ -2299,7 +2303,7 @@ impl NodeHTTPResponse { self.update_flags(|f| f.remove(Flags::IS_DATA_BUFFERED_DURING_PAUSE)); // Every site that unrefs `body_read_ref` also transitions `body_read_state` out of `.pending` - // or sets `is_data_buffered_during_pause_last`, both of which are rejected by the guard above. + // or sets `is_data_buffered_during_pause_last` or `upgraded`, all of which are rejected by the guard above. // So reaching here, `body_read_ref` is still held from create(). Do not re-acquire it or // `this.ref()` — there would be no balancing release (PR #18564 removed the paired derefs). debug_assert!(self.body_read_ref.get().has); diff --git a/test/js/node/http/node-http-with-ws.test.ts b/test/js/node/http/node-http-with-ws.test.ts index 0ab97a4b1d26..691435642078 100644 --- a/test/js/node/http/node-http-with-ws.test.ts +++ b/test/js/node/http/node-http-with-ws.test.ts @@ -162,3 +162,109 @@ describe.concurrent("request handlers run to completion before the callbacks the expect(order).toEqual(["rest of handler", "nextTick", "microtask"]); }); }); + +// An Upgrade request can declare a body. The upgrade hands the socket to the WebSocket, so a body +// that has not arrived never does, and a keep-alive ref that waits for it never lets the process exit. +// Not concurrent: a debug build needs most of the default timeout to start this child, and more beside another. +test("WebSocket upgrade should unref body_read_ref from response", async () => { + const script = /* js */ ` + const http = require("http"); + const net = require("net"); + const { once } = require("events"); + const { WebSocketServer } = require("ws"); + const { getEventLoopStats } = require("bun:internal-for-testing"); + + async function upgradeRequestThatDeclaresABody(upgradeInLaterTask, sendBody) { + const tasksBefore = getEventLoopStats().activeTasks; + let request; + let requestEnded = false; + // A request that is not taken for an upgrade gets an answer too, so the client never waits. + const server = http.createServer((req, res) => res.writeHead(426).end()); + const wsServer = new WebSocketServer({ noServer: true }); + server.on("upgrade", (req, socket, head) => { + const upgrade = () => { + wsServer.handleUpgrade(req, socket, head, () => {}); + request = req.on("end", () => (requestEnded = true)).resume(); + }; + if (upgradeInLaterTask) setImmediate(upgrade); + else upgrade(); + }); + await once(server.listen(0, "127.0.0.1"), "listening"); + + const body = '{"hello":"world"}'; + const client = net.connect(server.address().port, "127.0.0.1"); + client.write( + [ + "GET / HTTP/1.1", + "Host: localhost", + "Connection: Upgrade", + "Upgrade: websocket", + "Sec-WebSocket-Version: 13", + "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==", + "Content-Length: " + body.length, + "", + sendBody ? body : "", + ].join("\\r\\n"), + ); + const { promise, resolve, reject } = Promise.withResolvers(); + let received = ""; + client.on("data", chunk => { + received += chunk; + if (!received.includes("\\r\\n\\r\\n")) return; + // The response is in, so the upgrade and the callbacks it queued are over. + const leakedTasks = getEventLoopStats().activeTasks - tasksBefore; + const observed = { status: received.split("\\r\\n")[0], leakedTasks }; + // As in Node, a request whose body never arrives stays open. The upgrade does not end it. + if (!sendBody) observed.requestOpen = !requestEnded && !request.complete; + resolve(observed); + }); + client.on("error", reject); + client.on("close", () => reject(new Error("closed before the response: " + JSON.stringify(received)))); + const result = await promise; + + client.destroy(); + wsServer.close(); + await once(server.close(), "close"); + return result; + } + + const results = { + "in a later task, body not sent": await upgradeRequestThatDeclaresABody(true, false), + // The body is complete before the upgrade: there is nothing left for the upgrade to release. + "in a later task, body sent": await upgradeRequestThatDeclaresABody(true, true), + "in the 'upgrade' event, body sent": await upgradeRequestThatDeclaresABody(false, true), + "in the 'upgrade' event, body not sent": await upgradeRequestThatDeclaresABody(false, false), + }; + console.log(JSON.stringify(results)); + // A leaked task keeps the event loop alive forever. Exit, so that the test fails fast. + if (Object.values(results).some(result => result.leakedTasks !== 0)) process.exit(1); + `; + + await using proc = Bun.spawn({ + cmd: [bunExe(), "-e", script], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + + const upgraded = { status: "HTTP/1.1 101 Switching Protocols", leakedTasks: 0 }; + expect({ + results: stdout.startsWith("{") ? JSON.parse(stdout) : stdout, + stderr, + exitCode, + signalCode: proc.signalCode, + }).toEqual({ + results: { + "in a later task, body not sent": { ...upgraded, requestOpen: true }, + "in a later task, body sent": upgraded, + "in the 'upgrade' event, body sent": upgraded, + "in the 'upgrade' event, body not sent": { ...upgraded, requestOpen: true }, + }, + stderr: "", + // The script calls process.exit() only for a leak: the process has to exit by itself. + exitCode: 0, + signalCode: null, + }); +});