Skip to content

Fix race condition in EventManager refresh error handling#895

Draft
Copilot wants to merge 2 commits intocodex/update-error-handling-in-setuppublishersfrom
copilot/sub-pr-886-again
Draft

Fix race condition in EventManager refresh error handling#895
Copilot wants to merge 2 commits intocodex/update-error-handling-in-setuppublishersfrom
copilot/sub-pr-886-again

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 4, 2026

When multiple refreshes fired concurrently, a failing refresh could overwrite newer values written by a succeeding one — because each Task captured a current snapshot upfront, and the error handler blindly restored that stale state.

Changes

  • EventManager.swift: On fetch failure, emit nil instead of the stale current snapshot; add .compactMap { $0 } so the .sink only fires on success. Failed refreshes now leave published properties untouched entirely.
// Before: stale snapshot could overwrite newer successful state
let current = await MainActor.run { (self.calendars, self.events) }
// ...
} catch {
    promise(.success(current)) // race: may clobber a later successful refresh
}

// After: failed refresh is a no-op on published state
} catch {
    promise(.success(nil))
}
// ...
.compactMap { $0 }  // skip nils — properties unchanged on error
  • FakeEventStore.swift: Added shouldThrow flag to simulate fetch failures in tests.
  • EventManagerTests.swift: Added test_eventsPreservedOnRefreshError — uses an inverted expectation to assert no $events update is published after a failed refresh.

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: leits <12017826+leits@users.noreply.github.com>
Copilot AI changed the title [WIP] Update handling of last events during refresh errors Fix race condition in EventManager refresh error handling Mar 4, 2026
Copilot finished work on behalf of leits March 4, 2026 11:41
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.

2 participants