sqlite: applyChangeset() keeps part of the changeset when the filter callback throws
Version
main (1724da82b, v28.0.0-pre); v22.22.2 for the abort below
Platform
Linux 5.15.0-139-generic x86_64
Subsystem
sqlite
What steps will reproduce the bug?
const { DatabaseSync } = require('node:sqlite');
const make = () => {
const db = new DatabaseSync(':memory:');
db.exec('CREATE TABLE a(id INTEGER PRIMARY KEY, v); CREATE TABLE b(id INTEGER PRIMARY KEY, v)');
return db;
};
const source = make();
const session = source.createSession();
source.exec("INSERT INTO a VALUES (1, 'x'); INSERT INTO b VALUES (1, 'y')");
const target = make();
try {
target.applyChangeset(session.changeset(), {
filter(table) {
if (table === 'b') throw new Error('filter failed for b');
return true;
},
});
} catch (err) {
console.log('threw:', err.message);
}
console.log('a:', target.prepare('SELECT count(*) AS n FROM a').get().n);
console.log('b:', target.prepare('SELECT count(*) AS n FROM b').get().n);
How often does it reproduce? Is there a required condition?
Always, when the filter callback throws for a table after it has accepted another one.
What is the expected behavior? Why is that the expected behavior?
Either the changeset is not applied at all when applyChangeset() throws, or the
documentation says that the changes of the tables accepted before the exception
remain applied.
For onConflict, the documentation states: "When an error is thrown in the
conflict handler or when any other value is returned from the handler, applying
the changeset is aborted and the database is rolled back." A caller who sees
applyChangeset() throw would reasonably expect the same for filter, which the
documentation does not cover.
What do you see instead?
threw: filter failed for b
a: 1
b: 0
applyChangeset() throws, but the changes for table a have been applied. The
failed filter call makes xFilter skip the remaining tables, and
sqlite3changeset_apply() returns SQLITE_OK for the tables that were
accepted (#64823 notes that a falsy filter result is not a SQLite error).
Additional information
On v22.22.2 the same script aborts the process instead
(FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal in
node::sqlite::DatabaseSync::ApplyChangeset, exit code 134). The v22.x branch
still calls ToLocalChecked() on the filter result; #60028, which fixed this on
main and is in v24.x, does not seem to have been backported to v22.x.
This was found by an LLM-assisted static analysis tool; I reproduced both behaviours myself on a local build of main and on v22.22.2.
Reproduced on Linux with v24.22.3 and v25.6.1 (threw: filter failed for b, a: 1, b: 0), and the abort on v22.20.0 (FATAL ERROR: v8::ToLocalChecked Empty MaybeLocal, exit code 134).
For comparison, an exception from onConflict on the same changeset does roll back: with a conflicting row already in b, applyChangeset() throws and a stays empty.
The difference is in how SQLite treats the two callbacks. When onConflict throws, xConflict returns SQLITE_CHANGESET_ABORT, so sqlite3changeset_apply() rolls back what it applied and returns SQLITE_ABORT. When filter throws, the callback in Database::ApplyChangeset() sets filterCallbackFailed and returns false, which SQLite only takes as "skip this table", so the apply finishes with SQLITE_OK and keeps the tables it already applied; the pending exception then surfaces in JavaScript. xFilter has no way to abort the apply, so making this atomic would need Node to wrap the call in its own savepoint and roll back to it when the filter failed. Otherwise the documentation could say that tables accepted before the exception stay applied.
One additional regression boundary for the savepoint approach: run applyChangeset inside an already-open caller transaction, with an unrelated write before the call. A filter exception should remove the accepted changeset rows while preserving that earlier write and leaving the outer transaction usable.
I checked this on Linux / Node v24.22.3 using the issue's two-table changeset plus CREATE TABLE marker(id INTEGER). On the target:
target.exec('BEGIN; INSERT INTO marker VALUES(7); SAVEPOINT apply_boundary');
const original = new Error('filter failed for b');
try {
target.applyChangeset(session.changeset(), {
filter(table) { if (table === 'b') throw original; return true; }
});
} catch (err) {
assert.equal(err, original);
target.exec('ROLLBACK TO apply_boundary; RELEASE apply_boundary');
}
// a: 0 rows; b: 0 rows; marker: 1 row
// The subsequent outer ROLLBACK removes marker as expected.
Without the savepoint cleanup, a retains one row, as already reported. This adds an outer-transaction preservation test to the rollback test; a plain ROLLBACK in a prospective implementation would incorrectly discard the caller's marker write. The script also verified that the original thrown Error object was preserved. This is a userland boundary experiment on v24.22.3, not a test of a proposed Node patch.