Skip to content

quic: pin session and stream lifetimes across re-entrant JS - #67744

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:quic-pin-session-stream-lifetimes
Open

mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:quic-pin-session-stream-lifetimes

Conversation

@mcollina

Copy link
Copy Markdown
Member

QUIC sessions and streams are detached from JavaScript ownership: the
SessionManager and the session's streams map hold the sole BaseObjectPtr
references. Synchronous JavaScript callbacks issued while native code is
still using these objects (Stream::Destroy, Session::SendPendingData,
Session::SendDatagram, Session::TryWritePendingDatagram, and the
SendPendingDataScope destructor) can destroy the session, drop the last
owning reference, and free the C++ object while it is still being accessed,
causing a heap use-after-free.

Pin the session with a strong BaseObjectPtr in SendPendingDataScope
and in SendPendingData()/SendDatagram(), and pin the stream with a
BaseObjectPtr in Stream::Destroy(). Add is_destroyed() guards at the
re-entrant JavaScript call sites (StreamDataBlocked -> EmitBlocked,
DatagramStatus) so the send loop does not continue after the session has been
destroyed.

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/quic

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC transport implementation. labels Nov 24, 2026
Sessions and streams are detached from JavaScript ownership: the
SessionManager and the session's streams map hold the sole
BaseObjectPtr references. Synchronous JavaScript callbacks made during
Stream::Destroy(), Session::SendPendingData(), Session::SendDatagram()
and Session::TryWritePendingDatagram() can destroy the session, dropping
the last owning reference and freeing the C++ object while native code
still touches it, resulting in a heap use-after-free (observed as a
crash in Session::SendPendingDataScope::~SendPendingDataScope()).

Pin the session with a strong BaseObjectPtr<Session> in
SendPendingDataScope and in SendPendingData()/SendDatagram(), and pin
the stream with a BaseObjectPtr<Stream> in Stream::Destroy(). Guard the
re-entrant JavaScript call sites (StreamDataBlocked -> EmitBlocked,
DatagramStatus) with is_destroyed() checks before continuing, so the
send loop never dereferences a destroyed session's impl_.

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@mcollina
mcollina force-pushed the quic-pin-session-stream-lifetimes branch from d31e737 to d2c4b69 Compare November 24, 2026 10:45
@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.55%. Comparing base (d2c9ac0) to head (d2c4b69).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67744   +/-   ##
=======================================
  Coverage   90.54%   90.55%           
=======================================
  Files         803      803           
  Lines      285094   285110   +16     
  Branches    55024    55024           
=======================================
+ Hits       258133   258170   +37     
+ Misses      17105    17088   -17     
+ Partials     9856     9852    -4     

see 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. quic Issues and PRs related to the QUIC transport implementation.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants