Repository navigation
http: unref client IncomingMessage.setTimeout timer #29701
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
Merged
Jarred-Sumner
merged 6 commits into
main
from
farm/592ac96b/http-client-res-settimeout-unref
Apr 26, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e7e44c8
http: unref client IncomingMessage.setTimeout timer
robobun a8fcf82
test: drop weak stderr check; stdout+exitCode are sufficient
robobun f98aad5
test: move res.setTimeout unref test to its own file
robobun 78452b1
Merge branch 'main' into farm/592ac96b/http-client-res-settimeout-unref
dylan-conway b1cfc41
Merge branch 'main' into farm/592ac96b/http-client-res-settimeout-unref
dylan-conway 925ef69
http: hoist client res.setTimeout out of ClientRequest closure
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
| 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(); | ||
| } | ||
| }); |
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.