Skip to content
Closed
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
2 changes: 0 additions & 2 deletions src/js/internal/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ const serverSymbol = Symbol.for("::bunternal::");
const kPendingCallbacks = Symbol("pendingCallbacks");
const kRequest = Symbol("request");
const kCloseCallback = Symbol("closeCallback");
const kDeferredTimeouts = Symbol("deferredTimeouts");

const kEmptyObject = Object.freeze(Object.create(null));

Expand Down Expand Up @@ -543,7 +542,6 @@ export {
kBodyChunks,
kClearTimeout,
kCloseCallback,
kDeferredTimeouts,
kDeprecatedReplySymbol,
kEmitState,
kEmptyObject,
Expand Down
39 changes: 26 additions & 13 deletions src/js/node/_http_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ const {
kRealListen,
tlsSymbol,
optionsSymbol,
kDeferredTimeouts,
kDeprecatedReplySymbol,
headerStateSymbol,
NodeHTTPHeaderState,
Expand Down Expand Up @@ -75,6 +74,7 @@ const OutgoingMessagePrototype = OutgoingMessage.prototype;
const { kIncomingMessage } = require("node:_http_common");
const kConnectionsCheckingInterval = Symbol("http.server.connectionsCheckingInterval");
const kTrackedConnections = Symbol("http.server.trackedConnections");
const kTimeout = Symbol("http.server.timeout");

// node.http trace events ('http.server.request' b/e). The agent module is
// only created on the first request, and emission is gated per-request on the
Expand Down Expand Up @@ -320,6 +320,7 @@ function Server(options, callback): void {

this[optionsSymbol] = options;
storeHTTPOptions.$call(this, options);
this[kTimeout] = 0;

if (callback) this.on("request", callback);
return this;
Expand Down Expand Up @@ -920,25 +921,37 @@ Server.prototype[kRealListen] = function (tls, port, host, socketPath, reusePort
this.once("listening", onListen);
}

if (this[kDeferredTimeouts]) {
for (const { msecs, callback } of this[kDeferredTimeouts]) {
this.setTimeout(msecs, callback);
}
delete this[kDeferredTimeouts];
const timeoutMs = this[kTimeout];
if (typeof timeoutMs === "number" && timeoutMs > 0) {
setServerIdleTimeout(this[serverSymbol], Math.ceil(timeoutMs / 1000));
}

setTimeout(emitListeningNextTick, 1, this, this[serverSymbol]?.hostname, this[serverSymbol]?.port);
}
};

// Node.js applies `this.timeout` to every new socket in connectionListener,
// so `server.timeout = N` and `server.setTimeout(N)` are interchangeable; the
// accessor keeps that equivalence on top of Bun's server-wide idle timeout.
Object.defineProperty(Server.prototype, "timeout", {
enumerable: true,
configurable: true,
get() {
return this[kTimeout];
},
set(msecs) {
this[kTimeout] = msecs;
const server = this[serverSymbol];
if (server) {
const seconds = typeof msecs === "number" && msecs > 0 ? Math.ceil(msecs / 1000) : 0;
setServerIdleTimeout(server, seconds);
}
},
});

Server.prototype.setTimeout = function (msecs, callback) {
const server = this[serverSymbol];
if (server) {
setServerIdleTimeout(server, Math.ceil(msecs / 1000));
if (typeof callback === "function") this.once("timeout", callback);
} else {
(this[kDeferredTimeouts] ??= []).push({ msecs, callback });
}
this.timeout = msecs;
if (typeof callback === "function") this.on("timeout", callback);
return this;
};

Expand Down
58 changes: 58 additions & 0 deletions test/js/node/http/node-http-server-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect, test } from "bun:test";
import { once } from "node:events";
import http from "node:http";
import net from "node:net";

test("http.Server: server.timeout defaults to 0 and reflects setTimeout()", () => {
const srv = http.createServer(() => {});
try {
expect(srv.timeout).toBe(0);

const returned = srv.setTimeout(1234);
expect(returned).toBe(srv);
expect(srv.timeout).toBe(1234);

srv.timeout = 4321;
expect(srv.timeout).toBe(4321);

srv.setTimeout(0);
expect(srv.timeout).toBe(0);
} finally {
srv.close();
}
});

// Node.js treats `server.timeout = N` and `server.setTimeout(N)` as the same
// knob; assigning the property must apply the idle timeout to subsequent
// sockets so a stalled connection is eventually closed.
test("http.Server: assigning server.timeout closes a stalled connection", async () => {
const srv = http.createServer((req, res) => {
req.resume();
req.on("end", () => res.end("ok"));
});
try {
srv.listen(0, "127.0.0.1");
await once(srv, "listening");

srv.timeout = 500;
expect(srv.timeout).toBe(500);

const addr = srv.address() as net.AddressInfo;
const client = net.connect(addr.port, "127.0.0.1");
client.setNoDelay(true);
client.on("error", () => {});
client.resume();
await once(client, "connect");
// Complete request head, then stall the body.
client.write("POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 900\r\n\r\nab");

const [timedOutSocket] = (await once(srv, "timeout")) as [net.Socket];
timedOutSocket.destroy();
await once(client, "close");

client.destroy();
} finally {
srv.closeAllConnections?.();
srv.close();
}
}, 20_000);

Check warning on line 58 in test/js/node/http/node-http-server-timeout.test.ts

View check run for this annotation

Claude / Claude Code Review

Explicit per-test timeout violates test/CLAUDE.md convention

nit: test/CLAUDE.md says "Do not set a timeout on tests. Bun already has timeouts" — the `20_000` third argument here can be dropped. (Several sibling files in this directory do the same, so this is a minor convention nit rather than anything functional.)
Comment thread
robobun marked this conversation as resolved.
Resolved
Loading