From fcd277e3ba76776e2a89d64a313e6cc40ca92221 Mon Sep 17 00:00:00 2001 From: Jonas Bark Date: Fri, 21 Aug 2026 09:07:18 +0200 Subject: [PATCH] fix(local-network): don't let a pre-Settings probe repopulate the cache MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit invalidate() cleared the cached status but did nothing about a probe already in flight. That probe then completed and wrote its answer back — the answer from before whatever prompted the invalidation. The case that matters is the one invalidate() exists for: the user is told Local Network is off, taps through to System Settings, grants it and comes back. If a probe was in flight when they left, its stale "denied" lands afterwards and becomes the cached truth, so the app still claims the permission is off. Tag each probe with a generation, bump it in invalidate(), and only write the cache when the generation is still current. Whoever is awaiting the stale probe still gets its result — it just doesn't become the shared answer. invalidate() also drops _inFlight now, so the next caller starts a fresh probe instead of being handed the pre-invalidation one. Found while chasing an intermittent test failure. That turned out to be an unrelated ambient issue (a MissingPluginException on the app_links EventChannel that attaches to whichever test is in flight, which already fails ten other files), but the race it exposed here was real. Co-Authored-By: Claude Opus 5 (1M context) --- lib/services/local_network_access.dart | 20 +++++++++++-- test/services/local_network_access_test.dart | 29 +++++++++++++++++++ .../utils/local_network_requirement_test.dart | 2 +- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/services/local_network_access.dart b/lib/services/local_network_access.dart index 6a6b9e10..f04cc4ac 100644 --- a/lib/services/local_network_access.dart +++ b/lib/services/local_network_access.dart @@ -33,6 +33,11 @@ class LocalNetworkAccess { static DateTime? _cachedAt; static Future? _inFlight; + /// Bumped by [invalidate]. A probe that started in an earlier generation + /// describes the world before whatever prompted the invalidation, so it must + /// not write its answer back into the cache. + static int _generation = 0; + /// The last known status without probing, or null if nothing is cached. static LocalNetworkStatus? get cached => _isFresh ? _cached : null; @@ -58,6 +63,7 @@ class LocalNetworkAccess { } static Future _probe() async { + final generation = _generation; LocalNetworkStatus result; try { result = await LocalNetworkPermission.check(timeout: probeTimeout); @@ -66,8 +72,13 @@ class LocalNetworkAccess { // A broken probe must not lock the user out of their trainer app. result = LocalNetworkStatus.unknown; } - _cached = result; - _cachedAt = now(); + // Still answer whoever is awaiting this probe, but a stale generation must + // not repopulate the cache: the user has been to System Settings since it + // started, so this result describes the permission as it was beforehand. + if (generation == _generation) { + _cached = result; + _cachedAt = now(); + } return result; } @@ -76,8 +87,12 @@ class LocalNetworkAccess { /// Call this whenever the user has been somewhere they could have changed /// the answer — the System Settings trip in particular. static void invalidate() { + _generation++; _cached = null; _cachedAt = null; + // Drop the in-flight probe too, so the next caller starts a fresh one + // rather than being handed the pre-invalidation answer. + _inFlight = null; } /// Whether local-network features should be allowed to run. @@ -92,7 +107,6 @@ class LocalNetworkAccess { @visibleForTesting static void resetForTest() { invalidate(); - _inFlight = null; now = DateTime.now; } } diff --git a/test/services/local_network_access_test.dart b/test/services/local_network_access_test.dart index 5f743f6e..e1ff92e2 100644 --- a/test/services/local_network_access_test.dart +++ b/test/services/local_network_access_test.dart @@ -96,6 +96,35 @@ void main() { expect(probes, 1); }); + test('a probe in flight when the cache is invalidated does not repopulate it', () async { + // The real sequence: a probe starts, the user leaves for System Settings + // and flips the toggle, and the pre-Settings answer lands afterwards. It + // must not become the cached truth. + final gate = Completer(); + mockNative(() => gate.future); + + final pending = LocalNetworkAccess.status(); + LocalNetworkAccess.invalidate(); + gate.complete('denied'); + expect(await pending, LocalNetworkStatus.denied, reason: 'the caller still gets its answer'); + + expect(LocalNetworkAccess.cached, isNull, reason: 'but the stale answer must not come back'); + }); + + test('invalidate makes the next caller start a fresh probe', () async { + final gate = Completer(); + mockNative(() => gate.future); + final stale = LocalNetworkAccess.status(); + + LocalNetworkAccess.invalidate(); + mockNative(() => 'granted'); + expect(await LocalNetworkAccess.status(), LocalNetworkStatus.granted); + + gate.complete('denied'); + await stale; + expect(LocalNetworkAccess.cached, LocalNetworkStatus.granted); + }); + test('a probe that failed does not wedge later probes', () async { mockNative(() => throw PlatformException(code: 'probe_unavailable')); expect(await LocalNetworkAccess.status(), LocalNetworkStatus.unknown); diff --git a/test/utils/local_network_requirement_test.dart b/test/utils/local_network_requirement_test.dart index 7cc0e3af..0ee61f6a 100644 --- a/test/utils/local_network_requirement_test.dart +++ b/test/utils/local_network_requirement_test.dart @@ -2,8 +2,8 @@ import 'dart:io'; import 'package:bike_control/gen/l10n.dart'; import 'package:bike_control/main.dart' show screenshotMode; -import 'package:bike_control/services/local_network_access.dart'; import 'package:bike_control/bluetooth/emulation/emulated_ble_platform.dart'; +import 'package:bike_control/services/local_network_access.dart'; import 'package:bike_control/utils/core.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; import 'package:shared_preferences/shared_preferences.dart';