Skip to content

Apply subscription changes made while the sync stream is being established - #183

Merged
simolus3 merged 3 commits into
powersync-ja:mainfrom
Chubby-Studio:fix/subscription-changes-during-connect
Sep 9, 2026
Merged

Apply subscription changes made while the sync stream is being established#183
simolus3 merged 3 commits into
powersync-ja:mainfrom
Chubby-Studio:fix/subscription-changes-during-connect

Conversation

@asiergmorato

Copy link
Copy Markdown
Contributor

Fixes #157

Problem

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. With includeDefaultStreams: false and 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 DidUpdateSubscriptions notification that should have caused an immediate reconnect never reached the core.

Cause

In SyncIteration.run(), watchSyncStreams and watchCompletedCrudUploads start before the request goes out, but the merge only subscribed to localEvents after fetchSyncLines returned. BroadcastStream.dispatch drops events that have no listener, so an updateSubscriptions raised in that window (a few milliseconds locally, up to the full round trip on a slow link) was lost.

Fix

Subscribe to localEvents before the watchers start. The AsyncStream is 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

subscriptionAddedWhileConnectingReconnects holds the first mock response open, subscribes in that window, and requires a second /sync/stream request 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():

  • Before: first stream opened with buckets: 0 and stayed idle for 19.93 s; the service logged Sync stream complete right after the keep-alive, then a second stream with buckets: 16 delivered the data.
  • After: first stream still opens with buckets: 0, Sync stream complete after 70 ms, second stream with buckets: 16 300 ms later. First row visible to the app 0.59 s after sign-in instead of 20 s.

…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.
Comment thread Sources/PowerSync/Implementation/sync/StreamingSyncClient.swift Outdated
Comment thread Tests/PowerSyncTests/SyncTests.swift Outdated
Comment thread CHANGELOG.md Outdated
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 simolus3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution!

@simolus3
simolus3 merged commit e4b4cdc into powersync-ja:main Sep 9, 2026
5 checks passed
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.

~20s stall when re-fetching data after TTL eviction of a sync stream subscription (connection stays connected=true, no data delivered)

2 participants