Skip to content

sqlite: guard re-entrant reset of a virtual table cursor - #67540

Open
gengjiawen wants to merge 1 commit into
nodejs:mainfrom
gengjiawen:sqlite-guard-reentrant-statement-reset
Open

gengjiawen wants to merge 1 commit into
nodejs:mainfrom
gengjiawen:sqlite-guard-reentrant-statement-reset

Conversation

@gengjiawen

Copy link
Copy Markdown
Member

Summary

StatementIterator::Return() and SQLTagStore::ResetAndBindStatement() called sqlite3_reset() without a SteppingStatementGuard. Resetting a statement that has an open virtual-table cursor runs the generator's finally block. JavaScript there can reset the same statement again. SQLite then halts the VM a second time while the cursor slot still points at the cursor, so xClose frees the same NodeVTabCursor twice.

Hold the stepping guard across those resets. The re-entrant call fails with statement is already being executed instead of closing the cursor again. SQLTagStore::All() holds the same guard so its trailing reset is covered too. A cursor that is already closing ignores a second xClose.

Test plan

  • node --expose-gc --test test/parallel/test-sqlite-virtual-table.js
  • node --expose-gc --test test/parallel/test-sqlite-udf-close.js
  • finally calls iterator.return(), statement.get(), and the same tagged get() while the outer reset is in progress. Each re-entry throws ERR_INVALID_STATE.
  • for...of with break covers the same path through StatementIterator::Return().

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-guard-reentrant-statement-reset branch from 6bc0ffe to fc48f73 Compare November 17, 2026 09:58
@codecov

codecov Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.71%. Comparing base (3ea9547) to head (600ae5d).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/node_sqlite.cc 75.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main   #67540   +/-   ##
=======================================
  Coverage   90.71%   90.71%           
=======================================
  Files         801      801           
  Lines      280705   280716   +11     
  Branches    54168    54174    +6     
=======================================
+ Hits       254640   254660   +20     
+ Misses      16247    16241    -6     
+ Partials     9818     9815    -3     
Files with missing lines Coverage Δ
src/node_sqlite.h 87.93% <100.00%> (+0.21%) ⬆️
src/node_sqlite.cc 82.85% <75.00%> (-0.02%) ⬇️

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

@MikeMcC399

This comment was marked as resolved.

StatementIterator::Return() and SQLTagStore::ResetAndBindStatement()
called sqlite3_reset() without a SteppingStatementGuard. Resetting a
statement with an open virtual-table cursor runs the generator's
finally block, and that JavaScript can reset the same statement
again. SQLite then halts the VM a second time while the cursor slot
still points at the cursor, so xClose frees the same NodeVTabCursor
twice.

Hold the stepping guard across those resets so the re-entrant call
fails with "statement is already being executed" instead of closing
the cursor again. Ignore a second xClose on a cursor that is already
closing.

Assisted-by: Cursor Grok 4.7
Signed-off-by: gengjiawen <technicalcute@gmail.com>
@gengjiawen
gengjiawen force-pushed the sqlite-guard-reentrant-statement-reset branch from fc48f73 to 600ae5d Compare November 18, 2026 03:50
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