diff --git a/lib/core/network/auth_notifier.dart b/lib/core/network/auth_notifier.dart index 1ac1c4a99..07adcf158 100644 --- a/lib/core/network/auth_notifier.dart +++ b/lib/core/network/auth_notifier.dart @@ -88,6 +88,11 @@ class AuthNotifier extends _$AuthNotifier { @visibleForTesting Future? revalidationDone; + /// Listeners armed by [_scheduleRevalidation], replaced on re-arm: the + /// provider is keepAlive, so stacked ones would live for good. + StreamSubscription>? _connectivitySub; + AppLifecycleListener? _lifecycleListener; + /// Number of times the user-switch DB wipe has fired since the notifier /// was built. Exposed so tests can assert the user-mismatch path ran /// without having to instrument PowerSync or the filesystem. @@ -105,6 +110,10 @@ class AuthNotifier extends _$AuthNotifier { _client = ref.read(authHttpClientProvider); _storage = ref.read(authCredentialsStorageProvider); _gating = ref.read(serverGatingProvider); + ref.onDispose(() { + _connectivitySub?.cancel(); + _lifecycleListener?.dispose(); + }); return _tryAutoLogin(); } @@ -527,20 +536,21 @@ class AuthNotifier extends _$AuthNotifier { void _scheduleRevalidation() { revalidationDone = Future(_revalidate); - final sub = Connectivity().onConnectivityChanged.listen((results) { + _connectivitySub?.cancel(); + _connectivitySub = Connectivity().onConnectivityChanged.listen((results) { final online = results.any((r) => r != ConnectivityResult.none); if (online) { revalidationDone = _revalidate(); } }); - ref.onDispose(sub.cancel); // A warm resume must also revalidate: the process can stay alive in the // background for days, so the tokens may have expired without any cold // start noticing. The app is offline-first, so without this a dead // session would only surface once some server-backed action happens to // run. Gated on needsRefresh so quick app switches stay request-free. - final lifecycleListener = AppLifecycleListener( + _lifecycleListener?.dispose(); + _lifecycleListener = AppLifecycleListener( onResume: () { final credential = state.asData?.value.credential; if (credential?.needsRefresh(refreshLeeway) ?? false) { @@ -549,7 +559,6 @@ class AuthNotifier extends _$AuthNotifier { } }, ); - ref.onDispose(lifecycleListener.dispose); } /// Revalidates the restored session against the server. Fire-and-forget: it diff --git a/test/core/network/auth_notifier_powersync_test.dart b/test/core/network/auth_notifier_powersync_test.dart index 5d953dd73..b2b9e3b21 100644 --- a/test/core/network/auth_notifier_powersync_test.dart +++ b/test/core/network/auth_notifier_powersync_test.dart @@ -358,6 +358,28 @@ void main() { verifyNever(mockClient.head(tProbe, headers: anyNamed('headers'))); }); + + test('a retried auto-login replaces the listener instead of stacking one', () async { + final container = makeContainer(); + await container.read(authProvider.future); + final notifier = container.read(authProvider.notifier); + await notifier.revalidationDone; + + // What the recovery screens' retry button runs; it re-arms the listener + await notifier.retryAutoLogin(); + await notifier.revalidationDone; + await pumpEventQueue(); + // Consume the probes the two scheduling runs fired themselves + verify(mockClient.head(tProbe, headers: anyNamed('headers'))); + + connectivityStream.add(const [ConnectivityResult.wifi]); + await pumpEventQueue(); + await notifier.revalidationDone; + await pumpEventQueue(); + + // One reconnect, one probe + verify(mockClient.head(tProbe, headers: anyNamed('headers'))).called(1); + }); }); group('never-synced session: PowerSync reachability', () {