Queue a follow-up sync for requests made mid-sync
Problem
Syncable#sync_later reuses any visible sync of the syncable, including one that is already running. Window expansion only applies to a pending sync, so a request that arrives while the sync runs is silently dropped. Whatever triggered it (an entry created, edited or deleted after the running sync had read the account's entries) does not reach the account's balances until some unrelated later sync.
How it showed up: creating a few hundred transactions through the API in a burst. Each create calls sync_account_later. The last rows landed while the account's sync was running, so the account's balance history stopped a few days short and the account balance was wrong (off by the sum of those rows) until a manual sync. The same can happen when a user edits a transaction while that account is syncing.
#3871 proposed queuing follow-up syncs for this; it was closed without being merged.
#4209 (Serialize syncs of the same account, item or family) takes the other half of the problem: it runs Sync#perform under a per-syncable Postgres advisory lock and retries a blocked job, so two syncs of one syncable never run at once, however long the first takes. Its sync_later still joins a visible running sync, so a request made mid-sync is still folded into a sync that has already read the data. This PR is the complement to it, not a replacement.
Before and after
Before: a request made while the sync runs joins it, but that sync has already read the entries.
sequenceDiagram
participant W as Writes (API, user edits)
participant SL as sync_later
participant S1 as Running sync
S1->>S1: reads the account's entries
W->>SL: entry created mid-sync
SL-->>S1: joins the running sync
S1->>S1: calculates balances without the new entry
Note over S1: Balance stays wrong until some later, unrelated sync
After: the request queues one follow-up sync, later requests join it, and it waits for the running sync to finish.
sequenceDiagram
participant W as Writes (API, user edits)
participant SL as sync_later
participant S1 as Running sync
participant S2 as Follow-up sync
S1->>S1: reads the account's entries
W->>SL: entry created mid-sync
SL->>S2: queues a pending sync
W->>SL: more entries
SL-->>S2: join it, widening its window
S2->>S2: SyncJob starts while the running sync is still going, retries in 5 s
S1->>S1: finishes
S2->>S2: runs and reads every entry
Note over S2: One extra sync per burst, and the balance includes every write
Change
sync_lateronly joins a pending sync (as before, widening its window and adopting the parent). If the syncable's sync is already running, it queues a new pending sync; later requests join that one while it waits, so a burst of writes during a sync produces one follow-up, not one per write.SyncJobholds a pending sync back (re-enqueues itself after 5 seconds) while another sync of the same syncable is running, so two syncs never calculate the same syncable at once. Running syncs older thanSync::VISIBLE_FORare not waited for, the same rulesync_lateralready uses to treat them as lost. #4209's lock covers that case without the time limit; once it merges, this wait becomes redundant and this PR will be rebased onto it to drop it.- Sophtron, SnapTrade and Plaid had each worked around the dropped request with a job that polled until the sync finished and then called
sync_lateragain:SophtronInitialLoadJob,SnaptradeFollowUpSyncJobandPlaidFollowUpSyncJob. The generic follow-up now covers them (Sophtron's with the initial-load window), so each job only added another sync. - The second commit retires them. The callers, SnapTrade's controller actions and
PlaidItem#sync_later_with_provider_refresh, callsync_later, andsync_later_with_follow_upis gone. The three jobs stay for one release as shims that just callsync_later, so a job queued by the previous release still makes its request.
Code that runs inside a sync only schedules syncs of other records (items schedule their accounts, the family schedules items and manual accounts), so a sync never queues a follow-up for itself.
Validation
- New tests:
SyncableInterfaceTest(run for Account, Family, PlaidItem, Trading212Item) covers a request during a running sync queuing one follow-up and further requests joining it;SyncJobTestcovers the wait and that syncs of other syncables are not held back. Onmain, all of them fail except the last one, which guards against over-waiting. SnaptradeItemTest,PlaidItemTestand the SnapTrade OAuth callback test check that a sync requested during an active sync queues a pending follow-up. Each shim job has a test that it requests a sync; making one do nothing fails its test.bin/rails test: full suite green at each commit (14,325 runs at the second commit, 0 failures).- Rebased onto main 6aa8cf218 (2026-11-21):
SnaptradeItemsControllerkeeps main'scredentials_configured?check withsync_later, and thepersonal_keyaction #5032 added now callssync_lateras well, since this PR removessync_later_with_follow_up(its controller test asserts the sync is queued). Full suite at each commit: 18,148 and 18,145 runs, 0 failures. - Codex round (2026-11-21): comments that said a request joins or reuses a running sync are updated in the commit that changes
sync_later(account, TrueLayer and Enable Banking syncers,DataCacheClearJob, the Wise linked-account job andWiseItem, SnapTrade's follow-up method, Sophtron's reprocess job). Full suite at each commit: 18,148 and 18,145 runs, 0 failures.Api::V1::TransactionsControllerTest#test_index_avoids_per-transaction_transfer_queriesis flaky on main (17 vs 18 queries, both ways; 2 of 8 local runs on plain main fail), unrelated to this PR. bin/rubocopon the changed files: no offenses.bin/brakeman: no warnings.- No UI, ERB or JavaScript changes, so system tests, erb_lint and Biome do not apply.
Notes
- Behaviour change: a request during a running sync now costs one extra sync of that syncable after it finishes, instead of being dropped.
SophtronInitialLoadJob,SnaptradeFollowUpSyncJobandPlaidFollowUpSyncJobcan be deleted one release after this one. Until then, any copy queued before the deploy requests its sync as before.- Depends on #4209 for syncs that run longer than
Sync::VISIBLE_FOR: on its own, a follow-up queued during such a sync starts beside it at the five-minute mark, as any new sync does today; with #4209's lock it waits.
🤖 Generated with Claude Code
Summary by cubic
Sync requests made while another sync of the same syncable is running are no longer dropped; they queue a follow-up sync that runs once the current sync finishes. A burst of writes during a sync (for example, a large API import) previously left the last entries missing from the account's balances until some unrelated later sync.
sync_lateronly joins a pending sync; requests during a running sync create one shared follow-up.SyncJobholds a follow-up back, re-enqueuing it after 5 seconds while another sync of the same syncable runs, so two syncs never calculate the same syncable at once.- The polling waits in
SophtronInitialLoadJob,SnaptradeFollowUpSyncJob, andPlaidFollowUpSyncJobare gone; the jobs remain for one release as shims that just callsync_later, so copies queued before deploy still run.
Behavior change: a request made while a sync runs now costs one extra sync of that syncable instead of being silently dropped.
Summary by CodeRabbit
-
New Features
- Sync requests made while another sync is running are queued as follow-ups and held until the active sync completes.
- Additional requests reuse the pending follow-up and can expand its date range.
- Syncs for other items can proceed without waiting.
- Follow-up syncs retain their balances-only setting when rescheduled.
-
Bug Fixes
- Initial loads now enqueue a sync directly, including when another sync is already running.