Apply subscription changes made while the sync stream is being established - #183
Merged
simolus3 merged 3 commits intoSep 9, 2026
Conversation
…ished A sync stream subscription added after `connect()` returned but before the first `/sync/stream` response arrived only took effect on the next keep-alive line from the service, typically 20 seconds later. On a fresh database that window covers the common "connect, then subscribe" pattern, so a client's first stream ran with no subscriptions at all and delivered nothing until then. `SyncIteration.run()` started `watchSyncStreams` and `watchCompletedCrudUploads` before the request went out, but only subscribed to `localEvents` after `fetchSyncLines` returned. `BroadcastStream.dispatch` drops events with no listener, so an `updateSubscriptions` raised in that gap never reached the core extension, which fell back to its periodic check on keep-alive. Subscribe to `localEvents` before the watchers start. The stream buffers everything dispatched until the control loop consumes it, so the change is applied as soon as the response arrives and the iteration reconnects with the new subscription set right away. The same fix covers a CRUD upload completing during connection establishment. Adds a regression test that subscribes while the first request is held open and expects a second request carrying the subscription without any keep-alive.
…elay State the invariant in the comment instead of the history, and drop the claim about completed uploads: that watcher still subscribes inside its own task, and an upload finishing before any checkpoint exists is a no-op in the core anyway. Give the test a short retry delay so its 5 s polling budget no longer coincides with the default retry interval.
simolus3
reviewed
Sep 9, 2026
The comment reads as obvious once the ordering is known, and the test's short retry delay was defensive rather than needed: the close a subscription change produces carries hide_disconnect, so the reconnect never goes through the retry path and the test runs in about 60 ms either way. Sets the CHANGELOG heading and libraryVersion to 1.16.2 for the release.
simolus3
approved these changes
Sep 9, 2026
simolus3
left a comment
Contributor
There was a problem hiding this comment.
Thanks for the contribution!
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.
Fixes #157
Problem
A sync stream subscription added after
connect()returned but before the first/sync/streamresponse arrived only took effect on the next keep-alive line from the service, typically 20 seconds later. WithincludeDefaultStreams: falseand a fresh database, the common "connect, then subscribe" pattern hits this on every first launch: the first request goes out with no subscriptions at all, the service answers with an empty checkpoint (buckets: 0), and nothing is delivered until the keep-alive triggers the periodic subscription check in the core.This is the same stall @elcode100 describes in #157, and the mechanism @simolus3 pointed at there: the
DidUpdateSubscriptionsnotification that should have caused an immediate reconnect never reached the core.Cause
In
SyncIteration.run(),watchSyncStreamsandwatchCompletedCrudUploadsstart before the request goes out, but the merge only subscribed tolocalEventsafterfetchSyncLinesreturned.BroadcastStream.dispatchdrops events that have no listener, so anupdateSubscriptionsraised in that window (a few milliseconds locally, up to the full round trip on a slow link) was lost.Fix
Subscribe to
localEventsbefore the watchers start. TheAsyncStreamis unbounded, so anything dispatched while the request is in flight is buffered and consumed as soon as the control loop starts; the core then compares the current subscriptions against the in-flight request and reconnects right away, exactly as it already does once connected. No changes to the core extension or the protocol.Test
subscriptionAddedWhileConnectingReconnectsholds the first mock response open, subscribes in that window, and requires a second/sync/streamrequest carrying the subscription without ever sending a keep-alive. It fails on 1.16.1 (times out after 5 s) and passes with the change in about 70 ms. The full package suite passes.Verified against a real service
PowerSync service 1.24.0 (self-hosted), iOS app, fresh anonymous account, subscribing four streams right after
connect():buckets: 0and stayed idle for 19.93 s; the service loggedSync stream completeright after the keep-alive, then a second stream withbuckets: 16delivered the data.buckets: 0,Sync stream completeafter 70 ms, second stream withbuckets: 16300 ms later. First row visible to the app 0.59 s after sign-in instead of 20 s.