Repository navigation
fix(node_http2): clean up destroyed sessions - #36043
Conversation
bartlomieju
left a comment
There was a problem hiding this comment.
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::closeare idempotent (Cell swap-to-null guards,Option::take,HashMap::clear) — safe to call any number of times, andDropre-calls cleanly.- Stream closed exactly once via
take_stream_for_close— no doubleuv_close. - Nulling
(*stream).data(instead of restoringorig_stream_data) is correct:tcp.detach()already relinquished theStreamHandleData, so the old restore path pointed at an owner-less pointer.h2_read_cbalready null-guardssession_ptr, so late libuv reads no-op. - Reentrant
emit_pending_internal_error->onSessionInternalError->destroymid-send_pending_datafrees native, butselfstays valid memory and the followingmaybe_notify_graceful_close_complete/flush_pending_rst_streamsgotis_null()guards. No UAF. - JS
onPing/onSettings/onGoawayData/...now guardsession === undefined || session.destroyed, andonStreamTrailersreorder 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.
## 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`
Summary
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 denocargo check -p deno_nodetarget/debug/deno test -A --config tests/config/deno.json tests/unit_node/http2_test.ts./tools/format.jscargo fmt --check --package deno_node