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): let the session's replay drive the read-state reconnect …
…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
brow and studio committed Nov 13, 2026
commit c620e9d4b52c54e5a55c6271141f0bdcc2923b35
51 changes: 28 additions & 23 deletions mobile/lib/shared/read_state/read_state_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -224,7 +223,7 @@ class ReadStateManager {
return;
}

await _requestSync(resubscribe: true);
await _requestSync();
if (_localContexts.isNotEmpty ||
_pending.isNotEmpty ||
_carriedOverrides.isNotEmpty) {
Expand Down Expand Up @@ -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();

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] 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 _liveSubscriptions without onClosed; this manager survives because it watches the notifier identity, not auth. It retains the stale cleanup callback (including when subscribe returns after its 500 ms fallback), so this guard skips reopening on every reconnect and no REQ remains to emit ready: 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.

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. 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.

_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
Expand All @@ -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();

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) {
Expand Down Expand Up @@ -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) {

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 (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();
Expand Down Expand Up @@ -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 ||
Expand Down
210 changes: 114 additions & 96 deletions mobile/test/features/channels/read_state/read_state_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -903,84 +903,7 @@ void main() {
second.dispose(flushPending: false);
});

test('a reconnect during the first snapshot syncs again', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final keychain = nostr.Keys.generate();
final crypto = ReadStateCrypto.tryCreate(
nsec: keychain.nsec,
pubkey: keychain.public,
)!;
// The live subscription goes live at once, then the socket drops and
// reconnects while the first snapshot is still downloading.
final gate = Completer<void>();
final relay = _FakeRelaySession()
..readyOnSubscribe = true
..fetchGate = gate.future;
final manager = ReadStateManager(
pubkey: keychain.public,
relayUrl: _relayUrl,
prefs: prefs,
crypto: crypto,
relaySession: relay,
signedEventRelay: _FakeSignedEventRelay(),
remoteEnabled: true,
onChanged: () {},
);

final initializing = manager.initialize();
await pumpEventQueue();
final reconnecting = manager.reinitializeRemote();
relay.fetchGate = null;
gate.complete();
await Future.wait([initializing, reconnecting]);

// The snapshot may predate marks published while the socket was down,
// and limit 0 replays none, so the reconnect reads it again.
expect(relay.fetchCount, 2);
expect(relay.liveSubscriptions, 1);
manager.dispose();
});

test('a connect during a failed initialize syncs again', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final keychain = nostr.Keys.generate();
final crypto = ReadStateCrypto.tryCreate(
nsec: keychain.nsec,
pubkey: keychain.public,
)!;
final gate = Completer<void>();
final relay = _FakeRelaySession()
..fetchGate = gate.future
..failNextFetch = true;
final manager = ReadStateManager(
pubkey: keychain.public,
relayUrl: _relayUrl,
prefs: prefs,
crypto: crypto,
relaySession: relay,
signedEventRelay: _FakeSignedEventRelay(),
remoteEnabled: true,
onChanged: () {},
);

final initializing = manager.initialize();
final reinitializing = manager.reinitializeRemote();
relay.fetchGate = null;
gate.complete();
await Future.wait([initializing, reinitializing]);

// Both passes subscribed, but the later one replaced the earlier one.
expect(relay.fetchCount, 2);
expect(relay.subscribeCount, 2);
expect(relay.liveSubscriptions, 1);

manager.dispose();
expect(relay.liveSubscriptions, 0);
});

test('reconnects during a re-sync leave one live subscription', () async {
test('readies during a held snapshot read share one more read', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final keychain = nostr.Keys.generate();
Expand All @@ -1001,31 +924,29 @@ void main() {
);
await manager.initialize();

// The first re-sync holds its fetch; three more connects arrive.
// A ready starts a read and holds it; three more readies arrive.
final gate = Completer<void>();
relay.fetchGate = gate.future;
final first = manager.reinitializeRemote();
relay.statusListeners.last(RelaySubscriptionStatus.ready);
await Future<void>.delayed(Duration.zero);
final later = [
manager.reinitializeRemote(),
manager.reinitializeRemote(),
manager.reinitializeRemote(),
];
for (var i = 0; i < 3; i++) {
relay.statusListeners.last(RelaySubscriptionStatus.ready);
}
relay.fetchGate = null;
gate.complete();
await Future.wait([first, ...later]);
await pumpEventQueue();

// One initial pass, the held re-sync, and one shared re-sync after it.
// The initial read, the held read, and one shared read after it.
expect(relay.fetchCount, 3);
expect(relay.subscribeCount, 3);
expect(relay.subscribeCount, 1);
expect(relay.liveSubscriptions, 1);

manager.dispose();
expect(relay.liveSubscriptions, 0);
});

test(
'live subscription replays nothing and opens before each snapshot',
'live subscription replays nothing and opens before the snapshot',
() async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
Expand All @@ -1047,17 +968,13 @@ void main() {
);

await manager.initialize();
await manager.reinitializeRemote();

// The snapshot is the only read of stored blobs. A stored replay on the
// live subscription re-downloaded every slot (megabytes) on each resume.
expect(relay.subscribeFilters, hasLength(2));
for (final filter in relay.subscribeFilters) {
expect(filter.limit, 0);
}
expect(relay.subscribeFilters.single.limit, 0);
// Opened before its snapshot, so the snapshot covers the gap.
expect(relay.fetchCountAtSubscribe, [0, 1]);
expect(relay.fetchCount, 2);
expect(relay.fetchCountAtSubscribe, [0]);
expect(relay.fetchCount, 1);

manager.dispose();
},
Expand Down Expand Up @@ -1269,6 +1186,73 @@ void main() {
manager.dispose(flushPending: false);
});

