Skip to content

Commit 3189560

Browse files
authored
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: #67539 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9a2046e commit 3189560

2 files changed

Lines changed: 114 additions & 3 deletions

File tree

‎src/node_sqlite.cc‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,11 +1744,18 @@ void Database::FinalizeBackups() {
17441744
}
17451745

17461746
void Database::FinalizeStatements() {
1747-
for (auto stmt : statements_) {
1747+
// Finalizing a statement can run JavaScript: closing a virtual-table cursor
1748+
// calls the generator's return(). That code can prepare another statement,
1749+
// which inserts into this set. Erase each statement first and keep going
1750+
// until the set is empty, so a statement created from that JavaScript is
1751+
// finalized before the connection is released. Holding an iterator across
1752+
// the insertion would also be undefined for std::unordered_set.
1753+
while (!statements_.empty()) {
1754+
auto it = statements_.begin();
1755+
Statement* stmt = *it;
1756+
statements_.erase(it);
17481757
stmt->Finalize();
17491758
}
1750-
1751-
statements_.clear();
17521759
}
17531760

17541761
void Database::TrackStatement(Statement* statement) {

‎test/parallel/test-sqlite-virtual-table.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
const { skipIfSQLiteMissing, mustCallAtLeast } = require('../common');
44
skipIfSQLiteMissing();
55
const assert = require('node:assert');
6+
const { join } = require('node:path');
67
const { Database } = require('node:sqlite');
78
const { suite, test } = require('node:test');
9+
const tmpdir = require('../common/tmpdir');
810

911
suite('Database.prototype.createModule()', () => {
1012
suite('input validation', () => {
@@ -986,4 +988,106 @@ suite('Database.prototype.createModule()', () => {
986988
assert.strictEqual(callCount, 2);
987989
});
988990
});
991+
992+
suite('statements prepared while a cursor is closing', () => {
993+
test('finalizes statements prepared from a generator finally', () => {
994+
tmpdir.refresh();
995+
const filename = join(tmpdir.path, 'vtab-close.db');
996+
const db = new Database(filename);
997+
db.exec('CREATE TABLE log (m TEXT)');
998+
let late;
999+
db.createModule('gen', {
1000+
columns: [{ name: 'v', type: 'INTEGER' }],
1001+
*rows() {
1002+
try {
1003+
yield [1];
1004+
yield [2];
1005+
} finally {
1006+
db.prepare("INSERT INTO log VALUES ('closed')").run();
1007+
late = db.prepare('SELECT COUNT(*) AS c FROM log');
1008+
}
1009+
},
1010+
});
1011+
1012+
const it = db.prepare('SELECT v FROM gen').iterate();
1013+
assert.strictEqual(it.next().value.v, 1);
1014+
db.close();
1015+
1016+
assert.strictEqual(db.isOpen, false);
1017+
assert.throws(() => late.get(), {
1018+
code: 'ERR_INVALID_STATE',
1019+
message: 'statement has been finalized',
1020+
});
1021+
assert.throws(() => late.run(), {
1022+
code: 'ERR_INVALID_STATE',
1023+
message: 'statement has been finalized',
1024+
});
1025+
1026+
const check = new Database(filename);
1027+
assert.strictEqual(
1028+
check.prepare('SELECT COUNT(*) AS c FROM log').get().c,
1029+
1,
1030+
);
1031+
check.close();
1032+
});
1033+
1034+
test('finalizes statements already open when prepare() reenters', () => {
1035+
const db = new Database(':memory:');
1036+
const earlier = db.prepare('SELECT 1 AS n');
1037+
const prepared = [];
1038+
db.createModule('gen', {
1039+
columns: [{ name: 'v', type: 'INTEGER' }],
1040+
*rows() {
1041+
try {
1042+
yield [1];
1043+
} finally {
1044+
for (let i = 0; i < 32; i++) {
1045+
prepared.push(db.prepare(`SELECT ${i} AS n`));
1046+
}
1047+
}
1048+
},
1049+
});
1050+
1051+
db.prepare('SELECT v FROM gen').iterate().next();
1052+
db.close();
1053+
1054+
assert.throws(() => earlier.get(), { code: 'ERR_INVALID_STATE' });
1055+
for (const stmt of prepared) {
1056+
assert.throws(() => stmt.get(), { code: 'ERR_INVALID_STATE' });
1057+
}
1058+
});
1059+
1060+
test('finalizes a statement prepared while deserialize() drops it', () => {
1061+
const source = new Database(':memory:');
1062+
source.exec('CREATE TABLE t (a INTEGER)');
1063+
source.exec('INSERT INTO t VALUES (123)');
1064+
const serialized = source.serialize();
1065+
source.close();
1066+
1067+
const db = new Database(':memory:');
1068+
db.exec('CREATE TABLE t (a INTEGER)');
1069+
db.exec('INSERT INTO t VALUES (7)');
1070+
let late;
1071+
db.createModule('gen', {
1072+
columns: [{ name: 'v', type: 'INTEGER' }],
1073+
*rows() {
1074+
try {
1075+
yield [1];
1076+
} finally {
1077+
late = db.prepare('SELECT a FROM t');
1078+
}
1079+
},
1080+
});
1081+
db.prepare('SELECT v FROM gen').iterate().next();
1082+
db.deserialize(serialized);
1083+
1084+
assert.strictEqual(db.isOpen, true);
1085+
assert.throws(() => late.get(), {
1086+
code: 'ERR_INVALID_STATE',
1087+
message: 'statement has been finalized',
1088+
});
1089+
assert.strictEqual(db.prepare('SELECT a FROM t').get().a, 123);
1090+
db.close();
1091+
});
1092+
});
9891093
});

0 commit comments

Comments
 (0)