Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 13 additions & 4 deletions packages/bun-uws/src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -752,15 +752,24 @@ struct HttpContext {
/* CONNECT/Upgrade tunnels allow half-open: the peer finishing its
* writable side ends the JS socket's readable side ('end' event) but
* the server can keep writing until it ends the socket itself, like
* Node's http server (allowHalfOpen: true). This includes an accepted
* Upgrade whose body never completed (HTTP_NODE_TUNNEL_AFTER_BODY): the
* EOF ends the upgrade socket, exactly like Node's UpgradeStream. */
if (httpResponseData->isConnectRequest || (httpResponseData->state & HttpResponseData<SSL>::HTTP_NODE_TUNNEL_AFTER_BODY)) {
* Node's http server (allowHalfOpen: true). */
if (httpResponseData->isConnectRequest) {
if (httpResponseData->socketData && httpContextData->onSocketData) {
httpContextData->onSocketData(httpResponseData->socketData, SSL, s, "", 0, true);
}
return s;
}
/* An accepted Upgrade whose body never completed is not a tunnel yet:
* Node's UpgradeStream wraps a socket that still has socketOnEnd
* attached, so a mid-body FIN ends the raw socket and the
* UpgradeStream destroys with it. Staying half-open here stranded
* the response's body-read ref and the server's pending-request
* count. onClose() runs the tunnel-after-body cleanup (socketData
Comment thread
robobun marked this conversation as resolved.
Resolved
* last=true + inStream last=true). */
if (httpResponseData->state & HttpResponseData<SSL>::HTTP_NODE_TUNNEL_AFTER_BODY) {
asyncSocket->uncorkWithoutSending();
return asyncSocket->close();
}

if (httpContextData->onClientError && !(httpResponseData->state & HttpResponseData<SSL>::HTTP_NODE_PARSING_STOPPED)
&& (httpResponseData->hasBufferedPartialRequestHeaders()
Expand Down
51 changes: 51 additions & 0 deletions test/js/node/http/node-http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3467,6 +3467,57 @@
await closed;
});

it("upgrade with a body: a mid-body client FIN closes the upgraded socket and lets the process exit", async () => {
// Node's UpgradeStream wraps a socket that still has socketOnEnd attached, so
// a client FIN before the request body completes closes the connection instead
// of staying half-open. Staying half-open stranded the server's pending-request
// accounting and the body-read ref, hanging the process.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
const fixture = /* js */ `
const http = require("node:http");
const net = require("node:net");
const { once } = require("node:events");

(async () => {
const server = http.createServer();
let upgraded;
const gotUpgrade = new Promise(r => { upgraded = r; });
const events = [];
server.on("upgrade", (req, socket, head) => {
events.push("upgrade");
req.on("data", () => {});
socket.on("end", () => events.push("end"));
socket.on("close", () => events.push("close"));
upgraded(socket);
});
server.listen(0, "127.0.0.1");
await once(server, "listening");

const c = net.connect(server.address().port, "127.0.0.1");
await once(c, "connect");
c.write("GET / HTTP/1.1\\r\\nHost: x\\r\\nUpgrade: foo\\r\\nConnection: Upgrade\\r\\nContent-Length: 100\\r\\n\\r\\npartial");
const upgradeSocket = await gotUpgrade;
// Half-close the client; the body (100 bytes) is never completed.
c.end();
await once(upgradeSocket, "close");

await new Promise((resolve, reject) => {
server.close(err => err ? reject(err) : resolve());
});
console.log(JSON.stringify(events));
})().catch(err => { console.error(err); process.exit(1); });
`;
await using proc = Bun.spawn({
cmd: [bunExe(), "-e", fixture],
env: bunEnv,
stderr: "pipe",
timeout: 10_000,
});
const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);
expect(stderr).toBe("");

Check warning on line 3516 in test/js/node/http/node-http.test.ts

View check run for this annotation

Claude / Claude Code Review

Test asserts stderr is exactly empty

Nit: `expect(stderr).toBe("")` will spuriously fail on ASAN/debug builds where the runtime writes benign warnings to stderr. Per REVIEW.md, prefer asserting a combined `{ stdout, stderr, exitCode }` object (or check stderr for the absence of specific error markers) rather than requiring stderr to be exactly empty.
Comment thread
robobun marked this conversation as resolved.
Resolved
expect(stdout.trim()).toBe(JSON.stringify(["upgrade", "end", "close"]));
expect(exitCode).toBe(0);
}, 15_000);

it("req.upgrade is true inside the 'connect' listener", async () => {
let upgradeValue: unknown = "unset";
const { promise: sawConnect, resolve: onConnect } = Promise.withResolvers<void>();
Expand Down
Loading