Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions Lite/Services/NotificationRouteCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Comment on lines +196 to +200

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, on the "cannot spin" argument in the body. It is sound as written — on a pool thread OnApplicationDispatcher() (:105-106) is false, so Current takes the gate path and either advances _loadedVersion (:261) or arms _retryAfterTicks (:268), and MoreToLoad() goes false either way.

What the argument now rests on that it did not before is the injected predicate. NotificationRouteCacheCore takes isUiThread as a ctor parameter, and if one ever returns true on the loop's own thread, Current returns at :239-243 without loading and without arming the retry window, so MoreToLoad() stays true and this inner loop spins hot. The old shape spun there too, so this is not a regression — but it spun with the flag toggling, where this one spins with the flag pinned at 1, which turns a CPU burn into a hang in every while (BackgroundReloadRunning) poll (NotificationRouteCacheBackgroundTests.cs:35, NotificationRouteFirstStartTests.cs:97, the latter uncapped).

No current caller does it — both injected predicates compare against the test thread id (NotificationRouteCacheBackgroundTests.cs:136 and :169) and the loop runs under Task.Run (:186) — so nothing to fix here. It is just worth knowing that the loop's termination proof is now a property of the predicate, not of this method.


/* 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);
Comment on lines +204 to +209

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the flag-false-with-a-load-pending window is narrowed, not closed.

Take this interleaving:

  1. inner MoreToLoad() at :200 reads false (_loadedVersion == _version) and the inner loop exits;
  2. a writer's Invalidate() increments _version, then StartBackgroundReload CASes, sees the flag still 1 and starts nothing;
  3. :204 clears the flag;
  4. :209 reads MoreToLoad() true, re-takes the flag and loads.

Between (3) and (4) BackgroundReloadRunning is false while a load of this same reload is still to come — the shape the PR fixes, now two atomic instructions wide instead of one DuckDB read wide. Any single-flag design has it; closing it needs a separate "pending" count the observer reads with the flag. Not worth changing for the test this PR unflakes (ManyInvalidatesDuringOneReload_… issues every Invalidate while load 1 is in flight, so they are all absorbed by the inner loop at :196-200 and never reach this gap), but the body's "BackgroundReloadRunning no longer drops to false between two loads of one reload" reads as absolute and is not.

}

/* 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);

/// <summary>Whether a background reload is running; tests wait for it to finish.</summary>
internal bool BackgroundReloadRunning => Volatile.Read(ref _backgroundRunning) != 0;

Expand Down
Loading