A local-first personal finance app
fix hanging when switching accounts & reconciling (#8628)
Matt Fiddaman17a72d3c3b54
diff --git a/packages/desktop-client/src/queries/liveQuery.ts b/packages/desktop-client/src/queries/liveQuery.tsindex ba5177529b..3fc240b7b6 100644--- a/packages/desktop-client/src/queries/liveQuery.ts+++ b/packages/desktop-client/src/queries/liveQuery.ts@@ -215,11 +215,15 @@ export class LiveQuery<TResponse = unknown> {}this.onData(this.data, previousData);- this.inflightRequestId = null;}} catch (e) {console.log('Error fetching data', e);this.onError(e);+ } finally {+ // always clear the request id, to prevent an infinite loop+ if (this.inflightRequestId === reqId) {+ this.inflightRequestId = null;+ }}};}diff --git a/packages/desktop-client/src/queries/pagedQuery.test.ts b/packages/desktop-client/src/queries/pagedQuery.test.tsindex 83a9de71a6..d57bb58808 100644--- a/packages/desktop-client/src/queries/pagedQuery.test.ts+++ b/packages/desktop-client/src/queries/pagedQuery.test.ts@@ -618,4 +618,101 @@ describe('pagedQuery', () => {await wait(1000);});++ it('does not spin forever when unsubscribed during a refetch', async () => {+ // Regression test: unsubscribing while a refetch was in flight (e.g.+ // switching accounts while a scroll-triggered `fetchNext` was pending)+ // used to leave `inflightRequestId` set forever, making `fetchNext` spin+ // in an infinite microtask loop that froze the app at 100% CPU.+ const data = [];+ for (let i = 0; i < 400; i++) {+ data.push({ id: i });+ }++ // Server that blocks every page query until we release it, so the test+ // controls exactly when each in-flight request resolves.+ const pending = [];+ mockServer({+ send: async (name, args) => {+ switch (name) {+ case 'query': {+ const query = args;+ if (isCountQuery(query)) {+ return { data: data.length, dependencies: ['transactions'] };+ }+ await new Promise(resolve => pending.push(resolve));+ return {+ data: limitOffset(+ data.map(row => select(row, query.selectExpressions)),+ query.limit,+ query.offset,+ ),+ dependencies: ['transactions'],+ };+ }+ default:+ throw new Error(`Command not implemented: ${name}`);+ }+ },+ });++ const query = q('transactions').select('id');+ const paged = pagedQuery(query, {+ options: { onlySync: true, pageCount: 150 },+ });++ await wait(1);+ pending.shift()();+ await wait(1);+ expect(paged.data.length).toBe(150);++ // A scroll near the bottom starts fetching the next page...+ let fetchNextSettled = false;+ const fetchNextPromise = paged.fetchNext().then(() => {+ fetchNextSettled = true;+ });+ await wait(1);++ // ...a database change triggers a refetch while that page is loading...+ mockPublishEvent('sync-event', {+ type: 'success',+ tables: ['transactions'],+ });+ await wait(1);++ // ...and the account switch unsubscribes before either request finishes.+ paged.unsubscribe();+ while (pending.length) {+ pending.shift()();+ }++ // Pump the microtask queue a bounded number of times. Timers never fire+ // while `fetchNext` spins, so this must not wait on a timer.+ for (let i = 0; i < 2000; i++) {+ await Promise.resolve();+ }++ // The cancelled refetch must not leave a stale in-flight request id+ // behind — `fetchNext` loops on it until it clears.+ const internals = paged as unknown as {+ _inflightRequestId: number | null;+ };+ const staleRequestId = internals._inflightRequestId;+ // Break the loop if it is spinning, so a regression fails cleanly here+ // instead of hanging the test worker.+ internals._inflightRequestId = null;+ expect(staleRequestId).toBeNull();++ // fetchNext recovers: release the page request it retries and let it+ // finish.+ for (let i = 0; i < 20 && !fetchNextSettled; i++) {+ while (pending.length) {+ pending.shift()();+ }+ await wait(1);+ }+ await fetchNextPromise;+ expect(fetchNextSettled).toBe(true);+ expect(paged.data.length).toBe(300);+ });});diff --git a/upcoming-release-notes/fix-account-switch-freeze.md b/upcoming-release-notes/fix-account-switch-freeze.mdnew file mode 100644index 0000000000..6dc229bf3c--- /dev/null+++ b/upcoming-release-notes/fix-account-switch-freeze.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [matt-fidd]+---++Fix intermittent app freezes with 100% CPU usage