Skip to content

fix(node_http2): clean up destroyed sessions - #36043

Merged
nathanwhit merged 6 commits into
denoland:mainfrom
nathanwhit:fix/http2-session-cleanup
Jul 17, 2026
Merged

nathanwhit merged 6 commits into
denoland:mainfrom
nathanwhit:fix/http2-session-cleanup

Conversation

@nathanwhit

Copy link
Copy Markdown
Member

Summary

  • release native HTTP/2 session and callback allocations during teardown
  • clear retained JavaScript session references and HTTP/2-owned socket callbacks
  • make late receive and drain paths safe after session destruction
  • cover deterministic cleanup of the session handle and socket references

The Node-compatible HTTP/2 implementation owns state across JavaScript, Rust, nghttp2, and the underlying socket. Session destruction previously left parts of that ownership graph attached after close. This change centralizes teardown, makes it idempotent, and ensures late callbacks become harmless no-ops.

Testing

  • cargo build --bin deno
  • cargo check -p deno_node
  • target/debug/deno test -A --config tests/config/deno.json tests/unit_node/http2_test.ts
  • ./tools/format.js
  • cargo fmt --check --package deno_node

@nathanwhit
nathanwhit marked this pull request as ready for review July 15, 2026 01:24

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Solid lifetime fix that closes real leaks — the base code leaked the nghttp2 session, its callbacks table, the Box<Session>, and the UvTcp handle on every teardown (there was no Drop) — while making late callbacks safe no-ops.

Verified the load-bearing invariant: destroy frees only the native nghttp2 resources, never the Box<Session>, which is reclaimed only at cppgc finalize via teardown. Every reentrant path respects it:

  • close_native_resources / NativeResources::close are idempotent (Cell swap-to-null guards, Option::take, HashMap::clear) — safe to call any number of times, and Drop re-calls cleanly.
  • Stream closed exactly once via take_stream_for_close — no double uv_close.
  • Nulling (*stream).data (instead of restoring orig_stream_data) is correct: tcp.detach() already relinquished the StreamHandleData, so the old restore path pointed at an owner-less pointer. h2_read_cb already null-guards session_ptr, so late libuv reads no-op.
  • Reentrant emit_pending_internal_error -> onSessionInternalError -> destroy mid-send_pending_data frees native, but self stays valid memory and the following maybe_notify_graceful_close_complete / flush_pending_rst_streams got is_null() guards. No UAF.
  • JS onPing/onSettings/onGoawayData/... now guard session === undefined || session.destroyed, and onStreamTrailers reorder fixes a real latent throw.

The test-http2-session-cleanup-on-nghttp2-goaway enablement genuinely exercises the new FRAME_SIZE_ERROR GOAWAY branch (deferred via pending_internal_error), with mustCall on error+close — not a rubber-stamp flip. New unit test is deterministic.

Non-blocking: (1) when destroy is deferred (is_sending==true) but cleanupSession has stubbed sendPending, native+socket free can slip to GC finalize — safe but a soft lifetime delay under load; (2) the read-only ops (frames_received, active_stream_count, etc.) still deref inner without the null guard the other ops got — safe today but asymmetric.

Heads up: overlaps session.rs with #36044, so expect a textual (not semantic) rebase conflict for whichever lands second.

@nathanwhit
nathanwhit enabled auto-merge (squash) July 17, 2026 21:26
@nathanwhit
nathanwhit merged commit 10344fa into denoland:main Jul 17, 2026
136 checks passed
bartlomieju pushed a commit that referenced this pull request Jul 23, 2026
## Summary

- release native HTTP/2 session and callback allocations during teardown
- clear retained JavaScript session references and HTTP/2-owned socket
callbacks
- make late receive and drain paths safe after session destruction
- cover deterministic cleanup of the session handle and socket
references

The Node-compatible HTTP/2 implementation owns state across JavaScript,
Rust, nghttp2, and the underlying socket. Session destruction previously
left parts of that ownership graph attached after close. This change
centralizes teardown, makes it idempotent, and ensures late callbacks
become harmless no-ops.

## Testing

- `cargo build --bin deno`
- `cargo check -p deno_node`
- `target/debug/deno test -A --config tests/config/deno.json
tests/unit_node/http2_test.ts`
- `./tools/format.js`
- `cargo fmt --check --package deno_node`
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