Improve error recovery in sync client - #218
Open
simolus3 wants to merge 2 commits into
Open
Conversation
simolus3
marked this pull request as ready for review
August 20, 2026 11:12
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.
This simplifies the control flow in the sync client to make sure we handle errors in a consistent manner. This fixes a bug I noticed while working on the Kotlin SDK: If we send say an invalid sync line, the client errors and the state is then set to
ClientState::Idle. SDKs also invokepowersync_control('stop')after errors, which then does nothing as the client has already been reset. This causes SDKs to miss the hint that the sync status should be set to disconnected.Currently, the sync client is implemented as an
asyncfuture that is polled on each event. To be able to recover from some errors (namely wa-sqlite returningSQLITE_BUSYfor async retries), we already have a manual state machine:At this point, we might as well call that manually instead of using the future as an indirection. So, this migrates all events to be based on that state machine (we only used it when handling sync lines before) and drops the async method.
This ensures that we consistently update the sync status when the client is reset, and makes all errors recoverable (which in many cases doesn't make sense: An invalid sync line should lead to a restart. But crucially, this lets the SDK be in control of how errors are handled). This is safe because
powersync_controlcan only be called in transactions and because the only step capable of erroring has an immutable reference. Thus, errors are guaranteed to cause no further side-effects, everything is reset to the state before the failing call.Apart from consistently sending a disconnect status update before stop instructions, the sync client behaves the same and eixsting tests continue to work.
AI use: Reviewed with Claude Code, which found minor issues I've fixed by hand.