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>
This commit is contained in:
Jiawen Geng authored and GitHub committed 2026-11-22 18:49:50 +00:00
1 parent 9a2046e198
commit 3189560355
2 files changed
+114 -3

No files matched your search

+10 -3
View File
@@ -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) {
+104
View File
@@ -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();
});
});
});