Repository navigation
perf(mobile): stop replaying stored read state on every resume #9090
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
Changes from 1 commit
c43b44c
36204c2
f0cc23b
29be4ea
4eff9e4
eefce94
8244942
c620e9d
fb1ba45
b7f17da
388e627
56a161a
937bc7b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…read A reconnect resubscribed the live read-state subscription before the session replayed it, so the new socket carried two identical REQs. Both EOSEs reported ready, and the second, landing while the first snapshot downloaded, read the whole snapshot again on every clean reconnect. The session already keeps live subscriptions across reconnects and replays them, so a reconnect now opens a subscription only when the previous one was lost (terminal CLOSED or failed subscribe), and the replay's ready requests the single read. This drops the resubscribe flag and the wait for an in-flight publish. A publish's failed snapshot read now settles its slot in the read ordering. Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Signed-off-by: Tom Brow <[email protected]>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -164,13 +164,12 @@ class ReadStateManager { | |
| void Function()? _unsubscribeLive; | ||
| bool _initialized = false; | ||
| bool _initialSeedComplete = false; | ||
| // The one sync loop, while it runs. Each pass resubscribes if asked, then | ||
| // reads the snapshot, so two passes never each install a subscription and | ||
| // leak one. A request before a pass starts its read joins that read; a | ||
| // request after runs one more pass. | ||
| // The one sync loop, while it runs. Each pass opens the live subscription | ||
| // if it is missing, then reads the snapshot, so two passes never each | ||
| // install a subscription and leak one. A request before a pass starts its | ||
| // read joins that read; a request after runs one more pass. | ||
| Future<void>? _syncing; | ||
| bool _snapshotWanted = false; | ||
| bool _resubscribeWanted = false; | ||
|
|
||
| bool _disposed = false; | ||
| bool _isPublishing = false; | ||
|
|
@@ -224,7 +223,7 @@ class ReadStateManager { | |
| return; | ||
| } | ||
|
|
||
| await _requestSync(resubscribe: true); | ||
| await _requestSync(); | ||
| if (_localContexts.isNotEmpty || | ||
| _pending.isNotEmpty || | ||
| _carriedOverrides.isNotEmpty) { | ||
|
|
@@ -284,20 +283,22 @@ class ReadStateManager { | |
| await _publish(); | ||
| } | ||
|
|
||
| /// Resubscribe and read the snapshot after a reconnect. | ||
| /// Catch up after a reconnect. The session replays the live subscription | ||
| /// on the new socket, and its ready reads the snapshot, so this reads only | ||
| /// when it has to open a subscription that was lost (a terminal CLOSED or a | ||
| /// failed subscribe). | ||
| Future<void> reinitializeRemote() async { | ||
| if (_disposed || !_remoteEnabled || !_initialized) return; | ||
| debugPrint('[ReadStateManager] reinitializeRemote'); | ||
| await _requestSync(resubscribe: true); | ||
| if (_unsubscribeLive == null) await _requestSync(); | ||
| _schedulePublish(); | ||
| _onChanged(); | ||
| } | ||
|
|
||
| /// Asks the sync loop for a snapshot read, after a resubscribe if | ||
| /// [resubscribe]. Completes when the loop has nothing left to do. | ||
| Future<void> _requestSync({bool resubscribe = false}) { | ||
| /// Asks the sync loop for a snapshot read. Completes when the loop has | ||
| /// nothing left to do. | ||
| Future<void> _requestSync() { | ||
| _snapshotWanted = true; | ||
| if (resubscribe) _resubscribeWanted = true; | ||
| final syncing = _syncing; | ||
| if (syncing != null) return syncing; | ||
| // Set before the loop starts: a subscription can report ready from | ||
|
|
@@ -310,18 +311,15 @@ class ReadStateManager { | |
|
|
||
| Future<void> _runSync(Completer<void> done) async { | ||
| try { | ||
| while ((_snapshotWanted || _resubscribeWanted) && !_disposed) { | ||
| if (_resubscribeWanted) { | ||
| _resubscribeWanted = false; | ||
| if (_isPublishing) await _publishCompleter?.future; | ||
| _unsubscribeLive?.call(); | ||
| _unsubscribeLive = null; | ||
| while (_snapshotWanted && !_disposed) { | ||
| if (_unsubscribeLive == null) { | ||
| await _startLiveSubscription(); | ||
| if (_disposed) return; | ||
| } | ||
| _snapshotWanted = false; | ||
| // A failed read is retried by the next publish, which reads the | ||
| // snapshot in full after a failure. | ||
| // A failed read waits for the next trigger: the publish scheduled | ||
| // here, which reads the snapshot in full after a failure, or a later | ||
| // ready, resume or reconnect. | ||
| if (!await _fetchAndMerge()) _schedulePublish(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking, wording only. The retry chain here is one publish deep. If that publish's snapshot also fails, Related nit: the publish read at
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Both done in c620e9d4b5. The comment now says a failed read waits for the next trigger (the publish it schedules, or a later ready, resume or reconnect), and |
||
| } | ||
| } catch (e) { | ||
|
|
@@ -478,13 +476,14 @@ class ReadStateManager { | |
| // With limit 0 nothing replays marks published before the REQ went | ||
| // live, so read the snapshot every time it goes live: its EOSE, which | ||
| // can come after the 500 ms fallback on a slow AUTH, on a reconnect's | ||
| // replay, or after a CLOSED's retry. The sync loop folds a ready during its own subscribe | ||
| // into the read that follows. | ||
| // replay, or after a CLOSED's retry. | ||
| onStatusChanged: (status) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤖 Blocker: on cold start, a peer mark published before the held REQ registers is never received. If Scenario: the user cold-launches the phone with slow AUTH. The snapshot reads old state, then the desktop marks a channel read, then AUTH completes and the REQ registers. The phone shows that channel unread until the next resume or a publish that reads the snapshot. The same 500 ms fallback applies to the subscription Fix: also catch up when the first |
||
| if (status == RelaySubscriptionStatus.ready && !_disposed) { | ||
| unawaited(_requestSync()); | ||
| } | ||
| }, | ||
| // The session dropped it, so the next reconnect opens a new one. | ||
| onClosed: (_) => _unsubscribeLive = null, | ||
| ); | ||
| if (_disposed) { | ||
| unsub.call(); | ||
|
|
@@ -620,7 +619,13 @@ class ReadStateManager { | |
| var snapshot = <NostrEvent>[]; | ||
| if (_relaySession != null) { | ||
| final read = ++_snapshotsStarted; | ||
| final events = await _relaySession.fetchReadStateSnapshot(); | ||
| final List<NostrEvent> events; | ||
| try { | ||
| events = await _relaySession.fetchReadStateSnapshot(); | ||
| } catch (_) { | ||
| _settleSnapshot(read, succeeded: false); | ||
| rethrow; | ||
| } | ||
| _settleSnapshot(read, succeeded: true); | ||
| if (!_servesThisCommunity || | ||
| !_ownsJournal || | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 [P2] Reopen subscriptions retired by a session rebuild
On cold start, the eager badge provider can initialize this manager after community/config resolves but before auth completes. Auth completion rebuilds the same
RelaySessionNotifier, whose_dispose()clears_liveSubscriptionswithoutonClosed; this manager survives because it watches the notifier identity, not auth. It retains the stale cleanup callback (including whensubscribereturns after its 500 ms fallback), so this guard skips reopening on every reconnect and no REQ remains to emitready: incoming cross-device read updates stop. Track subscription ownership across session rebuilds and invalidate/reopen it, including an in-flight subscribe; extend the provider lifecycle journey to cover that startup ordering.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 Fixed in 388e627. The session now exposes
liveSubscriptionOwner, which its dispose replaces when it drops the live subscriptions. The manager treats a subscription held for an earlier owner as gone, so the connect after the auth rebuild reopens it, and a subscribe that was in flight across the rebuild opens again on the new build. The provider journey now covers both orderings: initialized before auth with the rebuild after, and the rebuild landing during the subscribe.