Files
node/test/parallel/test-sqlite-diagnostic-channel.js
Jiawen Geng c9a7b131f1 sqlite: skip query trace while an exception is pending
Database::TraceCallback published sqlite.db.query from SQLite's
profile callback without checking for a pending JavaScript exception.
SQLite runs that callback from sqlite3_step() when a statement fails
and from sqlite3_reset(), so a throwing SQL function or an
out-of-range integer conversion called subscriber JS while an
exception was already set. Debug builds abort in V8's Invoke, and
release builds drop the original error or replace it with an empty
ERR_SQLITE_ERROR.

Return before publishing when the isolate has a pending exception or
JavaScript cannot be entered.

Assisted-by: Cursor Grok 4.7
Signed-off-by: gengjiawen <technicalcute@gmail.com>
PR-URL: https://github.com/nodejs/node/pull/67538
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2026-11-25 18:00:54 +01:00

272 lines
8.4 KiB
JavaScript

// Flags: --expose-gc
'use strict';
const { mustCall, skipIfSQLiteMissing } = require('../common');
skipIfSQLiteMissing();
const assert = require('node:assert');
const dc = require('node:diagnostics_channel');
const { Database } = require('node:sqlite');
const { suite, it } = require('node:test');
const { gcUntil } = require('../common/gc');
suite('sqlite.db.query diagnostics channel', () => {
it('subscriber receives SQL string for exec() statements', (t) => {
const calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
db.exec('INSERT INTO t VALUES (1)');
assert.strictEqual(calls.length, 2);
assert.strictEqual(calls[0].sql, 'CREATE TABLE t (x INTEGER)');
assert.strictEqual(calls[1].sql, 'INSERT INTO t VALUES (1)');
});
it('subscriber receives SQL string for prepared INSERT statements', (t) => {
let calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
calls = []; // reset after setup
using stmt = db.prepare('INSERT INTO t VALUES (?)');
stmt.run(42);
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].sql, 'INSERT INTO t VALUES (42.0)');
});
it('subscriber receives SQL string for prepared SELECT statements', (t) => {
let calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
db.exec('INSERT INTO t VALUES (1)');
calls = []; // reset after setup
using stmt = db.prepare('SELECT x FROM t WHERE x = ?');
stmt.get(1);
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].sql, 'SELECT x FROM t WHERE x = 1.0');
});
it('subscriber receives SQL string for prepared UPDATE statements', (t) => {
let calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
db.exec('INSERT INTO t VALUES (1)');
calls = []; // reset after setup
using stmt = db.prepare('UPDATE t SET x = ? WHERE x = ?');
stmt.run(2, 1);
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].sql, 'UPDATE t SET x = 2.0 WHERE x = 1.0');
});
it('subscriber receives SQL string for prepared DELETE statements', (t) => {
let calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
db.exec('INSERT INTO t VALUES (1)');
calls = []; // reset after setup
using stmt = db.prepare('DELETE FROM t WHERE x = ?');
stmt.run(1);
assert.strictEqual(calls.length, 1);
assert.strictEqual(calls[0].sql, 'DELETE FROM t WHERE x = 1.0');
});
it('no calls received after unsubscribe', (t) => {
const calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
db.exec('CREATE TABLE t (x INTEGER)');
assert.strictEqual(calls.length, 1);
dc.unsubscribe('sqlite.db.query', handler);
db.exec('INSERT INTO t VALUES (1)');
assert.strictEqual(calls.length, 1); // No new calls after unsubscribe
});
it('falls back to source SQL when expansion fails', (t) => {
let calls = [];
using db = new Database(':memory:', { limits: { length: 1000 } });
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x TEXT)');
calls = []; // reset after setup
using stmt = db.prepare('INSERT INTO t VALUES (?)');
const longValue = 'a'.repeat(977);
stmt.run(longValue);
assert.strictEqual(calls.length, 1);
// Falls back to source SQL with unexpanded '?' placeholder
assert.strictEqual(calls[0].sql, 'INSERT INTO t VALUES (?)');
});
it('database property identifies the correct database', (t) => {
const calls = [];
using db1 = new Database(':memory:');
using db2 = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db1.exec('CREATE TABLE t (x INTEGER)');
db2.exec('CREATE TABLE t (x INTEGER)');
assert.strictEqual(calls.length, 2);
assert.strictEqual(calls[0].database, db1);
assert.strictEqual(calls[1].database, db2);
assert.notStrictEqual(calls[0].database, calls[1].database);
});
it('duration is a number', (t) => {
const calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
assert.strictEqual(calls.length, 1);
assert.strictEqual(typeof calls[0].duration, 'number');
});
it('duration is non-negative', (t) => {
const calls = [];
using db = new Database(':memory:');
const handler = (msg) => calls.push(msg);
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.exec('CREATE TABLE t (x INTEGER)');
assert.strictEqual(calls.length, 1);
assert.ok(calls[0].duration >= 0);
});
it('does not publish when an unfinished statement is collected', async (t) => {
let calls = 0;
const handler = () => calls++;
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
let collected = false;
const registry = new FinalizationRegistry(() => { collected = true; });
(() => {
const db = new Database(':memory:');
db.exec('CREATE TABLE t (x INTEGER)');
for (let i = 0; i < 10; i++) {
db.exec(`INSERT INTO t VALUES (${i})`);
}
const stmt = db.prepare('SELECT x FROM t');
registry.register(stmt);
stmt.iterate().next(); // Leave the statement unfinished.
})();
calls = 0; // reset after setup
await gcUntil('unfinished statement is collected', () => collected);
assert.strictEqual(calls, 0);
});
it('subscriber cannot close the database or statement', (t) => {
using db = new Database(':memory:');
db.exec('CREATE TABLE t (x INTEGER)');
using stmt = db.prepare('INSERT INTO t VALUES (?)');
const handler = mustCall(() => {
assert.throws(() => db.close(), { code: 'ERR_INVALID_STATE' });
assert.throws(() => stmt.close(), { code: 'ERR_INVALID_STATE' });
assert.throws(() => stmt[Symbol.dispose](), {
code: 'ERR_INVALID_STATE',
});
});
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
stmt.run(1);
dc.unsubscribe('sqlite.db.query', handler);
assert.deepStrictEqual(db.prepare('SELECT x FROM t').all(), [
{ __proto__: null, x: 1 },
]);
});
it('does not replace an exception that is already pending', (t) => {
using db = new Database(':memory:');
let calls = 0;
const handler = () => {
calls++;
};
dc.subscribe('sqlite.db.query', handler);
t.after(() => dc.unsubscribe('sqlite.db.query', handler));
db.function('boom', () => {
throw new Error('boom');
});
assert.throws(() => db.prepare('SELECT boom()').get(), {
message: 'boom',
});
db.exec('CREATE TABLE t (a INTEGER)');
db.exec('INSERT INTO t VALUES (9223372036854775807)');
assert.throws(() => db.prepare('SELECT a FROM t').get(), {
code: 'ERR_OUT_OF_RANGE',
});
assert.throws(() => db.prepare('SELECT a FROM t').all(), {
code: 'ERR_OUT_OF_RANGE',
});
// A query that does not throw is still published.
const before = calls;
assert.deepStrictEqual(db.prepare('SELECT 1 AS x').get(), {
__proto__: null,
x: 1,
});
assert.ok(calls > before);
});
});