mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 01:41:05 +01:00
Let the sync watchdog fire on a permanent error loop
This commit is contained in:
@@ -34,12 +34,16 @@ final _logger = Logger('powersync');
|
||||
/// `SyncStatus.anyError` stays null. The UI shows "Connecting" forever
|
||||
/// without a trace.
|
||||
///
|
||||
/// It also covers the noisier variant of the same problem: a connection that
|
||||
/// fails in a loop every few seconds. The error is in the status dialog, but
|
||||
/// nothing tells the user that no data has arrived for minutes.
|
||||
///
|
||||
/// The watchdog arms a timer while the client wants to sync but has not
|
||||
/// received a checkpoint in this connection epoch. Checkpoint progress
|
||||
/// disarms it until [reset]; an active download or a surfaced error pauses
|
||||
/// it (those cases are visible through other channels). If the timer
|
||||
/// survives [timeout], [stalled] flips to true (the sync status dialog
|
||||
/// shows a hint) and a WARNING lands in the app logs.
|
||||
/// disarms it until [reset]; only an active download pauses it, as that is
|
||||
/// data actually flowing. If the timer survives [timeout], [stalled] flips
|
||||
/// to true (the sync status dialog shows a hint) and a WARNING lands in the
|
||||
/// app logs.
|
||||
class SyncStreamWatchdog {
|
||||
SyncStreamWatchdog({this.timeout = const Duration(minutes: 2)});
|
||||
|
||||
@@ -52,6 +56,11 @@ class SyncStreamWatchdog {
|
||||
DateTime? _lastCheckpoint;
|
||||
bool _sawStatus = false;
|
||||
|
||||
/// Last error seen since the watchdog was armed, for the warning. Kept
|
||||
/// across status events: a retry loop clears the error while it reconnects
|
||||
/// and sets it again on the next failure.
|
||||
Object? _lastError;
|
||||
|
||||
/// Set once a checkpoint arrived; the watchdog then stays quiet so a later
|
||||
/// idle connection (status events stop while nothing changes) cannot
|
||||
/// re-trigger it. [reset] re-arms for the next connection epoch.
|
||||
@@ -68,6 +77,7 @@ class SyncStreamWatchdog {
|
||||
|
||||
if (madeProgress) {
|
||||
_disarmed = true;
|
||||
_lastError = null;
|
||||
_cancelTimer();
|
||||
if (stalled.value) {
|
||||
_logger.info('Sync stream recovered, checkpoint received');
|
||||
@@ -76,20 +86,23 @@ class SyncStreamWatchdog {
|
||||
return;
|
||||
}
|
||||
|
||||
// Downloading means data is flowing (a first checkpoint on a large
|
||||
// account can take a while); an error is already surfaced by the sync
|
||||
// status dialog. Neither is the silent failure we are watching for.
|
||||
if (status.downloading || status.anyError != null) {
|
||||
// Downloading means data is flowing, a first checkpoint on a large
|
||||
// account can take a while.
|
||||
if (status.downloading) {
|
||||
_cancelTimer();
|
||||
return;
|
||||
}
|
||||
|
||||
_lastError = status.anyError ?? _lastError;
|
||||
|
||||
// A `connecting` flag briefly dropping between two retry iterations must
|
||||
// NOT clear the timer: the silent connect/EOF loop would reset it on
|
||||
// every cycle. Only [reset] (deliberate disconnect) clears it. While
|
||||
// already stalled there is nothing new to detect (and no reason to log
|
||||
// the warning again), so the timer stays off until recovery.
|
||||
if (!_disarmed && !stalled.value && (status.connecting || status.connected)) {
|
||||
if (!_disarmed &&
|
||||
!stalled.value &&
|
||||
(status.connecting || status.connected || _lastError != null)) {
|
||||
_timer ??= Timer(timeout, _onTimeout);
|
||||
}
|
||||
}
|
||||
@@ -99,6 +112,7 @@ class SyncStreamWatchdog {
|
||||
void reset() {
|
||||
_cancelTimer();
|
||||
_disarmed = false;
|
||||
_lastError = null;
|
||||
stalled.value = false;
|
||||
}
|
||||
|
||||
@@ -110,6 +124,16 @@ class SyncStreamWatchdog {
|
||||
void _onTimeout() {
|
||||
_timer = null;
|
||||
stalled.value = true;
|
||||
|
||||
if (_lastError case final error?) {
|
||||
_logger.warning(
|
||||
'Sync has been failing for ${timeout.inMinutes} minutes without '
|
||||
'receiving any data.',
|
||||
error,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
_logger.warning(
|
||||
'Sync stream keeps terminating without receiving any data. The '
|
||||
'connection to the sync service may be blocked by a VPN, ad blocker, '
|
||||
|
||||
@@ -91,9 +91,29 @@ void main() {
|
||||
});
|
||||
});
|
||||
|
||||
test('a surfaced sync error holds the watchdog off', () {
|
||||
test('flags a connection that only produces errors', () {
|
||||
fakeAsync((async) {
|
||||
watchdog.onStatus(buildSyncStatus(connecting: true, downloadError: Exception('boom')));
|
||||
// Error loop in the five second rhythm of the SDK's retry: visible in
|
||||
// the sync dialog, but no checkpoint ever arrives.
|
||||
for (var i = 0; i < 30; i++) {
|
||||
watchdog.onStatus(buildSyncStatus(connecting: true));
|
||||
async.elapse(const Duration(seconds: 2));
|
||||
watchdog.onStatus(buildSyncStatus(downloadError: Exception('boom')));
|
||||
async.elapse(const Duration(seconds: 3));
|
||||
}
|
||||
|
||||
expect(watchdog.stalled.value, isTrue);
|
||||
final warnings = logLines(Level.WARNING, 'has been failing');
|
||||
expect(warnings, hasLength(1));
|
||||
expect(warnings.first.error.toString(), contains('boom'));
|
||||
});
|
||||
});
|
||||
|
||||
test('an error while data is downloading does not flag anything', () {
|
||||
fakeAsync((async) {
|
||||
watchdog.onStatus(
|
||||
buildSyncStatus(connected: true, downloading: true, uploadError: Exception('boom')),
|
||||
);
|
||||
async.elapse(watchdog.timeout * 2);
|
||||
|
||||
expect(watchdog.stalled.value, isFalse);
|
||||
|
||||
Reference in New Issue
Block a user