From 74fbb9db9530bbd61eadbbb16f981c9f1fd52acb Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Fri, 21 Aug 2026 14:51:09 +0200 Subject: [PATCH] Coalesce overlapping reachability probes --- lib/core/network/network_provider.dart | 29 ++++++++------------ test/core/network/network_provider_test.dart | 26 ++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/core/network/network_provider.dart b/lib/core/network/network_provider.dart index e96c865a8..520cfe276 100644 --- a/lib/core/network/network_provider.dart +++ b/lib/core/network/network_provider.dart @@ -163,9 +163,10 @@ class NetworkStatus extends _$NetworkStatus { /// re-probe backoff. int _failures = 0; - /// True from the moment a probe pass starts until it has its answer, so a - /// burst of failing requests triggers one probe instead of one each. - bool _probing = false; + /// The probe pass in flight, if any. Late callers join it instead of racing + /// it: overlapping passes would each count the same failed moment into + /// [_failures] and defeat the offline hysteresis. + Future? _inFlight; @override bool build() { @@ -208,12 +209,9 @@ class NetworkStatus extends _$NetworkStatus { } /// Reports that a real request failed with a network error, which triggers - /// a probe right away instead of waiting for the scheduled one. A probe - /// already in flight is answer enough. + /// a probe right away instead of waiting for the scheduled one. A failing + /// burst joins the probe pass the first failure started. void reportRequestFailure() { - if (_probing) { - return; - } unawaited(check()); } @@ -232,16 +230,11 @@ class NetworkStatus extends _$NetworkStatus { return _update(conn, timeout: timeout, optimistic: optimistic); }); - /// Runs one probe pass and marks it as in flight for [reportRequestFailure]. - /// The flag is set before the first await, so failures reported in the same - /// event-loop turn already see it. - Future _probeRun(Future Function() run) async { - _probing = true; - try { - return await run(); - } finally { - _probing = false; - } + /// Runs one probe pass, or joins the one already in flight. Coalescing is + /// safe against reentrancy: the synchronous prefix of [run] executes before + /// any other caller can observe [_inFlight]. + Future _probeRun(Future Function() run) { + return _inFlight ??= run().whenComplete(() => _inFlight = null); } /// Schedules the next probe: the idle cadence while things work, one of the diff --git a/test/core/network/network_provider_test.dart b/test/core/network/network_provider_test.dart index 880620668..4de5019e2 100644 --- a/test/core/network/network_provider_test.dart +++ b/test/core/network/network_provider_test.dart @@ -498,6 +498,32 @@ void main() { expect(container.read(networkStatusProvider), isTrue, reason: 'one failure is not offline'); }); + test('concurrent check calls join one probe pass and count one failure', () async { + // A timer or connectivity-event pass can start while another pass is + // mid-probe. Racing passes would each sample the same blip and count it + // twice, which is exactly what the offline hysteresis must not allow. + reachabilityCheck = (_, _, _) async => (reachable: true, reason: 'test'); + final container = makeContainer(serverUrl: 'https://wger.example'); + final notifier = container.read(networkStatusProvider.notifier); + await pumpEventQueue(); + + var probes = 0; + final pendingProbe = Completer(); + reachabilityCheck = (_, _, _) { + probes++; + return pendingProbe.future; + }; + final first = notifier.check(); + await pumpEventQueue(); + final second = notifier.check(); + + pendingProbe.complete((reachable: false, reason: 'test')); + await Future.wait([first, second]); + + expect(probes, 1); + expect(container.read(networkStatusProvider), isTrue, reason: 'one blip is one failure'); + }); + group('networkAdapterAvailableProvider', () { test('starts available and stays there once the platform confirms an adapter', () async { final container = makeContainer();