Commit 31895603550 for nodejs
commit 318956035501938c2b06fab4f130fae7797db85e
Author: Jiawen Geng <technicalcute@gmail.com>
Date: Mon Nov 23 02:49:50 2026 +0800
sqlite: finalize statements prepared during close
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>
PR-URL: https://github.com/nodejs/node/pull/67539
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc
index ba10d4e56c5..3e2d58dccf8 100644
--- a/src/node_sqlite.cc
+++ b/src/node_sqlite.cc
@@ -1744,11 +1744,18 @@ void Database::FinalizeBackups() {
}
void Database::FinalizeStatements() {
- for (auto stmt : statements_) {
+ // Finalizing a statement can run JavaScript: closing a virtual-table cursor
+ // calls the generator's return(). That code can prepare another statement,
+ // which inserts into this set. Erase each statement first and keep going
+ // until the set is empty, so a statement created from that JavaScript is
+ // finalized before the connection is released. Holding an iterator across
+ // the insertion would also be undefined for std::unordered_set.
+ while (!statements_.empty()) {
+ auto it = statements_.begin();
+ Statement* stmt = *it;
+ statements_.erase(it);
stmt->Finalize();
}
-
- statements_.clear();
}
void Database::TrackStatement(Statement* statement) {
diff --git a/test/parallel/test-sqlite-virtual-table.js b/test/parallel/test-sqlite-virtual-table.js
index cc520f72344..8e55a0ea444 100644
--- a/test/parallel/test-sqlite-virtual-table.js
+++ b/test/parallel/test-sqlite-virtual-table.js
@@ -3,8 +3,10 @@
const { skipIfSQLiteMissing, mustCallAtLeast } = require('../common');
skipIfSQLiteMissing();
const assert = require('node:assert');
+const { join } = require('node:path');
const { Database } = require('node:sqlite');
const { suite, test } = require('node:test');
+const tmpdir = require('../common/tmpdir');
suite('Database.prototype.createModule()', () => {
suite('input validation', () => {
@@ -986,4 +988,106 @@ suite('Database.prototype.createModule()', () => {
assert.strictEqual(callCount, 2);
});
});
+
+ suite('statements prepared while a cursor is closing', () => {
+ test('finalizes statements prepared from a generator finally', () => {
+ tmpdir.refresh();
+ const filename = join(tmpdir.path, 'vtab-close.db');
+ const db = new Database(filename);
+ db.exec('CREATE TABLE log (m TEXT)');
+ let late;
+ db.createModule('gen', {
+ columns: [{ name: 'v', type: 'INTEGER' }],
+ *rows() {
+ try {
+ yield [1];
+ yield [2];
+ } finally {
+ db.prepare("INSERT INTO log VALUES ('closed')").run();
+ late = db.prepare('SELECT COUNT(*) AS c FROM log');
+ }
+ },
+ });
+
+ const it = db.prepare('SELECT v FROM gen').iterate();
+ assert.strictEqual(it.next().value.v, 1);
+ db.close();
+
+ assert.strictEqual(db.isOpen, false);
+ assert.throws(() => late.get(), {
+ code: 'ERR_INVALID_STATE',
+ message: 'statement has been finalized',
+ });
+ assert.throws(() => late.run(), {
+ code: 'ERR_INVALID_STATE',
+ message: 'statement has been finalized',
+ });
+
+ const check = new Database(filename);
+ assert.strictEqual(
+ check.prepare('SELECT COUNT(*) AS c FROM log').get().c,
+ 1,
+ );
+ check.close();
+ });
+
+ test('finalizes statements already open when prepare() reenters', () => {
+ const db = new Database(':memory:');
+ const earlier = db.prepare('SELECT 1 AS n');
+ const prepared = [];
+ db.createModule('gen', {
+ columns: [{ name: 'v', type: 'INTEGER' }],
+ *rows() {
+ try {
+ yield [1];
+ } finally {
+ for (let i = 0; i < 32; i++) {
+ prepared.push(db.prepare(`SELECT ${i} AS n`));
+ }
+ }
+ },
+ });
+
+ db.prepare('SELECT v FROM gen').iterate().next();
+ db.close();
+
+ assert.throws(() => earlier.get(), { code: 'ERR_INVALID_STATE' });
+ for (const stmt of prepared) {
+ assert.throws(() => stmt.get(), { code: 'ERR_INVALID_STATE' });
+ }
+ });
+
+ test('finalizes a statement prepared while deserialize() drops it', () => {
+ const source = new Database(':memory:');
+ source.exec('CREATE TABLE t (a INTEGER)');
+ source.exec('INSERT INTO t VALUES (123)');
+ const serialized = source.serialize();
+ source.close();
+
+ const db = new Database(':memory:');
+ db.exec('CREATE TABLE t (a INTEGER)');
+ db.exec('INSERT INTO t VALUES (7)');
+ let late;
+ db.createModule('gen', {
+ columns: [{ name: 'v', type: 'INTEGER' }],
+ *rows() {
+ try {
+ yield [1];
+ } finally {
+ late = db.prepare('SELECT a FROM t');
+ }
+ },
+ });
+ db.prepare('SELECT v FROM gen').iterate().next();
+ db.deserialize(serialized);
+
+ assert.strictEqual(db.isOpen, true);
+ assert.throws(() => late.get(), {
+ code: 'ERR_INVALID_STATE',
+ message: 'statement has been finalized',
+ });
+ assert.strictEqual(db.prepare('SELECT a FROM t').get().a, 123);
+ db.close();
+ });
+ });
});