Skip to content

sqlite: finalize statements prepared during close - #67539

Open
gengjiawen wants to merge 1 commit into
nodejs:mainfrom
gengjiawen:sqlite-finalize-statements-during-close
Open

gengjiawen wants to merge 1 commit into
nodejs:mainfrom
gengjiawen:sqlite-finalize-statements-during-close

Conversation

@gengjiawen

Copy link
Copy Markdown
Member

Summary

Database::FinalizeStatements() range-iterated statements_ and then cleared the set. Finalizing a statement with an open virtual-table cursor runs the generator's finally block, and that JavaScript can prepare() another statement. The new statement was inserted into the set being iterated, so it was never finalized. sqlite3_close_v2() then left the connection open, and Statement::Run() dereferenced a null connection.

The same path runs from Database::Deserialize().

Erase each statement before finalizing it and keep going until the set is empty, so a statement created from that finally block is finalized before the connection is released. An INSERT done from finally still commits, because it runs while the connection is open.

Test plan

  • node --expose-gc --test test/parallel/test-sqlite-virtual-table.js
  • A file-backed database checks that the INSERT from finally is visible after reopen, and that the statement prepared there throws statement has been finalized.
  • A second case prepares 32 statements from finally on top of a statement that was already open.
  • deserialize() finalizes a statement prepared while the previous database is dropped, then a new query reads the replacement database.

Made with Cursor

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/sqlite

@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. sqlite Issues and PRs related to the SQLite subsystem. labels Nov 17, 2026
@gengjiawen
gengjiawen force-pushed the sqlite-finalize-statements-during-close branch from 3fb7b48 to 2fe343a Compare November 17, 2026 09:58
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.71%. Comparing base (bc6dd40) to head (9c7aaf5).
⚠️ Report is 45 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67539      +/-   ##
==========================================
+ Coverage   90.67%   90.71%   +0.03%     
==========================================
  Files         800      801       +1     
  Lines      280489   280707     +218     
  Branches    54166    54177      +11     
==========================================
+ Hits       254336   254638     +302     
+ Misses      16277    16245      -32     
+ Partials     9876     9824      -52     
Files with missing lines Coverage Δ
src/node_sqlite.cc 82.87% <100.00%> (+0.12%) ⬆️

... and 75 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.

Database::FinalizeStatements() range-iterates statements_ and then
clears the set. Finalizing a statement with an open virtual-table
cursor runs the generator's finally block, which can prepare a new
statement and insert it into the set being iterated. That statement
is never finalized, so it keeps running after close() releases the
connection, and run() then dereferences a null connection.

Erase each statement before finalizing it and continue until the set
is empty, so statements created from that JavaScript are finalized
before the connection is released.

Assisted-by: Cursor Grok 4.7
Signed-off-by: gengjiawen <technicalcute@gmail.com>
@gengjiawen
gengjiawen force-pushed the sqlite-finalize-statements-during-close branch from 2fe343a to 9c7aaf5 Compare November 18, 2026 03:50
@gengjiawen gengjiawen added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Nov 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

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. sqlite Issues and PRs related to the SQLite subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants