fix: prevent use-after-free when GC finalizes NodeQueryResult#10
Merged
fix: prevent use-after-free when GC finalizes NodeQueryResult#10
Conversation
…atabase close NodeQueryResult held a unique_ptr<MaterializedQueryResult> which internally owns a FactorizedTable that accesses database-managed memory (buffer pool, allocators) during destruction. If the Database was explicitly destroyed via db.closeSync() before the GC finalizer for NodeQueryResult ran, the FactorizedTable destructor would access freed memory, crashing the process. This manifested whenever query results were not explicitly closed before closing the connection and database -- a common and reasonable usage pattern. Fix: NodeQueryResult now holds a shared_ptr<Database> alongside its QueryResult. The database ref is acquired in AdoptQueryResult and released last in Close(), after ownedQueryResult is reset. This guarantees the Database cannot be freed until every NodeQueryResult that references it has been destroyed, regardless of GC timing. To thread the shared_ptr through: - NodeConnection retains its database shared_ptr after InitCppConnection() instead of dropping it immediately (the drop is moved to Close()) - Both async workers (ConnectionQueryAsyncWorker, ConnectionExecuteAsyncWorker) carry a shared_ptr<Database> and forward it to AdoptQueryResult - QuerySync and ExecuteSync pass the connection's database ref directly - NewInstance and GetNextQueryResultSync/Async propagate the ref to child results created when iterating multi-statement query chains Adds regression test: discards query results without closing them, then calls conn.closeSync() and db.closeSync(), verifying the process does not crash when the GC finalizer later runs.
NodeConnection now holds a shared_ptr<Database> to keep the database alive until all NodeQueryResults are GC'd. As a side effect, db.closeSync() no longer immediately invalidates the connection -- the database stays alive until the connection also releases its ref. Drop the assertions that querying and iterating throw after db.closeSync(); just verify the reverse close order doesn't crash.
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.
Fixes: #8
NodeQueryResult held a unique_ptr which internally owns a FactorizedTable that accesses database-managed memory (buffer pool, allocators) during destruction. If the Database was explicitly destroyed via db.closeSync() before the GC finalizer for NodeQueryResult ran, the FactorizedTable destructor would access freed memory, crashing the process.
This manifested whenever query results were not explicitly closed before closing the connection and database -- a common and reasonable usage pattern.
Fix: NodeQueryResult now holds a shared_ptr alongside its QueryResult. The database ref is acquired in AdoptQueryResult and released last in Close(), after ownedQueryResult is reset. This guarantees the Database cannot be freed until every NodeQueryResult that references it has been destroyed, regardless of GC timing.
To thread the shared_ptr through:
Adds regression test: discards query results without closing them, then calls conn.closeSync() and db.closeSync(), verifying the process does not crash when the GC finalizer later runs.