Repository navigation
fix: close reentrancy gap on the contended placement-lease path - #57
Merged
Merged
Conversation
ViewPlacementCoordinator.EnterAsync's fast/reentrant paths write the holder marker in a plain (non-async) method body so it stays visible to the caller, but the genuinely-contended path delegated to an async EnterSlowAsync helper - and since AsyncLocal writes made inside a called async method never become visible to that method's own caller once it returns, a lease acquired via that slow path was never recognized as held by IsHeld() or by a nested reentrant EnterAsync call on the same region, reproducing both the spurious InvalidOperationException from RestorePlacementAsync's lease check and the original self-deadlock the reentrant fast path was built to fix. Replace the AsyncLocal<int> depth counter with an AsyncLocal<Guid> flow id established once per logical operation via a new EnsureFlow(), called synchronously at every entry point before EnterAsync (parent to child AsyncLocal flow always works; only child-to-caller propagation after a suspension does not). The "who currently holds this region" marker itself is a plain field on the shared per-region gate, not an AsyncLocal, so setting it from inside the async slow path is fine - only the flow id needs the caller-established treatment. Added ViewPlacementCoordinatorTests covering both the fast-path reentrant case and the previously-broken contended-then-reentrant case. Also: synchronize WeakUnloadObserver's ConditionalWeakTable read-modify-write (Add throws on a concurrent duplicate-key race), and set up the indicator host in RequestNavigateAsync before job scheduling and lease acquisition - not just inside CreateNavigateTask - so an error indicator can still be shown if either of those throw before reaching it. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This was referenced Sep 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces #56, which had to be closed because its branch (
codex/region-floating) predates PR #55's squash-merge and compared against main as 15 conflicting commits. This PR carries the exact same single commit, cherry-picked onto a fresh branch off currentmain- clean, no conflicts, 1 commit.PR #55 was merged before this commit finished pushing, so the fix below never landed on
main.EnterSlowAsync) set its reentrancy marker via anAsyncLocal<int>write made inside an async method — which never becomes visible to that method's own caller once it returns (the compiler-generated state machine restores the caller's capturedExecutionContexton resume, even when the method never truly suspends). This reproduced both the original self-deadlock the reentrant-lease fix was meant to prevent, and a spuriousInvalidOperationExceptionfromRestorePlacementAsync'sIsHeld()guard, whenever the first lease acquisition for a region happened to be genuinely contended. Replaced theAsyncLocal<int>depth counter with anAsyncLocal<Guid>flow id established once per logical operation via a newEnsureFlow(), called synchronously at every entry point beforeEnterAsync— since AsyncLocal always propagates correctly parent→child, only the old child→caller-after-suspension direction was broken. The "who holds this region" marker itself is a plain field on the shared per-region gate object, not an AsyncLocal, so writing it from inside the async slow path is fine.ConditionalWeakTableread-modify-write sequence (Addthrows on a concurrent duplicate-key race).RequestNavigateAsyncbefore job scheduling and lease acquisition — not just insideCreateNavigateTask— so an error indicator can still be shown if either of those throw before reaching it.Added
ViewPlacementCoordinatorTestscovering both the fast-path reentrant case and the previously-broken contended-then-reentrant case.Test plan
dotnet build tests/AsyncNavigation.Tests(Release) — 0 errorsdotnet test tests/AsyncNavigation.Tests— 157 passeddotnet test tests/AsyncNavigation.E2E.Tests— 9 passedViewPlacementCoordinatorTests(2 tests) reproduce and verify the fix for the contended-lease self-deadlock/IsHeld gapCo-Authored-By: Claude Sonnet 5 noreply@anthropic.com