From d34d755a53357fccd11b26cf80f83fabbb9943aa Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Sat, 24 Oct 2026 06:17:50 -0400 Subject: [PATCH 1/2] Lite: the route cache's background reload stays running while it still has a load to do --- Lite/Services/NotificationRouteCache.cs | 27 ++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/Lite/Services/NotificationRouteCache.cs b/Lite/Services/NotificationRouteCache.cs index 52de90ccd6..e75d2a651c 100644 --- a/Lite/Services/NotificationRouteCache.cs +++ b/Lite/Services/NotificationRouteCache.cs @@ -190,20 +190,29 @@ 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; + /* 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()); + Volatile.Write(ref _backgroundRunning, 0); - /* 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. */ + /* 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; From 3b6389cc9be29ed49d0f5dad5753fc77347339b3 Mon Sep 17 00:00:00 2001 From: Erik Darling <2136037+erikdarlingdata@users.noreply.github.com> Date: Sat, 24 Oct 2026 06:18:49 -0400 Subject: [PATCH 2/2] Lite: clear the reload flag with a full fence so the look after it cannot run first --- Lite/Services/NotificationRouteCache.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lite/Services/NotificationRouteCache.cs b/Lite/Services/NotificationRouteCache.cs index e75d2a651c..c05cd05ee8 100644 --- a/Lite/Services/NotificationRouteCache.cs +++ b/Lite/Services/NotificationRouteCache.cs @@ -199,7 +199,9 @@ reload as finished with that load still to come. */ } while (MoreToLoad()); - Volatile.Write(ref _backgroundRunning, 0); + /* 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). */