Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(mobile): serialize read-state snapshot reads in one sync loop
Initialize, reconnects and every live-subscription ready now request a
snapshot through one loop. A request before a pass starts its read joins
that read; a request during the read runs one more pass, so a queued
cold-start pass can no longer swallow a later reconnect. Snapshot reads
are numbered in start order and only the newest settled read decides
whether the last snapshot succeeded, so an older read finishing late
cannot clear a newer failure's retry.

Drops the live generation and ready-transition checks, accepting an
occasional extra snapshot read.

Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Tom Brow <[email protected]>
  • Loading branch information
3 people committed Nov 10, 2026
commit 824494224aab7b849e85b40f5611530606a41878
184 changes: 78 additions & 106 deletions mobile/lib/shared/read_state/read_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ class ReadStateManager {
Future<void> _saved = Future.value();
bool _saveScheduled = false;
Object? syncError;
// Whether the latest snapshot read succeeded. A publish that skips its own
// Whether the newest snapshot read succeeded. A publish that skips its own
// snapshot clears [syncError] only on this evidence.
bool _lastSnapshotSucceeded = false;
int _liveGeneration = 0;
// Snapshot reads numbered in start order. Only a read newer than every
// settled one decides [_lastSnapshotSucceeded], so an older read finishing
// late cannot clear the retry a newer failure needs.
int _snapshotsStarted = 0;
int _newestSettledSnapshot = 0;
final int Function() _now;
// Override keys already in this device's own slot, carried unchanged.
// See [isOverrideContext].
Expand All @@ -160,20 +164,13 @@ class ReadStateManager {
void Function()? _unsubscribeLive;
bool _initialized = false;
bool _initialSeedComplete = false;
// Pending first fetch and subscribe. A reconnect while it runs waits for it
// so two passes cannot each install a live subscription and leak one.
Future<void>? _initializing;
// The reconnect pass running now, and the one waiting to start after it.
Future<void>? _runningResync;
Future<void>? _queuedResync;
// Whether the first fetch and subscribe both succeeded. A connect that
// lands while they run (the cold-start connect) then has nothing to redo.
bool _initialSyncSucceeded = false;

// The current live subscription has reported ready. A connect while it
// is not yet live is the connection it was waiting for; a connect after
// it went live is a reconnect.
bool _liveReady = 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.
Future<void>? _syncing;
bool _snapshotWanted = false;
bool _resubscribeWanted = false;

bool _disposed = false;
bool _isPublishing = false;
Expand Down Expand Up @@ -227,16 +224,7 @@ class ReadStateManager {
return;
}

final initializing = Completer<void>();
_initializing = initializing.future;
try {
final subscribed = await _startLiveSubscription();
final fetched = await _fetchAndMerge();
_initialSyncSucceeded = fetched && subscribed;
} finally {
_initializing = null;
initializing.complete();
}
await _requestSync(resubscribe: true);
if (_localContexts.isNotEmpty ||
_pending.isNotEmpty ||
_carriedOverrides.isNotEmpty) {
Expand Down Expand Up @@ -296,56 +284,54 @@ class ReadStateManager {
await _publish();
}

/// Fetch and resubscribe after a reconnect. Passes run one at a time, so
/// each replaces the previous live subscription instead of leaking one.
/// Reconnects while a pass waits to start share that pass.
Future<void> reinitializeRemote() {
if (_disposed || !_remoteEnabled || !_initialized) return Future.value();
final queued = _queuedResync;
if (queued != null) return queued;
final running = _runningResync;
final initializing = _initializing;
final reconnected = _liveReady;
final pass = Completer<void>();
_queuedResync = pass.future;
unawaited(() async {
try {
if (running != null) {
await running;
} else if (initializing != null) {
await initializing;
// Connected while the first sync waited for its connection: it
// already used this one.
if (_initialSyncSucceeded && !reconnected) return;
}
if (_disposed) return;
if (identical(_queuedResync, pass.future)) _queuedResync = null;
_runningResync = pass.future;
await _resyncRemote();
} catch (e) {
debugPrint('[ReadStateManager] reinitializeRemote failed: $e');
} finally {
if (identical(_queuedResync, pass.future)) _queuedResync = null;
if (identical(_runningResync, pass.future)) _runningResync = null;
pass.complete();
}
}());
return pass.future;
}

Future<void> _resyncRemote() async {
/// Resubscribe and read the snapshot after a reconnect.
Future<void> reinitializeRemote() async {
if (_disposed || !_remoteEnabled || !_initialized) return;
debugPrint('[ReadStateManager] reinitializeRemote');
if (_isPublishing) {
await _publishCompleter?.future;
}
_unsubscribeLive?.call();
_unsubscribeLive = null;
await _startLiveSubscription();
await _fetchAndMerge();
await _requestSync(resubscribe: true);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Blocker: every clean reconnect reads the full snapshot twice.

The provider's connected listener runs synchronously inside state = in _handleConnected, before _replayLiveSubscriptions. So this resubscribe sends REQ l-2 on the new socket, and the replay right after it sends l-2 again (marked ambiguous in _sendLiveReq). Both EOSEs emit ready. The first one releases subscribe and the loop starts snapshot A. The second lands while A downloads, sets _snapshotWanted, and the loop reads the whole snapshot again.

Repro: a real RelaySessionNotifier with a recording socket and a gated fake fetchReadStateSnapshot. Call reinitializeRemote(), then debugReplayLiveSubscriptions(), then EOSE each REQ, the second while the read is held.

Head REQs on the new socket Snapshot reads per reconnect
eefce94ba0 [l-2, l-2] 1
824494224a [l-2, l-2] 2

A separate reviewer probe got the same result through the provider listener. With only this resubscribe removed, it got one REQ ([l-1]) and one read.

That is a full read-state download (discovery plus POST) on every resume, on the accounts this PR is meant to help. The PR text's "Each resume reads the read-state snapshot once over HTTP" no longer holds.

Suggested fix (simplest): let the session own resubscription. It already keeps live subscriptions across reconnects and replays them, and a subscription opened before AUTH is held and sent on connect. Pass onClosed to mark the subscription lost, and resubscribe here only when it is lost (terminal CLOSED, or a failed subscribe). Otherwise the replay's ready already asks the loop for the read. That also removes _resubscribeWanted, the wait for the publish before resubscribing, and the cold start's extra pass, and it stops a terminal CLOSED from dropping the subscription silently. The alternative is to restore "a repeated ready while already live reads nothing", reset on retrying and on each new subscribe. That works, but it keeps the duplicate REQ.

Please cover it with a test through the real session replay. The _FakeRelaySession path can't show the duplicate EOSE.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

🤖 Fixed in c620e9d4b5, with your suggested fix. A reconnect no longer resubscribes: the session's replay of the live subscription is the one REQ on the new socket, and its ready asks the sync loop for the one snapshot read. The manager opens a new subscription only after losing one (onClosed on a terminal CLOSED, or a failed subscribe), on the next reconnect. That also removed the resubscribe flag and the wait for an in-flight publish; the sync loop now just opens the subscription when it is missing. No generation or ready-while-live check came back.

New journey test a clean reconnect through the real session replays one REQ and reads the snapshot once, and a lost subscription is reopened drives the real RelaySessionNotifier with a recording socket: reinitializeRemote(), then debugReplayLiveSubscriptions(), then EOSE while the read is held. It fails against 824494224a ([l-2, l-2], two reads) and passes now ([l-1], one read). It also checks that after a terminal CLOSED the next reconnect opens l-2 and reads the snapshot. In that rare lost case the replay can still send the new REQ a second time.

The cold start keeps its occasional extra read when ready lands after the 500 ms fallback.

_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}) {
_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
// inside subscribe, and that request must join this loop.
final done = Completer<void>();
_syncing = done.future;
unawaited(_runSync(done));
return done.future;
}

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;
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.
if (!await _fetchAndMerge()) _schedulePublish();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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, _publishOnce's catch records syncError and schedules nothing. A mark another device published while the subscription was down then stays unread here until the next local mark, pause flush, resume or reconnect. That fits the PR's "Accepted gap" (it heals on its own, and syncError reaches no UI), so I'm not asking for backoff. But "A failed read is retried by the next publish" reads as a guarantee. Please say instead that it waits for the next trigger.

Related nit: the publish read at _publishOnce increments _snapshotsStarted but never settles a failure, so the invariant "only the newest settled read decides" has one unrecorded path. I can't build a user-visible failure from it, because a failed publish leaves unpublished state that forces the next full publish anyway. Settling the failure there would keep the accounting honest.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The 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 _publishOnce settles a failed snapshot read too.

}
} catch (e) {
debugPrint('[ReadStateManager] sync failed: $e');
} finally {
_syncing = null;
done.complete();
}
}

