Fix !m_RedirectContextInUse assert in RestoreContextSimulated on win-x86#127638
Draft
Fix !m_RedirectContextInUse assert in RestoreContextSimulated on win-x86#127638
!m_RedirectContextInUse assert in RestoreContextSimulated on win-x86#127638Conversation
…ort() with COMPlusCheckForAbort() in RestoreContextSimulated Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/8fa490ef-11e1-443a-bf1d-8d2d2dc09add Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix !m_RedirectContextInUse assert on win-x86
Fix May 1, 2026
!m_RedirectContextInUse assert in RestoreContextSimulated on win-x86
Contributor
|
Tagging subscribers to this area: @agocke |
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.
main PR
Description
On x86 Windows without
RtlRestoreContext,RestoreContextSimulatedcalledHandleThreadAbort()whilem_RedirectContextInUsewas stilltrue.HandleThreadAbort()constructs aThreadAbortExceptionby running managed code (resource string loading, etc.), during which a concurrent GC redirect firesMarkRedirectContextInUse()→ assert!m_RedirectContextInUse.Fix: Replace
HandleThreadAbort()with the same abort-redirect pattern already used by the non-x86 path inRedirectedHandledJITCase:CopyOSContext+COMPlusCheckForAbort()(bothNOTHROW/GC_NOTRIGGER— no managed code)pCtxIP toThrowControlForThreadand store the resume PC inm_OSContextEXCEPTION_HIJACKpath, which pops theRedirectedThreadFrameand callsUnmarkRedirectContextInUsebefore resuming at the abort handlerThe abort exception is now raised only after the redirect context is fully released, eliminating the re-entrancy window.
Customer Impact
Checked (debug) builds of .NET on Windows x86 crash with
STATUS_FAIL_FAST_EXCEPTIONwhenControlledExecution.Run(or any code path that triggers a thread abort) races with a GC redirect on the same thread. Release builds are not protected by the assert but the re-entrant redirect corrupts the saved context.Regression
Not a regression introduced in the most recent release; latent race in the x86-only
RestoreContextSimulatedpath.Testing
Built
clr.runtime(checked) with 0 errors/0 warnings. The affected code path is x86 Windows–specific (#ifdef TARGET_X86/#ifndef TARGET_UNIX) and not exercisable in a Linux x64 CI environment; the fix is a mechanical port of the existing non-x86 abort-redirect pattern that is already proven correct on all other platforms.Risk
Low. The change is confined to the x86-only
RestoreContextSimulatedfunction and replacesHandleThreadAbort()withCOMPlusCheckForAbort()— an exact match of the logic already in use on every other code path throughRedirectedHandledJITCase. No new mechanism is introduced.Package authoring no longer needed in .NET 9
IMPORTANT: Starting with .NET 9, you no longer need to edit a NuGet package's csproj to enable building and bump the version.
Keep in mind that we still need package authoring in .NET 8 and older versions.