-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(desktop): close saveMessage vs poll race with a pending-save counter #7559
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
Merged
kodjima33
merged 4 commits into
BasedHardware:main
from
eulicesl:feat/desktop-chat-save-race-fix
Jun 3, 2026
+306
−2
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d642932
fix(desktop): close saveMessage vs poll race with a pending-save counter
eulicesl f651c22
fix(desktop): harden pending-save counter per review
eulicesl f2ad021
fix(desktop): re-run deferred poll when pending saves drain
eulicesl 979a590
fix(desktop): keep deferred poll queued until it actually runs
eulicesl 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,76 @@ | ||
| import Foundation | ||
|
|
||
| /// Counter-based multi-holder gate for tracking in-flight persistence | ||
| /// operations. Companion to `ReentrancyGate` — that type is single-entry | ||
| /// (one holder at a time); this one allows multiple concurrent holders | ||
| /// and reports "is anything in flight right now?" via `isActive`. | ||
| /// | ||
| /// Used by `ChatProvider` to prevent the cross-platform message poll | ||
| /// from running while any `saveMessage(...)` is mid-flight. The poll | ||
| /// reads backend state to detect messages sent from other devices; if | ||
| /// it fires between a local save's request and its response, it can | ||
| /// observe the just-saved message and treat it as new. The existing | ||
| /// 200-char text-prefix merge at `pollForNewMessages` catches most of | ||
| /// these, but a counter-based suppression is defense-in-depth — | ||
| /// eliminates the race window entirely instead of relying on text | ||
| /// heuristics that fail on short common replies ("Yes", "Got it"). | ||
| /// | ||
| /// Caller contract: | ||
| /// ```swift | ||
| /// counter.begin() | ||
| /// Task { | ||
| /// do { | ||
| /// let response = try await APIClient.shared.saveMessage(...) | ||
| /// await MainActor.run { | ||
| /// // … sync state update … | ||
| /// self.counter.end() | ||
| /// } | ||
| /// } catch { | ||
| /// await MainActor.run { self.counter.end() } | ||
| /// logError(...) | ||
| /// } | ||
| /// } | ||
| /// ``` | ||
| /// | ||
| /// Both success and failure paths MUST call `end()`. Missing an `end()` | ||
| /// causes the counter to leak upward and permanently suppresses the | ||
| /// poll. `end()` is no-op when the counter is already at 0, so an | ||
| /// extra (defensive) `end()` is safe but masks bugs — prefer matched | ||
| /// pairs. | ||
| /// | ||
| /// Tested in `PendingSaveCounterTests`. | ||
| @MainActor | ||
| final class PendingSaveCounter { | ||
| private var count: Int = 0 | ||
|
|
||
| /// Invoked each time the count returns to 0 (the last in-flight save | ||
| /// completed). Lets the owner re-run any work that was suppressed | ||
| /// while saves were active — e.g. a `pollForNewMessages` cycle that | ||
| /// was deferred so it wouldn't observe a half-saved message. | ||
| var onDrained: (() -> Void)? | ||
|
|
||
| /// True when at least one save is in flight. | ||
| var isActive: Bool { count > 0 } | ||
|
|
||
| /// Visible only for tests. Production code should compare against | ||
| /// `isActive` rather than reading the raw count. | ||
| var currentCount: Int { count } | ||
|
|
||
| /// Increment the count. Call before launching a save Task (or | ||
| /// before `await`ing the inline save). | ||
| func begin() { | ||
| count += 1 | ||
| } | ||
|
|
||
| /// Decrement the count. Bounded at 0 — stray calls cannot drive | ||
| /// the counter negative, which would otherwise permanently | ||
| /// indicate "no saves in flight" even when there are. The `assert` | ||
| /// surfaces an unbalanced `begin()`/`end()` pair in debug builds | ||
| /// (zero cost in release) instead of failing silently. | ||
| func end() { | ||
| assert(count > 0, "PendingSaveCounter: unbalanced end() — no matching begin()") | ||
| guard count > 0 else { return } | ||
| count -= 1 | ||
| if count == 0 { onDrained?() } | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.