void dispose({bool flushPending = true}) {
if (_disposed) return;
_disposed = true;
Expand Down Expand Up @@ -382,23 +368,32 @@ class ReadStateManager {
}

Future<bool> _fetchAndMerge() async {
final read = ++_snapshotsStarted;
try {
// Snapshot transport validates completeness and fences retired sessions.
final events = await _relaySession!.fetchReadStateSnapshot();
if (_disposed || !_servesThisCommunity) return false;
_lastSnapshotSucceeded = true;
_settleSnapshot(read, succeeded: true);
_mergeEvents(events);
await _persistLocalState();
_onChanged();
return true;
} catch (error) {
_lastSnapshotSucceeded = false;
syncError = error;
if (_settleSnapshot(read, succeeded: false)) syncError = error;
debugPrint('[ReadStateManager] snapshot failed: $error');
return false;
}
}

/// Records snapshot read [read]'s outcome unless a newer read already
/// settled. Returns whether it was recorded.
bool _settleSnapshot(int read, {required bool succeeded}) {
if (read < _newestSettledSnapshot) return false;
_newestSettledSnapshot = read;
_lastSnapshotSucceeded = succeeded;
return true;
}

void _mergeEvents(List<NostrEvent> events) {
if (!_servesThisCommunity) return;
for (final event in events) {
Expand Down Expand Up @@ -468,9 +463,6 @@ class ReadStateManager {

Future<bool> _startLiveSubscription() async {
try {
final generation = ++_liveGeneration;
var returned = false;
_liveReady = false;
final unsub = await _relaySession!.subscribeWithStatus(
NostrFilter(
kinds: const [EventKind.readState],
Expand All @@ -483,25 +475,17 @@ class ReadStateManager {
limit: 0,
Comment thread
brow marked this conversation as resolved.
Resolved

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Wait for live readiness before taking the snapshot

When initialization starts before WebSocket authentication completes, or EOSE takes over 500 ms, RelaySessionNotifier.subscribe returns via its timeout while the REQ may still be held (relay_session.dart lines 382–390). The subsequent HTTP snapshot can therefore finish before the live subscription is installed, and a connection arriving during initialization may skip another resync because _initialSyncSucceeded was set. Any read-state event published in that gap is absent from both the snapshot and this limit: 0 replay, leaving stale unread state until a later resync; require confirmed live readiness or run another snapshot after delayed readiness.

AGENTS.md reference: AGENTS.md:L235-L238

Useful? React with 👍 / 👎.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Backfill after retryable subscription closures

When the relay sends a retryable CLOSED while the socket remains connected—for example a deadline or rate-limit closure—RelaySessionNotifier retries the same filter after backoff (relay_session.dart lines 901–921), while the read-state provider only requests a snapshot on a transition to SessionStatus.connected. With limit: 0, read-state events published during that backoff are not replayed by the retry and are silently missed; trigger a fresh snapshot when this subscription recovers or retain a bounded replay for this recovery path.

AGENTS.md reference: AGENTS.md:L235-L238

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 Blocker: read marks published while this subscription is CLOSED-and-retrying are never received.

RelaySession retries a retryable CLOSED (rate limit, deadline, capacity) on the same socket after backoff (relay_session.dart _handleClosed). Its own comment says the design relies on the retry replaying stored events: "since is not advanced, so events missed while closed aren't skipped." With limit: 0 the retry replays nothing. The manager uses plain subscribe, so it never learns that the stream was interrupted. No snapshot runs until the next session transition to connected.

Scenario: the relay closes this REQ with a rate-limit CLOSED, and the phone's socket stays up. During the backoff, the desktop marks a channel read. The retry registers with LIMIT 0, so that mark never arrives. The phone shows the channel as unread until the next background/resume or reconnect. A closely related window: if subscribe() completes on its 500 ms fallback rather than EOSE, the HTTP snapshot can be read before the relay has registered the REQ.

Fix: follow the existing pattern for limit: 0 subscribers (channel_list_changes.dart _subscribeChannelListNotices). Use subscribeWithStatus, and on retrying → ready, run _fetchAndMerge() (serialized with reinitializeRemote). Please add one journey test: a CLOSED retry with a peer write during the backoff, then the merged state after the retry is ready.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🤖 [P2] Keep replay-derived timestamp bounds off read-state notifications

The retained subscription keeps lastSeenCreatedAt, so _replayFilter adds since = lastSeenCreatedAt - 5 even with limit: 0; the relay applies it to future live delivery too. If this device observes slot X at T and reconnects, a peer's different slot Y signed at T-6 can arrive at T+1, after the reconnect snapshot, and be excluded (a seven-second transit is inside the publish timeout). The successful snapshot leaves no retry debt, so the peer mark stays stale until another independent sync; the former fresh REQ had no since. Preserve limit: 0 without replay-derived timestamp bounds, and extend the real-session journey with an older-timestamp peer event accepted after the snapshot.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

🤖 Fixed in 388e627. subscribeWithStatus takes resumeSince (default true), and the read-state subscription passes false, so its replayed REQ stays limit: 0 with no since. Other live subscriptions keep their resume cursor. The real-session journey now delivers a live mark, reconnects, checks the replayed filter has no since, then accepts a peer mark stamped 600 s earlier after the snapshot.

),
_handleIncomingEvent,
// The snapshot after this call covers marks published before the REQ
// went live, unless it goes live later: after the 500 ms fallback
// (slow AUTH on a cold start) or after a transient CLOSED's retry.
// With limit 0 nothing replays the gap, so read the snapshot then.
// A repeated ready while already live (a reconnect can send the
// same REQ twice) leaves no gap, so it reads nothing.
// 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.
onStatusChanged: (status) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 initialize() runs before WS AUTH completes, _subscribe holds the REQ and returns on its 500 ms fallback (relay_session.dart _subscribe). The HTTP snapshot does not wait for the socket (relay_session_snapshot.dart), so it can read before the REQ registers. initialize() then sets _initialSyncSucceeded, so the connect that lands mid-initialize skips its resync (reinitializeRemote). When the held REQ's EOSE finally arrives, this callback ignores the ready because nothing set interrupted. On main, the 500-event stored replay covered this window. With limit: 0, nothing covers it.

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 _resyncRemote opens if EOSE is slow, but that window is much smaller.

Fix: also catch up when the first ready arrives after subscribeWithStatus has returned. That is the case where the call fell back to its timeout instead of reaching EOSE. Normal starts get their EOSE inside the await, so they keep a single snapshot. For example, set a returned flag after the await and track seenReady in the callback, then call _catchUpAfterRetry() when interrupted || (returned && !seenReady). Please add one journey test where the fake withholds ready until after initialize() finishes, a peer mark lands, and the late ready merges it. The current ordering test's fake returns immediately, so it can't show relay registration.

if (generation != _liveGeneration) return;
if (status == RelaySubscriptionStatus.ready) {
if (!_liveReady && returned) {
unawaited(_catchUpAfterLateReady(generation));
}
_liveReady = true;
} else {
_liveReady = false;
if (status == RelaySubscriptionStatus.ready && !_disposed) {
unawaited(_requestSync());
}
},
);
returned = true;
if (_disposed) {
unsub.call();
return false;
Expand All @@ -515,19 +499,6 @@ class ReadStateManager {
}
}

/// Reads the snapshot after the live subscription went live late, once
/// any pass already running is done. Skipped when a reconnect pass has
/// replaced the subscription, since that pass read the snapshot after it.
Future<void> _catchUpAfterLateReady(int generation) async {
final pass = _queuedResync ?? _runningResync ?? _initializing;
if (pass != null) await pass;
if (_disposed || !_servesThisCommunity) return;
if (generation != _liveGeneration) return;
// A failed read is retried by the next publish, which reads the
// snapshot in full after a failure.
if (!await _fetchAndMerge()) _schedulePublish();
}

void _handleIncomingEvent(NostrEvent event) {
if (_disposed || !_servesThisCommunity) return;
_mergeEvents([event]);
Expand Down Expand Up @@ -648,8 +619,9 @@ class ReadStateManager {
final checkConnection = _relaySession?.captureConnectionFence();
var snapshot = <NostrEvent>[];
if (_relaySession != null) {
final read = ++_snapshotsStarted;
final events = await _relaySession.fetchReadStateSnapshot();
_lastSnapshotSucceeded = true;
_settleSnapshot(read, succeeded: true);
if (!_servesThisCommunity ||
!_ownsJournal ||
(_disposed && !allowDisposed)) {
Expand Down
Loading
Loading