Skip to content

Sync: a conflict from a server behind the Mac's journal is sent once, not forever - #1115

Open
naman0r wants to merge 2 commits into
yetanotherbrowser:mainfrom
naman0r:fix/sync-conflict-retry-loop
Open

naman0r wants to merge 2 commits into
yetanotherbrowser:mainfrom
naman0r:fix/sync-conflict-retry-loop

Conversation

@naman0r

@naman0r naman0r commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

What changes

When the sync server holds an older version of a record than the Mac's journal, the Mac sends the record once more on the server's version and goes quiet. Before, it sent the same change about twice a second for as long as Yab was open, using a core and uploading tens of KB a second.

Fixes #1088

Why it happened

A push whose base is not the server's version comes back as a conflict. SyncClient.heard hands conflicts to take(conflicts) (Sources/SyncKit/SyncClient.swift:323-333), which merges them through Journal.receive. That saves the merge at max(version, had?.version ?? 0) (Sources/SyncKit/Journal.swift:799), so when the Mac's version is the higher one, it keeps it. The record stays dirty, push() runs again at once (SyncClient.swift:340) on the same base, and the server answers with the same conflict, forever.

The fix: after a conflict is merged, the record takes the version the server gave, keeping the dirty flag the merge left (Journal.conflicted, called right after take(conflicts)). The next push lands on the server's real version and is accepted. The version is taken only when the server's record is already in the local one (merging it again changes nothing), checked and saved in one step: take and the app's deliver both swallow a failed merge, and an unmerged record sent on the server's version would replace the fields only the server has. Journal.receive keeps its max, since catch-up deliveries rely on it never lowering a version.

Why no change can be lost

  • Only the stored version changes. The merged record and its dirty flag are exactly what the merge produced, and the merge keeps the newer change of each field, the Mac's and the other device's.
  • A merge that didn't happen (a failed write, or deliver turning the record away) keeps the old version, so the next push meets the conflict again and merges then, as on main.
  • If the merge differs from the server's copy, it stays dirty and is pushed on the right version, so the Mac's newer fields reach the server.
  • If the merge equals the server's copy, the server already has it, and the record is correctly clean. Before, that clean record kept a version that was too high, and its next edit would have started the loop again.
  • An ordinary conflict, where the server is ahead, is unchanged: max already gave the server's version there, and this sets the same value.
  • The app merges through SyncCenter.deliver instead of Journal.receive, but that returns before take does, so the version is checked after the merge on both paths.

Proof

  • Logic: a test that fails on main and passes here: swift test --filter 'SyncClientTests/aConflictWithAServerBehindIsSentOnTheVersionGiven'

The test uses MemoryServer: the iPhone fills the account first (version 1), then the Mac starts with a dirty record at version 5 that changes another field. MemoryServer now counts pushes by device.

On main (fix reverted, test kept):

✘ Test aConflictWithAServerBehindIsSentOnTheVersionGiven() recorded an issue at SyncClientTests.swift:138:9: Expectation failed: await until(a) { a.journal.pending().isEmpty }
✘ Test aConflictWithAServerBehindIsSentOnTheVersionGiven() recorded an issue at SyncClientTests.swift:140:9: Expectation failed: (pushes → 9049) == 2
✘ Test aConflictWithAServerBehindIsSentOnTheVersionGiven() recorded an issue at SyncClientTests.swift:142:9: Expectation failed: await until(b) { b.journal.record(key)?["title"] == "Mac" }

9,049 pushes in the two seconds the test waits, and nothing settles. With the fix it passes: the Mac pushes exactly 2 times (one conflict, then accepted), keeps the iPhone's url, and the iPhone gets the Mac's title.

  • Logic: a merge that didn't happen leaves the server's fields alone: swift test --filter 'SyncClientTests/aConflictLeftUnmergedKeepsTheServersFields'

The Mac's deliver drops what it is given, so the conflict is never merged. On the first commit of this branch (292808d), the unmerged record went on the server's version and the server lost the iPhone's url:

✘ Test aConflictLeftUnmergedKeepsTheServersFields() recorded an issue at SyncClientTests.swift:168:9: Expectation failed: (kept?["url"] → nil) == ("https://b.test" → .string("https://b.test"))

With the fix it passes: the record keeps its old version, and the server keeps url.

Checked on

  • Checked through tests only, against the in-memory MemoryServer, on macOS 27.0. No app build, and no real sync server or account.
  • Engine(s) tried: not about a page

Checklist

  • swift test passes (or the suites this touches: list them): SyncClientTests, SyncKitTests, SyncFlushTests, SyncStaleReturnTests, SyncWireTests, 68 tests, all pass
  • ./design-check --head is clean, or each exception says why with // design:
  • Anything that changes how Yab looks was approved in the issue first (nothing visible changes)
  • A new menu item is also a command in the field; a new Settings row is classified in NativeActions.swift (none added)
  • Words on screen and in docs are American English, with no em dashes

Not verified

  • The fix in a running Yab against the real sync server, including the reporter's 17-record state.
  • Sync suites other than the five above, and the full swift test.
  • A conflict whose blob the Mac can't open. It still goes through the existing rebase path, which already uses the server's version, so this change leaves it as it was.

… not forever

When the server held an older version of a record than the Mac's journal
(sync set up again over an old sync.sqlite), every push of the record came
back as a conflict. Journal.receive saved the merge at the higher of the two
versions, so the Mac kept its own, the record stayed dirty, and the next push
went on the same base and met the same conflict, about twice a second.

A merged conflict now takes the version the server gave, with the dirty flag
the merge left, so the next push lands on top of it and is accepted.
@naman0r
naman0r marked this pull request as ready for review November 24, 2026 06:09
@greptile-apps

greptile-apps Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the new check addresses the previous risk of erasing server-only fields.

Summary

Prevents repeated conflicts when the server's record version is behind the journal.

  • Merged sync conflicts retry using the server's version.

Code review found no new actionable issues. Tests were not run during this review; the PR reports passing targeted suites.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Server returns conflict] --> B[Try to merge server record]
  B --> C{Merging again changes stored record?}
  C -->|Yes| D[Keep old version]
  C -->|No| E[Use server version]
  E --> F[Push remaining local changes]
  D --> G[Next push meets conflict again]
Loading

Reviews (2) · Last reviewed commit: "Sync: a conflict that failed to merge no..." · Reviewed by Greptile

Comment thread Sources/SyncKit/SyncClient.swift Resolved
…'s fields

The version the server gave was taken even when the merge of its record
didn't happen: a failed write in Journal.receive, or the app's deliver
turning the record away, both swallow the error. The unmerged record then
went on the server's version and replaced the fields only the server had.

The version is now taken only when the server's record is already in the
local one, checked and saved in one step; otherwise the next push meets the
conflict again and merges then.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync (low urgency): a conflict from a server that is behind the Mac's journal is pushed again forever

1 participant