sqlite: keep sessions alive across SQLite callbacks - #65465
Open
TrevorBurnham wants to merge 2 commits into
Open
sqlite: keep sessions alive across SQLite callbacks#65465TrevorBurnham wants to merge 2 commits into
TrevorBurnham wants to merge 2 commits into
Conversation
Session objects are weak and nothing else holds a strong reference to them, so a garbage collection can free one while SQLite is still using it. SQLite runs "PRAGMA table_xinfo" from inside its pre-update hook, which reaches JavaScript, so a GC during a callback invoked from there can collect a session the hook is still walking. Hold a strong reference to every attached session for the duration of each callback SQLite invokes. The trace callback now enters that guard before building its payload, since the allocation can itself trigger a garbage collection. Fixes: nodejs#65460 Signed-off-by: Trevor Burnham <trevorburnham@gmail.com>
Collaborator
|
Review requested:
|
Return the pinned sessions by value instead of appending to a caller's vector, which read as an append but reserved as if the target were empty. Signed-off-by: Trevor Burnham <trevorburnham@gmail.com>
TrevorBurnham
marked this pull request as ready for review
August 21, 2026 20:41
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #65465 +/- ##
==========================================
+ Coverage 90.12% 90.13% +0.01%
==========================================
Files 752 751 -1
Lines 252315 252306 -9
Branches 47444 47436 -8
==========================================
+ Hits 227395 227416 +21
+ Misses 16217 16196 -21
+ Partials 8703 8694 -9
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SessioncallsMakeWeak()andDatabaseSync::sessions_holds rawSession*, so nothing keeps a session alive once JavaScript drops the reference. SQLite runsPRAGMA table_xinfofrom inside its pre-update hook, which reaches JavaScript, so a GC during a callback invoked from there collects the session the hook is still walking:CallbackDepthGuardalready wraps every point where SQLite hands control back to JavaScript (authorizer, trace subscriber, user-defined functions, aggregates, changeset apply), so it now also holds aBaseObjectPtrto each attached session for the callback's duration.BaseObjectPtrclears the weak handle on the first strong reference and restores it on the last, so a session that JavaScript no longer references simply becomes collectable again once the callback returns. Connections with no sessions allocate nothing.Making
sessions_strong instead would create a cycle withSession::database_, which is deliberately strong (a session must keep its database alive), and leak both objects.TraceCallbackbuilt its payload before entering the guard. That allocation can trigger a GC, so the guard moves above it. I could not get that window to crash on demand, even with--gc-interval=1, so treat it as defensive rather than a demonstrated fix.Fixes: #65460