Skip to content

Commit 23a7d99

Browse files
square-tombstudio
andcommitted
fix(mobile): clear a recovered sync error when the publish has nothing to send
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz> Signed-off-by: Tom Brow <[email protected]>
1 parent 04f9ea8 commit 23a7d99

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ class ReadStateManager {
147147
Future<void> _saved = Future.value();
148148
bool _saveScheduled = false;
149149
Object? syncError;
150+
// Whether the latest snapshot read succeeded. A publish that skips its own
151+
// snapshot clears [syncError] only on this evidence.
152+
bool _lastSnapshotSucceeded = false;
150153
final int Function() _now;
151154
// Override keys already in this device's own slot, carried unchanged.
152155
// See [isOverrideContext].
@@ -376,11 +379,13 @@ class ReadStateManager {
376379
// Snapshot transport validates completeness and fences retired sessions.
377380
final events = await _relaySession!.fetchReadStateSnapshot();
378381
if (_disposed || !_servesThisCommunity) return false;
382+
_lastSnapshotSucceeded = true;
379383
_mergeEvents(events);
380384
await _persistLocalState();
381385
_onChanged();
382386
return true;
383387
} catch (error) {
388+
_lastSnapshotSucceeded = false;
384389
syncError = error;
385390
debugPrint('[ReadStateManager] snapshot failed: $error');
386391
return false;
@@ -589,11 +594,17 @@ class ReadStateManager {
589594
// The snapshot below is the whole read state (megabytes on a busy
590595
// account), so skip it when there is nothing to send. Each resume
591596
// schedules a publish after its re-sync whether or not anything changed.
592-
if (!_hasUnpublishedState()) return;
597+
if (!_hasUnpublishedState()) {
598+
// Nothing pending and every slot published, so after a good snapshot
599+
// this device is in sync, as a full publish would have concluded.
600+
if (_lastSnapshotSucceeded) syncError = null;
601+
return;
602+
}
593603
final checkConnection = _relaySession?.captureConnectionFence();
594604
var snapshot = <NostrEvent>[];
595605
if (_relaySession != null) {
596606
final events = await _relaySession.fetchReadStateSnapshot();
607+
_lastSnapshotSucceeded = true;
597608
if (!_servesThisCommunity ||
598609
!_ownsJournal ||
599610
(_disposed && !allowDisposed)) {

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,43 @@ void main() {
409409
manager.dispose(flushPending: false);
410410
});
411411

412+
test(
413+
'a skipped publish clears an error a later snapshot recovered',
414+
() async {
415+
SharedPreferences.setMockInitialValues({});
416+
final prefs = await SharedPreferences.getInstance();
417+
final keychain = nostr.Keys.generate();
418+
final crypto = ReadStateCrypto.tryCreate(
419+
nsec: keychain.nsec,
420+
pubkey: keychain.public,
421+
)!;
422+
final relay = _FakeRelaySession()..failNextFetch = true;
423+
final manager = ReadStateManager(
424+
pubkey: keychain.public,
425+
relayUrl: _relayUrl,
426+
prefs: prefs,
427+
crypto: crypto,
428+
relaySession: relay,
429+
signedEventRelay: _FakeSignedEventRelay(),
430+
remoteEnabled: true,
431+
onChanged: () {},
432+
);
433+
434+
await manager.initialize();
435+
expect(manager.syncError, isNotNull);
436+
// A publish with nothing to send keeps the error without new evidence.
437+
await manager.flush();
438+
expect(manager.syncError, isNotNull);
439+
440+
await manager.reinitializeRemote();
441+
await manager.flush();
442+
expect(manager.syncError, isNull);
443+
expect(relay.fetchCount, 2);
444+
445+
manager.dispose(flushPending: false);
446+
},
447+
);
448+
412449
test(
413450
'retains recent frontiers without a count eviction across restart',
414451
() async {

0 commit comments

Comments
 (0)