Skip to content

node:http2: release completed streams from the session map - #31359

Open
Jarred-Sumner wants to merge 17 commits into
mainfrom
claude/hardening-fix-pre-109-http2-closed-streams-never-evicted
Open

Jarred-Sumner wants to merge 17 commits into
mainfrom
claude/hardening-fix-pre-109-http2-closed-streams-never-evicted

Conversation

@Jarred-Sumner

Copy link
Copy Markdown
Collaborator

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-testing backs 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.

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.
@coderabbitai

coderabbitai Bot commented May 24, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It 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 reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

Walkthrough

This 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.

Changes

Stream Eviction, Safety, and Testing

Layer / File(s) Summary
Core eviction state and helpers
src/runtime/api/bun/h2_frame_parser.rs
Adds eviction-tracking fields and ring, ActivityGuard, eviction sweep implementation, and parser constructor initialization.
Stream struct and closure routing
src/runtime/api/bun/h2_frame_parser.rs
Extends Stream with inbound/outbound close-coordination flags, initializes them, and routes all CLOSED transitions through close_stream().
Eviction primitives and HPACK discard
src/runtime/api/bun/h2_frame_parser.rs
Implements activity_guard(), close_stream(), eviction sweep, recently-evicted ring, and HPACK-based discard path for late header blocks.
Frame handlers: HEADERS / CONTINUATION / DATA / RST handling
src/runtime/api/bun/h2_frame_parser.rs
Discards late DATA/HEADERS/CONTINUATION/RST frames for evicted/closed streams while decoding HPACK as needed and preserving flow-control/validation without allocating streams.
Inbound allocation and LIVE_STREAM_COUNT accounting
src/runtime/api/bun/h2_frame_parser.rs
Introduces LIVE_STREAM_COUNT, increments on allocation, decrements on eviction/free and deinit, prevents re-allocation of recently-evicted IDs, and updates session memory accounting to include CLOSED entries.
ActivityGuard scoping across JS/native entry points
src/runtime/api/bun/h2_frame_parser.rs
Wraps many JS host methods and native read/write callbacks with activity_guard() so eviction runs at the intended re-entrancy boundary; JS-visible APIs treat evicted IDs as completed no-ops or synthesize closed-state responses.
Write path and deferred local finish
src/runtime/api/bun/h2_frame_parser.rs
Routes send-data/send-trailer close completions through close_stream, makes writes to evicted IDs no-ops, and adds close_on_local_finish to defer CLOSED until writeStream(..., close=true) completes.
Testing API exposure and JS binding
src/runtime/api/bun/h2_frame_parser.rs, src/runtime/api.rs, src/js/internal-for-testing.ts
Exports testing_apis::live_stream_count and testing_ap_is, re-exports via api::bun::h2_frame_parser, and binds to JS as nodeHttp2Internals.liveStreamCount().
Regression tests and raw-frame suites
test/js/node/http2/node-http2-evict-closed-streams.fixture.js, test/js/node/http2/node-http2.test.js
Adds fixture that cycles short-lived requests and prints native live-stream counts; adds subprocess-driven eviction test, makeFrameScanner raw-frame parser, and multiple raw-socket tests covering cancel races, CONTINUATION splits, trailers, request-body handling, HPACK error handling, and protocol enforcement.
  • Possibly related PRs:
    • oven-sh/bun#31323: Both PRs modify src/runtime/api/bun/h2_frame_parser.rs’s per-stream HEADERS/CONTINUATION handling and related reassembly/HPACK logic.
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: releasing completed HTTP/2 streams from the session map, which is the core objective addressed across multiple files.
Description check ✅ Passed The description covers both required sections: it explains what the PR does (stream lifecycle management and HPACK handling) and how it was verified (live-stream gauge and comprehensive raw-socket tests).
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Comment @coderabbitai help to get the list of available commands and usage tips.

@robobun

robobun commented May 24, 2026 •

Copy link
Copy Markdown
Collaborator
Updated 6:50 PM PT - May 28th, 2026

✅ @Jarred-Sumner, your commit d701b0a4985e970c0535f7c1afd2e64d9b1f5c2c passed in Build #58765! 🎉


🧪   To try this PR locally:

bunx bun-pr 31359

That installs a local version of the PR into your bun-31359 executable, so you can run:

bun-31359 --bun

@github-actions

Copy link
Copy Markdown
Contributor

Found 3 issues this PR may fix:

  1. Native RSS grows linearly under sustained AWS SDK v3 Kinesis GetRecords; identical Node 22 workload is flat #30415 - Native RSS grows linearly under sustained AWS SDK v3 Kinesis GetRecords on long-lived HTTP/2 keep-alive connections; stream eviction fix directly addresses native-side memory growth
  2. @fastify/http-proxy with HTTP/2 spuriously emits FST_REPLY_FROM_HTTP2_REQUEST_TIMEOUT after idle on Bun #30307 - @fastify/http-proxy with HTTP/2 spuriously emits timeouts after idle; retained closed streams consuming session memory could trigger maxSessionMemory and cause failures on idle keep-alive connections
  3. @grpc/grpc-js server on Bun emits repeated empty HTTP/2 DATA frames and no trailers; Envoy aborts with PROTOCOL_ERROR #21759 - @grpc/grpc-js server emits repeated empty DATA frames and no trailers; the respond({endStream:true}) fix for stuck HALF_CLOSED_LOCAL state could address why trailers/END_STREAM are never properly emitted

If this is helpful, copy the block below into the PR description to auto-close these issues on merge.

Fixes #30415
Fixes #30307
Fixes #21759

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown
Contributor

This PR may be a duplicate of:

  1. http2: reclaim closed-stream entries from the session streams map #30416 - Also reclaims closed HTTP/2 stream entries from the session streams map to prevent memory growth on long-lived connections

🤖 Generated with Claude Code

Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread test/js/node/http2/node-http2.test.js Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 49c97de and ebaafda.

📒 Files selected for processing (5)
  • src/js/internal-for-testing.ts
  • src/runtime/api.rs
  • src/runtime/api/bun/h2_frame_parser.rs
  • test/js/node/http2/node-http2-evict-closed-streams.fixture.js
  • test/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

Comment thread test/js/node/http2/node-http2.test.js Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between ebaafda and ea02504.

📒 Files selected for processing (2)
  • src/runtime/api/bun/h2_frame_parser.rs
  • test/js/node/http2/node-http2.test.js

Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between ea02504 and 231e775.

📒 Files selected for processing (2)
  • src/runtime/api/bun/h2_frame_parser.rs
  • test/js/node/http2/node-http2.test.js

Comment thread test/js/node/http2/node-http2.test.js Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

📥 Commits

Reviewing files that changed from the base of the PR and between 231e775 and 0b92752.

📒 Files selected for processing (2)
  • src/runtime/api/bun/h2_frame_parser.rs
  • test/js/node/http2/node-http2.test.js

Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
Comment thread src/runtime/api/bun/h2_frame_parser.rs Resolved
@Jarred-Sumner

Copy link
Copy Markdown
Collaborator Author

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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants