diff --git a/Lite/Services/NotificationRouteCache.cs b/Lite/Services/NotificationRouteCache.cs index 52de90ccd6..c05cd05ee8 100644 --- a/Lite/Services/NotificationRouteCache.cs +++ b/Lite/Services/NotificationRouteCache.cs @@ -190,20 +190,31 @@ private void BackgroundReloadLoop() { do { - /* Current reloads when stale and logs a failed load itself, so nothing here can throw past a loader's catch. */ - _ = Current; - Volatile.Write(ref _backgroundRunning, 0); + /* Current reloads when stale and logs a failed load itself, so nothing here can throw past a loader's catch. The flag + stays set while an Invalidate during a load still needs one more: clearing it between loads let a reader see the + reload as finished with that load still to come. */ + do + { + _ = Current; + } + while (MoreToLoad()); - /* Look again after clearing the flag: an Invalidate that landed during the load saw the flag set and started - nothing, so this pass loads once more (or that Invalidate won the race and started its own loop). Stop while the - 30 second retry window is open, or a broken store would spin. */ + /* A full fence, not a release store: the look below must not be read before the clear is seen, or an Invalidate in + between would start nothing and this loop would stop with its change unloaded. */ + Interlocked.Exchange(ref _backgroundRunning, 0); + + /* Look again after clearing the flag: an Invalidate that landed between the last look and the clear saw the flag set + and started nothing, so this loop goes on (or that Invalidate won the race and started its own loop). */ } - while (Volatile.Read(ref _loader) is not null - && Volatile.Read(ref _loadedVersion) != Volatile.Read(ref _version) - && DateTime.UtcNow.Ticks >= Volatile.Read(ref _retryAfterTicks) - && Interlocked.CompareExchange(ref _backgroundRunning, 1, 0) == 0); + while (MoreToLoad() && Interlocked.CompareExchange(ref _backgroundRunning, 1, 0) == 0); } + /* Stale with a loader set, and not inside the 30 second retry window, or a broken store would spin. */ + private bool MoreToLoad() => + Volatile.Read(ref _loader) is not null + && Volatile.Read(ref _loadedVersion) != Volatile.Read(ref _version) + && DateTime.UtcNow.Ticks >= Volatile.Read(ref _retryAfterTicks); + /// Whether a background reload is running; tests wait for it to finish. internal bool BackgroundReloadRunning => Volatile.Read(ref _backgroundRunning) != 0;