Repository navigation
node:http: let server 'timeout' listeners veto the default socket destroy #33735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5649f31
node:http: let server 'timeout' listeners veto the default socket des…
robobun b51bf87
[autofix.ci] apply automated fixes
autofix-ci[bot] 36e1b22
Address review: refresh idle timer on response writes and drop dead s…
robobun 6b05062
ci: retrigger
robobun 4398e28
Address review: unify req.setTimeout with the socket timer; arm befor…
robobun 4dec0ed
test: destroy the socket in server/req.setTimeout callbacks
robobun d066a09
Address review: progress-based _onTimeout guard, isSocketNew-gated ar…
robobun 2f5af30
Drop the now-unused setRequestTimeout binding chain
robobun 3d84d87
[autofix.ci] apply automated fixes
autofix-ci[bot] 06dcdc1
Validate msecs via getTimerDuration; drop orphaned Request::set_timeout
robobun b7f981c
Validate socket.setTimeout callback via validateFunction, matching ne…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { test, expect, describe } from "bun:test"; | ||
| import http from "node:http"; | ||
| import net from "node:net"; | ||
| import { once } from "node:events"; | ||
|
|
||
| // https://nodejs.org/api/http.html#serversettimeoutmsecs-callback | ||
| // Once a 'timeout' listener is installed on the server (or on req/res), the | ||
| // runtime must NOT destroy the socket on timeout; the listener decides. | ||
|
|
||
| async function connectAndWrite(port: number, wire: string) { | ||
| const s = net.connect(port, "127.0.0.1"); | ||
| s.setNoDelay(true); | ||
| s.on("error", () => {}); | ||
| s.resume(); | ||
| await once(s, "connect"); | ||
| s.write(wire); | ||
| return s; | ||
| } | ||
|
|
||
| async function listen(handler: http.RequestListener) { | ||
| const srv = http.createServer(handler); | ||
| srv.listen(0, "127.0.0.1"); | ||
| await once(srv, "listening"); | ||
| return srv; | ||
| } | ||
|
|
||
| function cleanup(srv: http.Server, client: net.Socket) { | ||
| try { | ||
| client.destroy(); | ||
| } catch {} | ||
| try { | ||
| srv.close(); | ||
| srv.closeAllConnections?.(); | ||
| } catch {} | ||
| } | ||
|
|
||
| describe("http.Server.prototype.setTimeout", () => { | ||
| test("sets server.timeout and returns this", () => { | ||
| const srv = http.createServer(); | ||
| expect(srv.timeout).toBe(0); | ||
| const ret = srv.setTimeout(1234); | ||
| expect(ret).toBe(srv); | ||
| expect(srv.timeout).toBe(1234); | ||
| }); | ||
|
|
||
| test("callback is registered as a 'timeout' listener", () => { | ||
| const srv = http.createServer(); | ||
| const cb = () => {}; | ||
| srv.setTimeout(1234, cb); | ||
| expect(srv.listeners("timeout")).toContain(cb); | ||
| }); | ||
|
|
||
| test.concurrent("a server 'timeout' listener vetoes the default destroy (stalled body)", async () => { | ||
| const srv = await listen((req, _res) => void req.resume()); | ||
| srv.setTimeout(200); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers<{ event: string; socket?: any }>(); | ||
| srv.on("timeout", socket => resolve({ event: "timeout", socket })); | ||
|
|
||
| const client = await connectAndWrite( | ||
| (srv.address() as net.AddressInfo).port, | ||
| "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 900\r\n\r\nab", | ||
| ); | ||
| client.on("close", () => resolve({ event: "close" })); | ||
|
|
||
| try { | ||
| const { event, socket } = await promise; | ||
| // The 'timeout' event must fire before the connection is torn down. | ||
| expect(event).toBe("timeout"); | ||
| expect(socket.destroyed).toBe(false); | ||
| // And the connection must still be open afterwards: the listener did | ||
| // not destroy, so the runtime must not either. Race a fresh 'timeout' | ||
| // (listener returning without destroying leaves the socket alive; the | ||
| // timer is one-shot until refreshed, but the old native path closed | ||
| // within a few ms of the event) against 'close'. | ||
| const winner = await Promise.race([ | ||
| once(client, "close").then(() => "close"), | ||
| Bun.sleep(250).then(() => "alive"), | ||
| ]); | ||
| expect(winner).toBe("alive"); | ||
| expect(socket.destroyed).toBe(false); | ||
| } finally { | ||
| cleanup(srv, client); | ||
| } | ||
| }); | ||
|
|
||
| test.concurrent("a server 'timeout' listener vetoes the default destroy (idle keep-alive)", async () => { | ||
| const srv = await listen((req, res) => { | ||
| req.resume(); | ||
| req.on("end", () => res.end("ok")); | ||
| }); | ||
| srv.setTimeout(200); | ||
|
|
||
| const { promise, resolve } = Promise.withResolvers<{ event: string; socket?: any }>(); | ||
| srv.on("timeout", socket => resolve({ event: "timeout", socket })); | ||
|
|
||
| const client = await connectAndWrite((srv.address() as net.AddressInfo).port, "GET / HTTP/1.1\r\nHost: a\r\n\r\n"); | ||
| client.on("close", () => resolve({ event: "close" })); | ||
|
|
||
| try { | ||
| const { event, socket } = await promise; | ||
| // An idle keep-alive connection reaped by the server timeout must | ||
| // emit 'timeout' (not close silently). | ||
| expect(event).toBe("timeout"); | ||
| expect(socket.destroyed).toBe(false); | ||
| const winner = await Promise.race([ | ||
| once(client, "close").then(() => "close"), | ||
| Bun.sleep(250).then(() => "alive"), | ||
| ]); | ||
| expect(winner).toBe("alive"); | ||
| expect(socket.destroyed).toBe(false); | ||
| } finally { | ||
| cleanup(srv, client); | ||
| } | ||
| }); | ||
|
|
||
| test.concurrent("with no listener, the socket is destroyed on timeout", async () => { | ||
| const srv = await listen((req, _res) => void req.resume()); | ||
| srv.setTimeout(200); | ||
|
|
||
| const client = await connectAndWrite( | ||
| (srv.address() as net.AddressInfo).port, | ||
| "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 900\r\n\r\nab", | ||
| ); | ||
| try { | ||
| await once(client, "close"); | ||
| } finally { | ||
| cleanup(srv, client); | ||
| } | ||
| }); | ||
|
|
||
| test.concurrent("a request 'timeout' listener also vetoes the default destroy", async () => { | ||
| const { promise, resolve } = Promise.withResolvers<{ event: string; socket?: any }>(); | ||
| const srv = await listen((req, _res) => { | ||
| req.resume(); | ||
| req.on("timeout", () => resolve({ event: "timeout", socket: req.socket })); | ||
| }); | ||
| srv.setTimeout(200); | ||
|
|
||
| const client = await connectAndWrite( | ||
| (srv.address() as net.AddressInfo).port, | ||
| "POST / HTTP/1.1\r\nHost: a\r\nContent-Length: 900\r\n\r\nab", | ||
| ); | ||
| client.on("close", () => resolve({ event: "close" })); | ||
|
|
||
| try { | ||
| const { event, socket } = await promise; | ||
| expect(event).toBe("timeout"); | ||
| expect(socket.destroyed).toBe(false); | ||
| const winner = await Promise.race([ | ||
| once(client, "close").then(() => "close"), | ||
| Bun.sleep(250).then(() => "alive"), | ||
| ]); | ||
| expect(winner).toBe("alive"); | ||
| } finally { | ||
| cleanup(srv, client); | ||
| } | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.