Skip to content

A local-first personal finance app

[AI] Fix bank sync deadlock from nested mutator (#8111)

Matiss Janis Aboltinscd58c7564d32
diff --git a/packages/loot-core/src/server/accounts/app-bank-sync.test.ts b/packages/loot-core/src/server/accounts/app-bank-sync.test.ts
index ac546364b8..455bab4da9 100644
--- a/packages/loot-core/src/server/accounts/app-bank-sync.test.ts
+++ b/packages/loot-core/src/server/accounts/app-bank-sync.test.ts
@@ -2,6 +2,7 @@
import * as asyncStorage from '#platform/server/asyncStorage';
import * as db from '#server/db';
import { loadMappings } from '#server/db/mappings';
+import { isMutating, runHandler, runMutator } from '#server/mutators';
import { app } from './app';
import * as bankSync from './sync';
@@ -220,3 +221,40 @@ describe('accountsBankSync', () => {
expect(account!.bank_sync_status).toBe('failed');
});
});
+
+describe('bank sync handlers must not nest mutators', () => {
+ it('does not register the sync handlers as mutating methods', () => {
+ expect(isMutating(app.handlers['accounts-bank-sync'])).toBe(false);
+ expect(isMutating(app.handlers['simplefin-batch-sync'])).toBe(false);
+ });
+
+ it('completes when run through runHandler even though the sync runs its own mutator', async () => {
+ insertBank({ id: 'bank1', bank_id: 'gc-bank', name: 'GoCardless' });
+ await db.insertAccount({
+ id: 'acct1',
+ name: 'Checking',
+ bank: 'bank1',
+ account_id: 'ext-1',
+ account_sync_source: 'goCardless',
+ });
+
+ vi.mocked(bankSync.syncAccount).mockImplementation(async () => {
+ await runMutator(async () => undefined);
+ return { added: [], updated: [], updatedPreview: [] };
+ });
+
+ let timer: ReturnType<typeof setTimeout> | undefined;
+ const result = await Promise.race([
+ runHandler(app.handlers['accounts-bank-sync'], { ids: ['acct1'] }),
+ new Promise<never>((_, reject) => {
+ timer = setTimeout(
+ () => reject(new Error('bank sync deadlocked')),
+ 2000,
+ );
+ }),
+ ]);
+ clearTimeout(timer);
+
+ expect(result.errors).toEqual([]);
+ });
+});
diff --git a/packages/loot-core/src/server/accounts/app.ts b/packages/loot-core/src/server/accounts/app.ts
index 4f08b59753..a8f84cfc65 100644
--- a/packages/loot-core/src/server/accounts/app.ts
+++ b/packages/loot-core/src/server/accounts/app.ts
@@ -1745,7 +1745,7 @@ app.method('pluggyai-accounts', pluggyAiAccounts);
app.method('akahu-accounts', akahuAccounts);
app.method('gocardless-get-banks', getGoCardlessBanks);
app.method('gocardless-create-web-token', createGoCardlessWebToken);
-app.method('accounts-bank-sync', mutator(accountsBankSync));
-app.method('simplefin-batch-sync', mutator(simpleFinBatchSync));
+app.method('accounts-bank-sync', accountsBankSync);
+app.method('simplefin-batch-sync', simpleFinBatchSync);
app.method('transactions-import', mutator(undoable(importTransactions)));
app.method('account-unlink', mutator(unlinkAccount));
diff --git a/upcoming-release-notes/8113.md b/upcoming-release-notes/8113.md
new file mode 100644
index 0000000000..2478300155
--- /dev/null
+++ b/upcoming-release-notes/8113.md
@@ -0,0 +1,6 @@
+---
+category: Bugfixes
+authors: [MatissJanis]
+---
+
+Fix bank sync hanging indefinitely (most visible on mobile, where the sync spinner never stopped).