-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
[AI] Fix batchMessages losing nested batch messages on outer failure #7225
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dearlordylord
wants to merge
4
commits into
actualbudget:master
Choose a base branch
from
dearlordylord:fix/batch-messages-concurrency
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+179
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
293d8c4
[AI] fix batchMessages losing nested batch messages on outer failure
dearlordylord 943f7af
[AI] fix lint errors and add release notes
dearlordylord b024ec1
[AI] use lint suppress instead of dummy Error allocation
dearlordylord 5101dad
[AI] use Error | undefined instead of unknown
dearlordylord File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
packages/loot-core/src/server/sync/batchMessages.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { Timestamp } from '@actual-app/crdt'; | ||
|
|
||
| import * as db from '../db'; | ||
|
|
||
| import { batchMessages, sendMessages, setSyncingMode } from './index'; | ||
|
|
||
| beforeEach(() => { | ||
| setSyncingMode('disabled'); | ||
| return global.emptyDatabase()(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| global.resetTime(); | ||
| setSyncingMode('disabled'); | ||
| }); | ||
|
|
||
| describe('batchMessages concurrency', () => { | ||
| it('preserves nested batch messages when outer batch fails', async () => { | ||
| // Regression test: nested batch messages must not be silently lost | ||
| // when the outer batch throws after the inner batch completes. | ||
| // | ||
| // Trace: A starts outer batch, sends msgA, yields. B starts nested | ||
| // batch, sends msgB, completes. A resumes and throws. Fix: messages | ||
| // are sent before rethrowing. | ||
|
|
||
| let resolveYield: () => void; | ||
| const yieldPromise = new Promise<void>(resolve => { | ||
| resolveYield = resolve; | ||
| }); | ||
|
|
||
| let innerBatchReturned = false; | ||
|
|
||
| // Start operation A - it will yield, allowing B to interleave | ||
| const operationA = batchMessages(async () => { | ||
| await sendMessages([ | ||
| { | ||
| dataset: 'transactions', | ||
| row: 'row-a', | ||
| column: 'amount', | ||
| value: 100, | ||
| timestamp: Timestamp.send(), | ||
| }, | ||
| ]); | ||
|
|
||
| // Yield to event loop - B will run during this await | ||
| await yieldPromise; | ||
|
|
||
| // A fails after B has completed | ||
| throw new Error('operation A failed'); | ||
| }); | ||
|
|
||
| global.stepForwardInTime(); | ||
|
|
||
| // B runs while A is yielded. IS_BATCHING is true, so B is nested. | ||
| await batchMessages(async () => { | ||
| await sendMessages([ | ||
| { | ||
| dataset: 'transactions', | ||
| row: 'row-b', | ||
| column: 'amount', | ||
| value: 200, | ||
| timestamp: Timestamp.send(), | ||
| }, | ||
| ]); | ||
| }); | ||
| innerBatchReturned = true; | ||
|
|
||
| // Resume A, which will throw | ||
| resolveYield(); | ||
|
|
||
| // A still fails | ||
| await expect(operationA).rejects.toThrow('operation A failed'); | ||
|
|
||
| // B returned successfully | ||
| expect(innerBatchReturned).toBe(true); | ||
|
|
||
| // FIX: B's message IS applied despite outer batch failure. | ||
| // All accumulated messages are sent before the error is rethrown. | ||
| const rowB = await db.first<{ id: string }>( | ||
| 'SELECT * FROM transactions WHERE id = ?', | ||
| ['row-b'], | ||
| ); | ||
| expect(rowB).not.toBeNull(); | ||
|
|
||
| // A's message is also applied (consistent with non-batched behavior | ||
| // where each sendMessages call applies immediately) | ||
| const rowA = await db.first<{ id: string }>( | ||
| 'SELECT * FROM transactions WHERE id = ?', | ||
| ['row-a'], | ||
| ); | ||
| expect(rowA).not.toBeNull(); | ||
| }); | ||
|
|
||
| it('applies all messages when outer batch succeeds', async () => { | ||
| let resolveYield: () => void; | ||
| const yieldPromise = new Promise<void>(resolve => { | ||
| resolveYield = resolve; | ||
| }); | ||
|
|
||
| const operationA = batchMessages(async () => { | ||
| await sendMessages([ | ||
| { | ||
| dataset: 'transactions', | ||
| row: 'row-a', | ||
| column: 'amount', | ||
| value: 100, | ||
| timestamp: Timestamp.send(), | ||
| }, | ||
| ]); | ||
|
|
||
| // Yield to event loop | ||
| await yieldPromise; | ||
| }); | ||
|
|
||
| global.stepForwardInTime(); | ||
|
|
||
| // B runs as nested batch | ||
| await batchMessages(async () => { | ||
| await sendMessages([ | ||
| { | ||
| dataset: 'transactions', | ||
| row: 'row-b', | ||
| column: 'amount', | ||
| value: 200, | ||
| timestamp: Timestamp.send(), | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| // Resume A (succeeds) | ||
| resolveYield(); | ||
| await operationA; | ||
|
|
||
| // Both messages should be applied | ||
| const rowA = await db.first<{ id: string; amount: number }>( | ||
| 'SELECT * FROM transactions WHERE id = ?', | ||
| ['row-a'], | ||
| ); | ||
| expect(rowA).not.toBeNull(); | ||
|
|
||
| const rowB = await db.first<{ id: string; amount: number }>( | ||
| 'SELECT * FROM transactions WHERE id = ?', | ||
| ['row-b'], | ||
| ); | ||
| expect(rowB).not.toBeNull(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| category: Bugfix | ||
| authors: [dearlordylord] | ||
| --- | ||
|
|
||
| Fix batchMessages silently losing nested batch messages when outer batch fails |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.