Skip to content

A local-first personal finance app

fix rare freeze when saving a change (#9027)

Michael Clarkef0f0614a0d1
diff --git a/packages/loot-core/src/platform/server/sqlite/index.electron.ts b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
index b038e4e51e..d0993a2ce9 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.electron.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.electron.ts
@@ -71,8 +71,23 @@ export function execQuery(db: SQL.Database, sql: string) {
db.exec(sql);
}
-export function transaction(db: SQL.Database, fn: () => void) {
- db.transaction(fn)();
+export type TransactionOptions = {
+ // Take the write lock up front (`BEGIN IMMEDIATE`). Only matters on
+ // the web backend, but keep the signature the same on every platform.
+ immediate?: boolean;
+};
+
+export function transaction(
+ db: SQL.Database,
+ fn: () => void,
+ { immediate = false }: TransactionOptions = {},
+) {
+ const run = db.transaction(fn);
+ if (immediate) {
+ run.immediate();
+ } else {
+ run();
+ }
}
// **Important**: this is an unsafe function since sqlite executes
diff --git a/packages/loot-core/src/platform/server/sqlite/index.test.ts b/packages/loot-core/src/platform/server/sqlite/index.test.ts
index 01f8c240e2..e77f032b80 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.test.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.test.ts
@@ -85,6 +85,46 @@ describe('Web sqlite', () => {
expect(rows[2].number).toBe(6);
});
+ it('should support immediate transactions with rollback and nesting', async () => {
+ const db = await openDatabase();
+ execQuery(db, initSQL);
+
+ transaction(
+ db,
+ () => {
+ runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id1', 1)");
+ // Nested transactions become savepoints regardless of the mode
+ transaction(
+ db,
+ () => {
+ runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id2', 2)");
+ },
+ { immediate: true },
+ );
+ },
+ { immediate: true },
+ );
+ expect(runQuery(db, 'SELECT * FROM numbers', null, true).length).toBe(2);
+
+ const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => null);
+ expect(() => {
+ transaction(
+ db,
+ () => {
+ runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id3', 3)");
+ runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id1', 1)");
+ },
+ { immediate: true },
+ );
+ }).toThrow(/constraint failed/);
+ consoleSpy.mockRestore();
+
+ expect(runQuery(db, 'SELECT * FROM numbers', null, true).length).toBe(2);
+ // The failed transaction must have released the write lock
+ runQuery(db, "INSERT INTO numbers (id, number) VALUES ('id4', 4)");
+ expect(runQuery(db, 'SELECT * FROM numbers', null, true).length).toBe(3);
+ });
+
it('should use the crdt index for a batched cell lookup', async () => {
// Mirrors the query `compareMessages` in server/sync builds for a
// full chunk. If the planner ever falls back to a table scan on the
diff --git a/packages/loot-core/src/platform/server/sqlite/index.ts b/packages/loot-core/src/platform/server/sqlite/index.ts
index d523dc7360..0c7ecc04c1 100644
--- a/packages/loot-core/src/platform/server/sqlite/index.ts
+++ b/packages/loot-core/src/platform/server/sqlite/index.ts
@@ -140,14 +140,27 @@ export function execQuery(db: Database, sql: string) {
let transactionDepth = 0;
-export function transaction(db: Database, fn: () => void) {
+export type TransactionOptions = {
+ // Take the write lock up front (`BEGIN IMMEDIATE`) instead of on the
+ // first write. Use it for transactions that read before they write:
+ // absurd-sql reuses an IndexedDB cursor across the lock upgrade, and
+ // a read that lands just after that cursor then fails with
+ // "TransactionInactiveError" and stalls the backend.
+ immediate?: boolean;
+};
+
+export function transaction(
+ db: Database,
+ fn: () => void,
+ { immediate = false }: TransactionOptions = {},
+) {
let before, after, undo;
if (transactionDepth > 0) {
before = 'SAVEPOINT __actual_sp';
after = 'RELEASE __actual_sp';
undo = 'ROLLBACK TO __actual_sp';
} else {
- before = 'BEGIN';
+ before = immediate ? 'BEGIN IMMEDIATE' : 'BEGIN';
after = 'COMMIT';
undo = 'ROLLBACK';
}
diff --git a/packages/loot-core/src/server/db/index.ts b/packages/loot-core/src/server/db/index.ts
index 8f982b9865..6b22b3e20a 100644
--- a/packages/loot-core/src/server/db/index.ts
+++ b/packages/loot-core/src/server/db/index.ts
@@ -162,8 +162,11 @@ function resetQueryCache() {
_queryCache = new LRUCache<string, Statement>({ max: 100 });
}
-export function transaction(fn: () => void) {
- return sqlite.transaction(db, fn);
+export function transaction(
+ fn: () => void,
+ options?: sqlite.TransactionOptions,
+) {
+ return sqlite.transaction(db, fn, options);
}
export function asyncTransaction(fn: () => Promise<void>) {
diff --git a/packages/loot-core/src/server/sync/index.ts b/packages/loot-core/src/server/sync/index.ts
index 434cdb3798..a360862d8f 100644
--- a/packages/loot-core/src/server/sync/index.ts
+++ b/packages/loot-core/src/server/sync/index.ts
@@ -436,7 +436,11 @@ async function _applyMessages(messages: Message[], deferUnknownSchema = false) {
// a transaction is a separate lock/commit cycle against IndexedDB. We
// avoid any side effects to in-memory objects, and apply them after
// this succeeds.
- db.transaction(() => {
+ //
+ // `immediate` takes the write lock before the reads below. Reading
+ // first and upgrading the lock later trips an absurd-sql bug on the
+ // web (a cursor kept across the upgrade) that stalls the backend.
+ const applyInTransaction = () => {
if (checkSyncingMode('enabled')) {
// Compare the messages with the existing crdt. This filters out
// already applied messages and determines if a message is old or
@@ -538,7 +542,8 @@ async function _applyMessages(messages: Message[], deferUnknownSchema = false) {
}
newData = fetchData(idsPerTable);
- });
+ };
+ db.transaction(applyInTransaction, { immediate: true });
// The transaction succeeded, so we can update in-memory objects now
undo.appendMessages(messages, oldData);
diff --git a/upcoming-release-notes/fix-apply-transaction-freeze.md b/upcoming-release-notes/fix-apply-transaction-freeze.md
new file mode 100644
index 0000000000..8c62a2c5e3
--- /dev/null
+++ b/upcoming-release-notes/fix-apply-transaction-freeze.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [MikesGlitch]
+---
+
+Fix a rare freeze when saving a change in the browser