Skip to content

Commit 4eff9e4

Browse files
square-tombstudioclaude
committed
fix(mobile): re-sync read state on a reconnect during the first snapshot
A connect after the live subscription went ready is a reconnect, so it reads the snapshot again even while initialize is still running. 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]>
1 parent 29be4ea commit 4eff9e4

2 files changed

Lines changed: 53 additions & 7 deletions

File tree

‎mobile/lib/shared/read_state/read_state_manager.dart‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ class ReadStateManager {
170170
// lands while they run (the cold-start connect) then has nothing to redo.
171171
bool _initialSyncSucceeded = false;
172172

173+
// The current live subscription has reported ready. A connect while it
174+
// is not yet live is the connection it was waiting for; a connect after
175+
// it went live is a reconnect.
176+
bool _liveReady = false;
177+
173178
bool _disposed = false;
174179
bool _isPublishing = false;
175180
// Set when a publish is asked for while one is running. The running
@@ -300,6 +305,7 @@ class ReadStateManager {
300305
if (queued != null) return queued;
301306
final running = _runningResync;
302307
final initializing = _initializing;
308+
final reconnected = _liveReady;
303309
final pass = Completer<void>();
304310
_queuedResync = pass.future;
305311
unawaited(() async {
@@ -308,9 +314,9 @@ class ReadStateManager {
308314
await running;
309315
} else if (initializing != null) {
310316
await initializing;
311-
// Connected while the first sync ran: it already used this
312-
// connection.
313-
if (_initialSyncSucceeded) return;
317+
// Connected while the first sync waited for its connection: it
318+
// already used this one.
319+
if (_initialSyncSucceeded && !reconnected) return;
314320
}
315321
if (_disposed) return;
316322
if (identical(_queuedResync, pass.future)) _queuedResync = null;
@@ -464,7 +470,7 @@ class ReadStateManager {
464470
try {
465471
final generation = ++_liveGeneration;
466472
var returned = false;
467-
var live = false;
473+
_liveReady = false;
468474
final unsub = await _relaySession!.subscribeWithStatus(
469475
NostrFilter(
470476
kinds: const [EventKind.readState],
@@ -484,13 +490,14 @@ class ReadStateManager {
484490
// A repeated ready while already live (a reconnect can send the
485491
// same REQ twice) leaves no gap, so it reads nothing.
486492
onStatusChanged: (status) {
493+
if (generation != _liveGeneration) return;
487494
if (status == RelaySubscriptionStatus.ready) {
488-
if (!live && returned) {
495+
if (!_liveReady && returned) {
489496
unawaited(_catchUpAfterLateReady(generation));
490497
}
491-
live = true;
498+
_liveReady = true;
492499
} else {
493-
live = false;
500+
_liveReady = false;
494501
}
495502
},
496503
);

‎mobile/test/features/channels/read_state/read_state_manager_test.dart‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,45 @@ void main() {
959959
},
960960
);
961961

962+
test('a reconnect during the first snapshot syncs again', () async {
963+
SharedPreferences.setMockInitialValues({});
964+
final prefs = await SharedPreferences.getInstance();
965+
final keychain = nostr.Keys.generate();
966+
final crypto = ReadStateCrypto.tryCreate(
967+
nsec: keychain.nsec,
968+
pubkey: keychain.public,
969+
)!;
970+
// The live subscription goes live at once, then the socket drops and
971+
// reconnects while the first snapshot is still downloading.
972+
final gate = Completer<void>();
973+
final relay = _FakeRelaySession()
974+
..readyOnSubscribe = true
975+
..fetchGate = gate.future;
976+
final manager = ReadStateManager(
977+
pubkey: keychain.public,
978+
relayUrl: _relayUrl,
979+
prefs: prefs,
980+
crypto: crypto,
981+
relaySession: relay,
982+
signedEventRelay: _FakeSignedEventRelay(),
983+
remoteEnabled: true,
984+
onChanged: () {},
985+
);
986+
987+
final initializing = manager.initialize();
988+
await pumpEventQueue();
989+
final reconnecting = manager.reinitializeRemote();
990+
relay.fetchGate = null;
991+
gate.complete();
992+
await Future.wait([initializing, reconnecting]);
993+
994+
// The snapshot may predate marks published while the socket was down,
995+
// and limit 0 replays none, so the reconnect reads it again.
996+
expect(relay.fetchCount, 2);
997+
expect(relay.liveSubscriptions, 1);
998+
manager.dispose();
999+
});
1000+
9621001
test('a connect during a failed initialize syncs again', () async {
9631002
SharedPreferences.setMockInitialValues({});
9641003
final prefs = await SharedPreferences.getInstance();

0 commit comments

Comments
 (0)