Skip to content

Recover from some errors within sync iteration #97

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
merged 3 commits into from
Jun 30, 2025

Conversation

simolus3
Copy link
Contributor

@simolus3 simolus3 commented Jun 30, 2025

This adds the can_retry method to the new PowerSyncError type (#96). Then, in the sync iteration, we catch errors for each sync line and check whether the error is fatal or whether we allow retrying the operation.

When retrying isn't necessary, we keep the sync iteration future active. powersync_control will still return an error, but the invocation can be retried after e.g. unlocking a database or waiting for a WA-sqlite VFS to settle.

To implement this in a sound way, handling sync lines is split across two functions:

  1. StreamingSyncIteration::prepare_handling_sync_line: This method is responsible for writing data to the database, but it does not mutate the internal sync client state. It returns a SyncStateMachineTransition, which describes pending changes to be applied if the call has succeeded.
  2. StreamingSyncIteration::apply_transition then updates the internal client state.

The reason for this split is that it makes it easy to argue that we cannot possibly update in-memory state before completing writes to SQLite (thanks Rust!). This ensures that handling sync lines is idempotent wrt. underlying SQLite failures, the property we need.
This also adds tests to block the database in different stages of the sync iteration.

rkistner
rkistner previously approved these changes Jun 30, 2025
@simolus3 simolus3 changed the title WIP: Recover from some errors within sync iteration Recover from some errors within sync iteration Jun 30, 2025
@simolus3 simolus3 marked this pull request as ready for review June 30, 2025 16:41
@simolus3 simolus3 changed the base branch from refactor-errors to main June 30, 2025 16:41
@simolus3 simolus3 dismissed rkistner’s stale review June 30, 2025 16:41

The base branch was changed.

@simolus3 simolus3 requested a review from rkistner June 30, 2025 16:41
@simolus3 simolus3 force-pushed the sync-client-error-recovery branch from 3e7d976 to d9a3acf Compare June 30, 2025 16:55
@simolus3 simolus3 merged commit 2edd4c3 into main Jun 30, 2025
21 checks passed
@simolus3 simolus3 deleted the sync-client-error-recovery branch June 30, 2025 19:42
@simolus3 simolus3 mentioned this pull request Jul 8, 2025
10 tasks
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