Repository navigation
node:http2: release completed streams from the session map - #31359
Jarred-Sumner wants to merge 17 commits into
Conversation
Streams that reach the CLOSED state are now removed from the
H2FrameParser stream map (and their allocations freed) once the current
native entry point unwinds, instead of being retained until the
connection is torn down. Long-lived connections no longer accumulate one
retained stream per completed request, and the session memory accounting
now reflects what is actually held.
Inbound frames and host functions that reference an already-released
stream id take the lenient path RFC 9113 prescribes for closed streams:
header blocks are still fed through the HPACK decoder to keep the
dynamic table in sync, DATA payloads still count against connection flow
control, and the rest is discarded. A bounded record of recently
released ids prevents a late frame from re-opening a stream while still
allowing genuinely new lower-numbered stream ids.
Also fixes the server-side respond({ endStream: true }) path leaving the
stream in HALF_CLOSED_LOCAL when the peer had already half-closed.
Adds a live-stream gauge to bun:internal-for-testing plus regression
tests for eviction, the late-response race, and late trailing HEADERS.
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughThis PR adds re-entrancy-scoped deferred eviction for HTTP/2 streams, centralizes stream closure via close_stream(), discards late frames for evicted streams while keeping HPACK state synchronized, exposes LIVE_STREAM_COUNT for testing, and adds fixture and raw-socket tests covering late/split-frame regressions. ChangesStream Eviction, Safety, and Testing
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Comment |
|
Updated 6:50 PM PT - May 28th, 2026
✅ @Jarred-Sumner, your commit d701b0a4985e970c0535f7c1afd2e64d9b1f5c2c passed in 🧪 To try this PR locally: bunx bun-pr 31359That installs a local version of the PR into your bun-31359 --bun |
|
Found 3 issues this PR may fix:
🤖 Generated with Claude Code |
|
This PR may be a duplicate of:
🤖 Generated with Claude Code |
… default closed-stream state
…urn closed-stream defaults for evicted ids
…ject unexpected continuation frames
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/node/http2/node-http2.test.js`:
- Around line 2115-2123: The test assumes the 24-byte HTTP/2 client preface
arrives in a single chunk (prefaceSeen handling) which is flaky; change the
logic that reads the preface to accumulate bytes across data events until you
have at least 24 bytes (introduce or reuse a buffer like a prefaceBuffer that
appends incoming chunk data), only then set prefaceSeen=true, write the
SettingsFrame(s) via socket.write(new http2utils.SettingsFrame(...).data),
consume the first 24 bytes from the accumulated buffer (e.g., chunk =
accumulated.subarray(24)), and continue parsing frames; do not reject on
chunk.length < 24 but instead store and wait for more data.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: ee30e71a-33a7-453d-a304-bd51ced0fa46
📒 Files selected for processing (5)
src/js/internal-for-testing.tssrc/runtime/api.rssrc/runtime/api/bun/h2_frame_parser.rstest/js/node/http2/node-http2-evict-closed-streams.fixture.jstest/js/node/http2/node-http2.test.js
👮 Files not reviewed due to content moderation or server errors (1)
- src/runtime/api/bun/h2_frame_parser.rs
…fer the test preface across chunks
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/runtime/api/bun/h2_frame_parser.rs`:
- Around line 2246-2251: The code is retaining CLOSED streams in the live
streams map which allows subsequent CONTINUATION frames to be processed (via
lookup_inbound_stream(), handle_continuation_frame(), decode_header_block()) and
dispatch onStreamHeaders after closure; change the logic so that when
stream.is_waiting_more_headers is true but the stream state is CLOSED you do NOT
keep it on the normal streams path — instead move it to a discard-only path
(e.g., a separate discard_streams map or set a stream.discarding flag) and set
has_closed_streams; update lookup_inbound_stream() to return a discard-only stub
for such stream IDs so handle_continuation_frame()/decode_header_block() will
drain/ignore header bytes and never call onStreamHeaders for closed streams.
- Around line 4507-4510: When handling a remote END_STREAM while
stream.is_waiting_more_headers is true, preserve a double-closed state instead
of overwriting HALF_CLOSED_LOCAL: in the branch that currently sets stream.state
= StreamState::HALF_CLOSED_REMOTE, change it to check if stream.state ==
StreamState::HALF_CLOSED_LOCAL and if so set stream.state = StreamState::CLOSED,
otherwise set it to HALF_CLOSED_REMOTE; keep the existing guard that skips
changes for StreamState::CLOSED. This ensures split END_STREAM trailers result
in CLOSED (double-closed) and works correctly with handle_continuation_frame(),
end_after_headers, and evict_closed_streams/END_HEADERS logic.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 9876ae17-9ef9-4c75-9975-98ebd6556c8f
📒 Files selected for processing (2)
src/runtime/api/bun/h2_frame_parser.rstest/js/node/http2/node-http2.test.js
There was a problem hiding this comment.
All prior review feedback has been addressed and I found nothing further, but this is a large change to the HTTP/2 frame parser's stream-lifecycle and unsafe memory management (deferred eviction, re-entrancy guards, raw *mut Stream freeing) that went through several rounds of fixes — worth a human pass before merging.
Extended reasoning...
Overview
This PR reworks how H2FrameParser manages per-stream state over the life of a connection: CLOSED streams are now evicted from the session HashMap<u32, *mut Stream> and freed once the outermost native entry point unwinds, instead of being retained until connection teardown. To make that safe it introduces an ActivityGuard re-entrancy counter taken at the top of ~25 host fns and 3 socket callbacks, a bounded recently-evicted ring buffer, lenient closed-stream fallbacks at every host-fn map miss, a discard path that still feeds late HEADERS/CONTINUATION through the HPACK decoder, a close_on_local_finish deferral so respond({endStream:true}) doesn't tear down mid-_final, and != CLOSED guards against resurrecting streams from post-dispatch state transitions. ~550 net lines in h2_frame_parser.rs, plus a process-global LIVE_STREAM_COUNT gauge exposed via bun:internal-for-testing and three new tests.
Security risks
No new attack surface in the usual sense — the parser already accepted arbitrary peer frames. The risk profile here is memory safety: evict_closed_streams does unsafe { heap::take(ptr) } on raw *mut Stream pointers pulled from a map that re-entrant JS callbacks can also reach, and correctness depends entirely on the activity_depth == 0 invariant holding across every entry point. The PR also changes how malformed/late peer frames are handled (silently discarded vs. GOAWAY), which is protocol-conformance rather than security-sensitive.
Level of scrutiny
High. This is production-critical node:http2 runtime code with unsafe Rust pointer/lifetime management, and the review history itself demonstrates the subtlety: it went through six rounds of fixes including a deterministic CI regression in the compat-layer (respond({endStream:true}) early-detach), a connection-fatal GOAWAY escalation from the CONTINUATION gate, a ring-overflow phantom-stream reallocation, and a keepalive-ordering gap in on_native_writable. All of those are now addressed and the current bug-hunting pass found nothing further, but the density of edge cases argues for a human reviewer confirming the final shape.
Other factors
- All eight of my prior inline findings are resolved; the author re-ran the five
test-http2-compat-*regressions, continuation/rehash, and node-http2.test.js after each fix. - robobun reported failures on build #57706 at 74be464; one further commit (ea02504) landed after that — CI status at HEAD is not visible in the thread.
- The author explicitly documented a residual window in the recently-evicted ring approach (chosen over the watermark to avoid breaking out-of-order client stream-id allocation); that's a deliberate trade-off a maintainer should sign off on.
- New test coverage is solid (live-count gauge fixture + two raw-socket cancel-race tests covering HPACK dynamic-table sync and phantom-stream prevention).
…eaders for closed ones When an inbound HEADERS frame carries END_STREAM but not END_HEADERS on a stream we already finished sending on, the half-closed-local state was overwritten with half-closed-remote and the continuation handler never revisited it, so the stream could never be released. Close it there instead. A closed stream can still own an unfinished header block (it stays in the map until END_HEADERS so the HPACK dynamic table stays in sync); decode the remaining fragments but do not dispatch them to JS.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@test/js/node/http2/node-http2.test.js`:
- Line 2288: The CONTINUATION frame constructed with
http2utils.ContinuationFrame(1, trailerFragment2, 0, false) is missing the
END_HEADERS flag which leaves the HPACK decoder waiting and hangs the test;
update the fourth argument to true so the CONTINUATION frame signals end of
header block (match the pattern used where true is passed in the earlier
CONTINUATION usage) to allow the trailers event to complete.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: fcf4e63f-d1f1-4082-a51b-2aa9c48a17ea
📒 Files selected for processing (2)
src/runtime/api/bun/h2_frame_parser.rstest/js/node/http2/node-http2.test.js
…reams-never-evicted
…the outbound flag
Buffer HEADERS/CONTINUATION fragments for a discarded header block until END_HEADERS and run the HPACK decoder once over the assembled block, matching the live path. Track the in-progress discarded block with the same continuation state the frame dispatcher already checks.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@src/runtime/api/bun/h2_frame_parser.rs`:
- Around line 2390-2420: The code path that finishes evicted/discarded header
blocks calls discard_header_block(&block) but that helper currently swallows
HPACK decode failures; change discard_header_block to return a Result/flag so
callers (e.g., the code handling discarded_block here) can detect a decode error
and treat it the same as decode_header_block: call send_go_away(...) with
ErrorCode::COMPRESSION_ERROR (and a suitable payload like b"COMPRESSION_ERROR")
and clear expecting_continuation/discarded_block. Concretely, update
discard_header_block to return a Result<(), HpackError> or bool, update callers
(this function and any other invokers) to check the return value, and on Err
call self.send_go_away(frame.stream_identifier, ErrorCode::COMPRESSION_ERROR,
b"COMPRESSION_ERROR", self.last_stream_id.get(), true) and return Ok(end_).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: ASSERTIVE
Plan: Pro
Run ID: 2b3bacf6-fa51-4eff-ac53-007f8ca7c443
📒 Files selected for processing (2)
src/runtime/api/bun/h2_frame_parser.rstest/js/node/http2/node-http2.test.js
… to a connection error
|
Classified the two failing checks — both are stale-base, not caused by this PR. (1) linux-x64-baseline-verify-baseline flags an AVX Vpcmpeqw in llint_op_wide32_wide32: the branch was built against the WebKit pinned at the old base (cf8fb22b…); main has since bumped WEBKIT_VERSION to 963f8758… and main builds don't show this. (2) The transpiler.test.js "stack overflows" failures on Windows are fixed on main by #31382 (2cd36bd), which post-dates this branch's base; the bake/deinitialization segfault on windows-aarch64 is unrelated to the http2 change, and the remaining failures are already tagged [flaky]. Updated the branch onto current main in d701b0a (clean merge — main's only h2_frame_parser.rs change since the base is the outbound DATA padding zero-fill from #31417, in regions this PR doesn't touch), rebuilt, and re-ran test/js/node/http2/node-http2.test.js including the new session-map eviction and closed-stream tests; all pass locally (the only failure is the known pre-existing "minimal maxSessionMemory" debug-build timeout). Pushed the updated branch for a fresh CI run. |
HTTP/2 streams that reach the CLOSED state were retained in the native session map until the connection was torn down, so a long-lived keep-alive connection accumulated one retained stream allocation per completed request and the per-session memory cap eventually tripped for a well-behaved sequential client. Completed streams are now removed from the map (and freed) once the current native entry point unwinds, and frames or host-function calls that reference an already-released stream id take the lenient closed-stream path from RFC 9113 (header blocks still feed the HPACK decoder, DATA still counts against connection flow control, everything else is discarded) instead of throwing or escalating to a connection error.
Also fixes the server-side
respond(headers, { endStream: true })path leaving the stream pinned in HALF_CLOSED_LOCAL when the peer had already half-closed.Tests: a new live-stream gauge in
bun:internal-for-testingbacks a fixture asserting 50 sequential round trips leave zero retained streams on either side; raw-socket tests cover a late response arriving for a stream the client already reset (including that the next response's HPACK indexed references still resolve) and late trailing HEADERS arriving for a stream the server already reset.