-
Notifications
You must be signed in to change notification settings - Fork 4.7k
http2: give Http2Stream its own per-stream idle timer #30308
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
Open
robobun
wants to merge
2
commits into
main
Choose a base branch
from
farm/6e45c4d0/http2-per-stream-timer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,162 @@ | ||
| // https://github.com/oven-sh/bun/issues/30307 | ||
|
|
||
| import { describe, expect, it } from "bun:test"; | ||
| import { isASAN } from "harness"; | ||
| import { once } from "node:events"; | ||
| import http2 from "node:http2"; | ||
|
|
||
| // The ASAN lane is measurably slower than release; scale per-stream | ||
| // thresholds so a transient stall on a loaded CI box can't trip a | ||
| // correctly-cleared timer during the request. | ||
| const SCALE = isASAN ? 4 : 1; | ||
|
|
||
| describe("#30307", () => { | ||
| it("req.setTimeout does not fire on completed streams after a session-idle gap", async () => { | ||
| const server = http2.createServer(); | ||
| server.on("stream", stream => { | ||
| stream.respond({ ":status": 200 }); | ||
| stream.end("ok"); | ||
| }); | ||
|
|
||
| await new Promise<void>(resolve => server.listen(0, resolve)); | ||
| const port = (server.address() as import("node:net").AddressInfo).port; | ||
| const client = http2.connect(`http://localhost:${port}`); | ||
| try { | ||
| // Warm the session so subsequent requests return quickly and never | ||
| // approach the per-stream timeout during their own lifetime. | ||
| await once(client, "connect"); | ||
| { | ||
| const warmup = client.request({ ":path": "/" }); | ||
| warmup.resume(); | ||
| warmup.end(); | ||
| await once(warmup, "end"); | ||
| } | ||
|
|
||
| // The per-stream threshold must elapse during the idle gap below. | ||
| // The gap is driven by the session's own socket idle-timeout | ||
| // firing deterministically, so the timing is event-based. | ||
| const STREAM_TIMEOUT_MS = 150 * SCALE; | ||
| const IDLE_BARRIER_MS = 2 * STREAM_TIMEOUT_MS; | ||
|
|
||
| const timeoutFires: string[] = []; | ||
| async function doRequest(label: string) { | ||
| const req = client.request({ ":path": "/" }); | ||
| req.setTimeout(STREAM_TIMEOUT_MS, () => { | ||
| timeoutFires.push(label); | ||
| }); | ||
| req.resume(); | ||
| req.end(); | ||
| // once() rejects on 'error', so a stream failure surfaces at this | ||
| // await rather than as an uncaught emitter throw. | ||
| await once(req, "end"); | ||
| } | ||
|
|
||
| await doRequest("req-1"); | ||
| await doRequest("req-2"); | ||
| await doRequest("req-3"); | ||
| await doRequest("req-4"); | ||
|
|
||
| // Arm a session-level socket idle timeout as a deterministic | ||
| // barrier: wait for the session's own 'timeout' event rather than | ||
| // sleeping a fixed duration. By the time this fires, the per-stream | ||
| // threshold has elapsed for every completed stream above. On the | ||
| // buggy path the accumulated per-stream callbacks on the shared | ||
| // socket (or the session→streams cascade for completed-but-still- | ||
| // tracked streams) fire first and populate timeoutFires. | ||
| client.setTimeout(IDLE_BARRIER_MS); | ||
| await once(client, "timeout"); | ||
|
|
||
| // A follow-up request that also succeeds silently. | ||
| await doRequest("req-5"); | ||
|
|
||
| expect(timeoutFires).toEqual([]); | ||
| } finally { | ||
| client.close(); | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| it("session-level setTimeout does not emit 'timeout' on live streams", async () => { | ||
| // A server that never responds keeps client streams open, so they're | ||
| // still tracked by the session when the socket idle timer fires. | ||
| // On the buggy path, the session #onTimeout did | ||
| // `parser.forEachStream(emitTimeout)` and every live stream saw a | ||
| // spurious 'timeout' event. Per Node.js, session idle timeouts emit | ||
| // on the session only. | ||
| const server = http2.createServer(); | ||
| server.on("stream", _stream => { | ||
| // deliberately no response | ||
| }); | ||
|
|
||
| await new Promise<void>(resolve => server.listen(0, resolve)); | ||
| const port = (server.address() as import("node:net").AddressInfo).port; | ||
| const client = http2.connect(`http://localhost:${port}`); | ||
| try { | ||
| await once(client, "connect"); | ||
|
|
||
| const streamFired: string[] = []; | ||
| const req1 = client.request({ ":path": "/a" }); | ||
| const req2 = client.request({ ":path": "/b" }); | ||
| // Swallow the inevitable ERR_HTTP2_STREAM_ERROR on teardown so it | ||
| // doesn't surface as an uncaught stream error. | ||
| req1.on("error", () => {}); | ||
| req2.on("error", () => {}); | ||
| req1.on("timeout", () => streamFired.push("req1")); | ||
| req2.on("timeout", () => streamFired.push("req2")); | ||
| req1.end(); | ||
| req2.end(); | ||
|
|
||
| // Both streams are live (waiting for a response that never comes). | ||
| // Arm the session socket idle timer and wait deterministically for | ||
| // its 'timeout' event. No per-stream 'timeout' must fire. | ||
| client.setTimeout(150 * SCALE); | ||
| await once(client, "timeout"); | ||
|
|
||
| expect(streamFired).toEqual([]); | ||
|
|
||
| req1.close(http2.constants.NGHTTP2_CANCEL); | ||
| req2.close(http2.constants.NGHTTP2_CANCEL); | ||
| } finally { | ||
| client.close(); | ||
| server.close(); | ||
| } | ||
| }); | ||
|
|
||
| it("req.setTimeout does not fire on a completed stream whose body is never read", async () => { | ||
| // A clean END_STREAM response with a buffered, never-consumed body takes | ||
| // the client's deferred-destroy path: streamEnd calls markStreamClosed but | ||
| // waits for the reader to drain before destroying, so _destroy may never | ||
| // run. The per-stream timer must still be disarmed at the close | ||
| // transition (markStreamClosed), not only in _destroy. | ||
| const server = http2.createServer(); | ||
| server.on("stream", stream => { | ||
| stream.respond({ ":status": 200 }); | ||
| stream.end("a response body the client never reads"); | ||
| }); | ||
|
|
||
| await new Promise<void>(resolve => server.listen(0, resolve)); | ||
| const port = (server.address() as import("node:net").AddressInfo).port; | ||
| const client = http2.connect(`http://localhost:${port}`); | ||
| try { | ||
| await once(client, "connect"); | ||
|
|
||
| const fired: string[] = []; | ||
| const req = client.request({ ":path": "/" }); | ||
| req.on("error", () => {}); | ||
| req.setTimeout(150 * SCALE, () => fired.push("req")); | ||
| req.end(); | ||
| // Deliberately never resume()/read the body: the response stays buffered | ||
| // and the stream's _destroy is deferred until a consumer drains it. | ||
|
|
||
| // Barrier: a session idle-timeout at 2x the per-stream timeout. If the | ||
| // stream timer were left armed it would fire (at 1x) well before this. | ||
| client.setTimeout(2 * 150 * SCALE); | ||
| await once(client, "timeout"); | ||
|
|
||
| expect(fired).toEqual([]); | ||
| } finally { | ||
| client.close(); | ||
| 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.