From f428cf213bf5743e6793a99a1164fb0c8dec2f7b Mon Sep 17 00:00:00 2001 From: Jiawen Geng Date: Mon, 23 Nov 2026 02:50:06 +0800 Subject: [PATCH] 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 PR-URL: https://github.com/nodejs/node/pull/67538 Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- src/node_sqlite.cc | 10 ++++++ .../test-sqlite-diagnostic-channel.js | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/src/node_sqlite.cc b/src/node_sqlite.cc index 3e2d58dccf8..2f4050cb8c1 100644 --- a/src/node_sqlite.cc +++ b/src/node_sqlite.cc @@ -3369,6 +3369,16 @@ int Database::TraceCallback(unsigned int type, return 0; } + // The profile callback also runs when a statement fails and when it is + // reset, both of which can happen while a JavaScript exception is already + // pending: a throwing SQL function, or a row value that cannot be converted. + // Publishing here would enter V8 with that exception still set. Debug + // builds then abort in Invoke, and release builds drop the user's error or + // replace it with an empty ERR_SQLITE_ERROR. + if (!env->can_call_into_js() || env->isolate()->HasPendingException()) { + return 0; + } + // Entered before building the payload below, because allocating it can // trigger a garbage collection that SQLite is not prepared for. CallbackDepthGuard guard(db); diff --git a/test/parallel/test-sqlite-diagnostic-channel.js b/test/parallel/test-sqlite-diagnostic-channel.js index eb9461f337d..20df20edfe1 100644 --- a/test/parallel/test-sqlite-diagnostic-channel.js +++ b/test/parallel/test-sqlite-diagnostic-channel.js @@ -234,4 +234,38 @@ suite('sqlite.db.query diagnostics channel', () => { { __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); + }); });