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

No files matched your search

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