Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/node_sqlite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1739,11 +1739,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) {
Expand Down
104 changes: 104 additions & 0 deletions test/parallel/test-sqlite-virtual-table.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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();
});
});
});
Loading