test('a clean reconnect through the real session replays one REQ and reads '
'the snapshot once, and a lost subscription is reopened', () async {
SharedPreferences.setMockInitialValues({});
final prefs = await SharedPreferences.getInstance();
final keychain = nostr.Keys.generate();
final crypto = ReadStateCrypto.tryCreate(
nsec: keychain.nsec,
pubkey: keychain.public,
)!;
final socket = _RecordingRelaySocket();
final session = _SnapshotCountingSession()
..debugAttachSocketForTest(socket);
List<String> reqIds() => [
for (final message in socket.messages)
if (message.first == 'REQ') message[1] as String,
];
final manager = ReadStateManager(
pubkey: keychain.public,
relayUrl: _relayUrl,
prefs: prefs,
crypto: crypto,
relaySession: session,
signedEventRelay: _FakeSignedEventRelay(),
remoteEnabled: true,
onChanged: () {},
);

final initializing = manager.initialize();
await pumpEventQueue();
expect(reqIds(), ['l-1']);
session.debugHandleMessage(['EOSE', 'l-1']);
await initializing;
expect(session.fetchCount, 1);

// A reconnect: the provider's connected listener runs before the session
// replays its live subscriptions on the new socket.
socket.messages.clear();
session.debugSupersedeConnection();
final held = Completer<void>();
session.fetchGate = held.future;
final reconnecting = manager.reinitializeRemote();
await pumpEventQueue();
await session.debugReplayLiveSubscriptions();
expect(reqIds(), ['l-1']);
session.debugHandleMessage(['EOSE', 'l-1']);
await pumpEventQueue();
expect(session.fetchCount, 2);
session.fetchGate = null;
held.complete();
await reconnecting;
await pumpEventQueue();
expect(session.fetchCount, 2);

// A terminal CLOSED drops the subscription; the next reconnect opens a
// new one and reads the snapshot.
session.debugHandleMessage(['CLOSED', 'l-1', 'restricted: revoked']);
socket.messages.clear();
session.debugSupersedeConnection();
final reopening = manager.reinitializeRemote();
await pumpEventQueue();
expect(reqIds(), ['l-2']);
session.debugHandleMessage(['EOSE', 'l-2']);
await reopening;
expect(session.fetchCount, 3);
manager.dispose(flushPending: false);
});

test(
'an older snapshot finishing late keeps a newer failed read\'s retry',
() async {
Expand Down Expand Up @@ -1401,6 +1385,40 @@ NostrEvent _stubAckEvent() => const NostrEvent(
sig: '',
);

/// The real session, with the snapshot's HTTP read replaced by a counter.
class _SnapshotCountingSession extends RelaySessionNotifier {
int fetchCount = 0;
Future<void>? fetchGate;

@override
Future<List<NostrEvent>> fetchReadStateSnapshot({
Duration timeout = const Duration(seconds: 8),
}) async {
fetchCount++;
await fetchGate;
return [];
}
}

class _RecordingRelaySocket extends RelaySocket {
_RecordingRelaySocket()
: super(
wsUrl: 'wss://relay.example',
nsec: null,
onMessage: (_) {},
onConnected: () {},
onDisconnected: (_) {},
);

final List<List<dynamic>> messages = [];

@override
void send(List<dynamic> payload) => messages.add(payload);

@override
void dispose() {}
}

class _FakeSignedEventRelay implements SignedEventRelay {
final Completer<_SubmittedEvent> submitted = Completer<_SubmittedEvent>();
final List<String> contents = [];
Expand Down
Loading
Loading