diff --git a/lib/powersync/sync_watchdog.dart b/lib/powersync/sync_watchdog.dart index d4d2c4907..ef7a5888a 100644 --- a/lib/powersync/sync_watchdog.dart +++ b/lib/powersync/sync_watchdog.dart @@ -120,7 +120,9 @@ class SyncStreamWatchdog { } _offline = value; if (value) { - _cancelTimer(); + // A running timer deliberately survives: the probe misfires while a + // large download saturates the line, and cancelling here would let + // those misfires restart the detection over and over. stalled.value = false; } } @@ -143,6 +145,11 @@ class SyncStreamWatchdog { void _onTimeout() { _timer = null; + // Nothing to report while plain requests fail as well; the next status + // event re-arms once the backend answers again. + if (_offline) { + return; + } stalled.value = true; if (_lastError case final error?) { diff --git a/test/powersync/sync_watchdog_test.dart b/test/powersync/sync_watchdog_test.dart index 9be4987f3..eabab8871 100644 --- a/test/powersync/sync_watchdog_test.dart +++ b/test/powersync/sync_watchdog_test.dart @@ -191,7 +191,25 @@ void main() { }); }); - test('going offline cancels a pending timer and clears the flag', () { + test('a probe misfiring under load does not restart the detection', () { + fakeAsync((async) { + // The reachability probe times out while a large download saturates + // the line and recovers right after. If that restarted the clock, the + // watchdog would never reach its timeout on exactly the connection it + // is meant to flag. + watchdog.onStatus(buildSyncStatus(connecting: true)); + async.elapse(watchdog.timeout - const Duration(seconds: 10)); + + watchdog.offline = true; + watchdog.offline = false; + watchdog.onStatus(buildSyncStatus(connecting: true)); + + async.elapse(const Duration(seconds: 11)); + expect(watchdog.stalled.value, isTrue); + }); + }); + + test('going offline clears the flag and reports nothing further', () { fakeAsync((async) { watchdog.onStatus(buildSyncStatus(connecting: true)); async.elapse(watchdog.timeout);