sqlite: applyChangeset() keeps part of the changeset when the filter callback throws

未關閉
#67,841 1 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視

維護者通常 1 天內回覆

還沒有人認領這個 Issue。

評估

難度
3/5
預估耗時
1-2 天
新手友好度
72/100
Issue 類型
缺陷
描述清晰度
描述清楚
活躍度
活躍
技術堆疊
javascript, nodejs, sqlite
領域
database

研究方向

Start with the node:sqlite DatabaseSync.applyChangeset() implementation and its API documentation, then run the provided reproducer against the current build. Trace how filter callback errors affect the SQLite changeset application and whether the transaction can be rolled back. Done should establish the intended behavior and add a regression test covering a filter that throws after an earlier table was accepted.

由索引模型根據 Issue 內容生成。

描述

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.

主要語言
JavaScript
星號
124k
分支
38k
平均合併
4 天 2 小時
30 天內合併 PR
266

環境準備

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

nodejs/node 的其他 Issue

查看 nodejs/node 的全部 Issue

相似的 Issue

更多 JavaScript Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。