fix(sync): stop a pull dropping a saved query whose keyword another query held - #3087
Merged
Merged
Conversation
|
Preview deployment for your docs. Learn more about Mintlify Previews.
💡 Tip: Enable Automations to automatically generate PRs for you. |
datlechin
force-pushed
the
fix/sync-favorites-apply-before-token
branch
2 times, most recently
from
September 23, 2026 15:50
aaf9ed3 to
2559c00
Compare
This branch was successfully deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
A saved query pulled from iCloud could be lost for good. Mac A deletes query X (keyword
rev) and saves a new query Y with the same keyword. Mac B pulls both in one batch, stores neither, and still reports a successful sync. Y never arrives again, because the pull's change token had already moved past it. Found while investigating #2505.Root cause
SyncCoordinator.applyRemoteChangesapplied saved queries and their folders in a detachedTaskthat nothing awaited, andapplyPullResultsaved the change token and the record cache straight after. A write that failed later had already been acknowledged to CloudKit.favoriteshas a unique index on(keyword, connection_id)thatON CONFLICT(id) DO UPDATEdoes not cover, so Y's upsert failed while X still heldrev(measured withsqlite3:UNIQUE constraint failed: favorites.keyword, favorites.connection_id). Then X was deleted.SQLFavoriteManager.applyRemoteFavoritereturned early on a failed write and reported nothing.Fix
RemoteSQLFavoriteBatch, andSQLFavoriteManager.applyRemoteapplies it in order before anything is acknowledged: favorite deletions, folders, favorites, then folder deletions. It returns.applied,.skippedor.failed.applyPullResultsaves the token and the record cache only when every store, saved queries included, stored its part. Otherwise the next pull replays the batch.BEGIN IMMEDIATEtransaction: the older query (earliestcreatedAt, then the smaller id) keeps the keyword, the other loses it and is marked dirty so the result syncs.createdAttravels with the record, so every Mac picks the same winner.docs/features/favorites.mdxsays which query keeps a clashing keyword.Tests
SyncCoordinatorSQLFavoritePullTests: a refused favorite holds back the token and cache and reports the pull as not acknowledged; a stored one commits both; a delete and a create with the same keyword in one pull both land; two Macs claiming one keyword leave it with the older query.SQLFavoriteRemoteApplyTests: ordering, outcomes, and remote deletions of a query or a folder removing their dirty marks.RemoteFavoriteKeywordResolverTests: the winner rules, including global queries.SQLFavoriteDeletionSyncTests,SQLFavoriteStorageTests,SQLFavoriteScopeChangeTests,SQLFavoriteFolderScopeTests,SyncChangeTrackerTests,SyncCoordinatorTokenExpiryTests,FavoriteTablesStorageTests, andSyncErrorTestsinTableProSyncTests.Risks
sql_favorites.dbcannot be opened or written, every pull that carries a saved query now fails visibly and replays, for every record type, until the store works again. The store still stays unavailable until relaunch after a failed open (existing behaviour, not changed here).Deliberately not fixed here
An edit made while a sync is running can be reverted by that sync's own echo. Edit query F, a sync starts and pushes it, edit F again during the round trip: the push clears F's dirty flag, the pull brings back the first version and overwrites the second, and the next sync finds F clean, so the second edit is lost everywhere.
A fix was written and taken through three reviews. Each round found new defects in the code the previous round added, so it was removed from this PR. What a correct fix has to handle:
SyncChangeTracker.isSuppressedis process-wide. Any dirty mark made off the main actor while a pull applies is dropped.It needs its own design: task-scoped suppression, per-record edit stamps, per-record dirty marking, and an engine seam so the push, clear and pull sequence can be tested.