From b2ff0fb81cf9d1616130149ea601594dc73a6103 Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Sat, 22 Aug 2026 20:30:04 +0200 Subject: [PATCH] Skip the adapter-triggered reconnect while the stream is up --- lib/database/powersync/powersync.dart | 19 ++++++++---- test/database/powersync/powersync_test.dart | 32 +++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/lib/database/powersync/powersync.dart b/lib/database/powersync/powersync.dart index 1be44d8ab..eaf30a113 100644 --- a/lib/database/powersync/powersync.dart +++ b/lib/database/powersync/powersync.dart @@ -107,15 +107,16 @@ Future powerSyncInstance(Ref ref) async { // Gated on the network adapter only, never on the reachability probe: a // probe failure is an indication, an unreachable backend is PowerSync's own // retry loop to handle (the connector throttles its log output while the - // backend does not answer). Reconnecting while already connected is safe and - // doubles as the "retry now" signal when the adapter comes back. + // backend does not answer). void syncConnection(bool hasAdapter) { if (hasAdapter) { final serverUrl = ref.read(wgerBaseProvider).serverUrl; - if (serverUrl != null) { - connectPowerSync(db, serverUrl, client, watchdog, reason: 'network adapter available'); - } else { + if (serverUrl == null) { _logger.info('Network adapter available, but no server configured: not connecting'); + } else if (skipAdapterReconnect(db.currentStatus)) { + _logger.fine('Sync already connected, skipping reconnect'); + } else { + connectPowerSync(db, serverUrl, client, watchdog, reason: 'network adapter available'); } } else { _logger.info('No network adapter, disconnecting from the sync service'); @@ -214,6 +215,14 @@ Future _createRawTables(PowerSyncDatabase db) async { }); } +/// Whether an adapter-triggered connect may be skipped: only while the stream +/// is already up. The SDK's connect() is never a no-op and would abort a +/// healthy stream; every other state (connecting, error, retry delay) still +/// reconnects, which doubles as the "retry now" signal when the adapter comes +/// back. Deliberate reconnects (login, manual retry) bypass this guard. +@visibleForTesting +bool skipAdapterReconnect(SyncStatus status) => status.connected; + /// Creates a fresh [DjangoConnector] for [baseUrl] and connects [db] to it. /// Used both at initial creation and after a logout/login cycle to pick up /// the new user's server URL / credentials. [client] is the authenticated diff --git a/test/database/powersync/powersync_test.dart b/test/database/powersync/powersync_test.dart index aeb707eb9..fc5b4368f 100644 --- a/test/database/powersync/powersync_test.dart +++ b/test/database/powersync/powersync_test.dart @@ -19,6 +19,7 @@ import 'package:drift/drift.dart' show DriftSqlType, Table, TableInfo; import 'package:drift/native.dart'; import 'package:flutter_test/flutter_test.dart'; +import 'package:powersync/powersync.dart' show SyncStatus; import 'package:wger/core/http_overrides.dart'; import 'package:wger/database/powersync/database.dart'; import 'package:wger/database/powersync/powersync.dart'; @@ -115,4 +116,35 @@ void main() { expect(syncOptionsFor('https://gym.example.com'), isNull); }); }); + + group('skipAdapterReconnect', () { + // SyncStatus has only @internal constructors, so there is no public way + // to build one in a test. + SyncStatus status({bool connected = false, bool connecting = false, Object? downloadError}) => + // ignore: invalid_use_of_internal_member + SyncStatus( + connected: connected, + connecting: connecting, + lastSyncedAt: null, + downloadProgress: null, + downloading: false, + uploading: false, + downloadError: downloadError, + uploadError: null, + priorityStatusEntries: const [], + streamSubscriptions: null, + ); + + test('skips only while the stream is up', () { + expect(skipAdapterReconnect(status(connected: true)), isTrue); + }); + + test('still reconnects in every other state', () { + // connecting and the retry loop after an error are the "retry now" + // cases an adapter comeback must be able to interrupt. + expect(skipAdapterReconnect(status(connecting: true)), isFalse); + expect(skipAdapterReconnect(status(downloadError: Exception('stream died'))), isFalse); + expect(skipAdapterReconnect(status()), isFalse); + }); + }); }