fix(local-network): don't let a pre-Settings probe repopulate the cache

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) <noreply@anthropic.com>
This commit is contained in:
Jonas Bark
2026-08-21 09:07:18 +02:00
parent 20788e46b6
commit fcd277e3ba
3 changed files with 47 additions and 4 deletions

View File

@@ -33,6 +33,11 @@ class LocalNetworkAccess {
static DateTime? _cachedAt;
static Future<LocalNetworkStatus>? _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<LocalNetworkStatus> _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;
}
}