Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 31 additions & 13 deletions src/js/node/_http_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,19 +410,7 @@ function ClientRequest(input, options, cb) {
}));
setIsNextIncomingMessageHTTPS(prevIsHTTPS);
res.req = this;
let timer;
res.setTimeout = (msecs, callback) => {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
if (res.complete) {
return;
}
res.emit("timeout");
callback?.();
}, msecs);
};
res.setTimeout = clientResponseSetTimeout;
process.nextTick(
(self, res) => {
// If the user did not listen for the 'response' event, then they
Expand Down Expand Up @@ -1049,6 +1037,36 @@ function emitAbortNextTick(self) {
self.emit("abort");
}

const kResTimeoutTimer = Symbol("kResTimeoutTimer");

function onClientResponseTimeout(res) {
res[kResTimeoutTimer] = undefined;
if (res.complete) {
return;
}
res.emit("timeout");
}

// Assigned as res.setTimeout on client-side IncomingMessage instances. Kept at
// module scope so it doesn't close over ClientRequest locals and keep them
// alive for the lifetime of the response.
function clientResponseSetTimeout(msecs, callback) {
if (callback) {
this.on("timeout", callback);
}
Comment thread
robobun marked this conversation as resolved.
const existing = this[kResTimeoutTimer];
if (existing) {
clearTimeout(existing);
this[kResTimeoutTimer] = undefined;
}
if (msecs > 0) {
// Use an unref'd timer so an idle response timeout does not keep the
// event loop alive (matches Node's socket.setTimeout semantics).
this[kResTimeoutTimer] = setTimeout(onClientResponseTimeout, msecs, this).unref();
}
return this;
}

export default {
ClientRequest,
kBodyChunks,
Expand Down
62 changes: 62 additions & 0 deletions test/js/node/http/node-http-res-settimeout-unref.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { expect, test } from "bun:test";
import { bunEnv, bunExe } from "harness";
import { once } from "node:events";
import http from "node:http";
import type { AddressInfo } from "node:net";

test("client IncomingMessage.setTimeout does not keep the event loop alive", async () => {
// A child process makes a keep-alive http.get() and calls res.setTimeout(60000).
// Once the body is consumed there is nothing left to do, so the process must
// exit immediately instead of waiting for the timeout to fire. Node unrefs the
// socket timeout timer; Bun's res.setTimeout override must do the same.
const server = http.createServer((req, res) => {
res.setHeader("Connection", "keep-alive");
res.end("hello");
});
server.keepAliveTimeout = 60_000;
await once(server.listen(0, "127.0.0.1"), "listening");
try {
const { port } = server.address() as AddressInfo;

const script = `
const http = require("http");
const agent = new http.Agent({ keepAlive: true });
const req = http.get({ host: "127.0.0.1", port: ${port}, agent }, res => {
const ret = res.setTimeout(60000, () => {
process.stdout.write("TIMEOUT_FIRED\\n");
});
if (ret !== res) process.stdout.write("BAD_RETURN\\n");
let body = "";
res.on("data", d => (body += d));
res.on("end", () => {
process.stdout.write("end:" + body + "\\n");
});
});
req.end();
// If the res.setTimeout timer (or the idle keep-alive socket) is still
// ref'd after the response completes, this unref'd sentinel will fire
// and fail the test instead of letting it hang for 60s.
setTimeout(() => {
process.stdout.write("STILL_ALIVE\\n");
process.exit(1);
}, 2000).unref();
`;

await using proc = Bun.spawn({
cmd: [bunExe(), "-e", script],
env: bunEnv,
stdout: "pipe",
stderr: "pipe",
});
const [stdout, , exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]);

// Debug/ASAN builds print a startup banner to stderr; stdout === "end:hello"
// and exit 0 are sufficient — a ref'd timer would produce STILL_ALIVE on
// stdout and exit 1.
expect(stdout).toBe("end:hello\n");
expect(exitCode).toBe(0);
} finally {
server.closeAllConnections();
server.close();
}
});
Loading