Repository navigation
Lite: the route cache's background reload stays running while it still has a load to do #6711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d34d755
3b6389c
688dccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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); | ||
|
Comment on lines
+204
to
+209
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Between (3) and (4) |
||
| } | ||
|
|
||
| /* 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; | ||
|
|
||
|
|
||
There was a problem hiding this comment.
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, soCurrenttakes the gate path and either advances_loadedVersion(:261) or arms_retryAfterTicks(:268), andMoreToLoad()goes false either way.What the argument now rests on that it did not before is the injected predicate.
NotificationRouteCacheCoretakesisUiThreadas a ctor parameter, and if one ever returns true on the loop's own thread,Currentreturns at :239-243 without loading and without arming the retry window, soMoreToLoad()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 at1, which turns a CPU burn into a hang in everywhile (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:136and:169) and the loop runs underTask.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.