Repository navigation
Description
Btw, there is a libraries x86 failure but doesnt look like that is related to this change. The auto analysis is below. @VSadov surprised that ThreadAbort is being throw here?
!m_RedirectContextInUse Assert on win-x86 — Analysis
Assert
Assert failure (Thread 0x590): !m_RedirectContextInUse
File: src/coreclr/vm/threads.h:3104
Image: dotnet.exe (x86 Checked)
Exit code: -1073740286 (STATUS_FAIL_FAST_EXCEPTION)
Fires in Thread::MarkRedirectContextInUse when the runtime attempts to redirect (hijack) a thread whose redirect context is already occupied from a prior redirect.
Thread State at Crash
| Thread | OSID | GC Mode | Role | Activity |
|---|---|---|---|---|
| 11 | 0x590 | Cooperative | Crash thread | Inside 1st redirect → HandleThreadAbort → constructing ThreadAbortException → 2nd redirect fires assert |
| 12 | 0xccc | Cooperative | GC thread | Running GCExtendedTests.GetTotalAllocatedBytes() → GC.GetTotalAllocatedBytesPrecise() → triggered GC |
| 6 | 0x101c | Preemptive | Tiered compilation | JIT compiling, blocked in WaitUntilGCComplete |
| 5 | 0x1910 | Preemptive | Finalizer | Waiting for finalizer event |
| 0 | 0x1ad8 | Preemptive | Main | xunit runner waiting |
Full Stack — Thread 11 (OSID 0x590) — Native + Managed
Shows two nested redirects — the second one fires the assert:
=== 2nd redirect (fires the assert) ===
coreclr!FailFastOnAssert
coreclr!_DbgBreakCheck
coreclr!_DbgBreakCheckNoThrow
coreclr!DbgAssertDialog
coreclr!Thread::MarkRedirectContextInUse ← 2nd redirect: context already in use!
coreclr!Thread::RedirectedHandledJITCase ← 2nd redirect handler
coreclr!Thread::RedirectedHandledJITCaseForGCThreadControl
coreclr!RedirectedHandledJITCaseForGCThreadControl_Stub ← 2nd GC hijack entry point
=== Managed code running inside 1st redirect (ThreadAbortException construction) ===
System.IO.BinaryReader.ReadByte()
System.IO.BinaryReader.Read7BitEncodedInt()
System.Resources.ResourceReader.LoadString(Int32)
System.Resources.RuntimeResourceSet.ReadValue(...)
System.Resources.RuntimeResourceSet.GetObject(...)
System.Resources.RuntimeResourceSet.GetString(...)
System.Resources.ResourceManager.GetString(...)
System.SR.InternalGetResourceString(String)
System.SR.GetResourceString(String)
System.SR.get_Arg_SystemException()
System.SystemException..ctor()
System.Threading.ThreadAbortException..ctor()
System.Runtime.CompilerServices.RuntimeHelpers.CallDefaultConstructor(...)
=== 1st redirect processing the pending ThreadAbort ===
coreclr!CallDefaultConstructor
coreclr!EEException::CreateThrowable
coreclr!CLRException::GetThrowable
coreclr!CLRException::GetThrowableFromException
coreclr!Thread::HandleThreadAbort ← processes pending TS_AbortRequested
coreclr!Thread::RestoreContextSimulated
coreclr!Thread::RedirectedHandledJITCase ← 1st redirect handler
coreclr!Thread::RedirectedHandledJITCaseForGCThreadControl
coreclr!RedirectedHandledJITCaseForGCThreadControl_Stub ← 1st GC hijack entry point
=== Original managed code when 1st redirect hijacked the thread ===
[InlinedCallFrame] System.RuntimeMethodHandle.InvokeMethod(...)
System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object, IntPtr*)
System.Reflection.RuntimeMethodInfo.Invoke(...)
System.Reflection.MethodBase.Invoke(Object, Object[])
Xunit.Sdk.TestInvoker`1.CallTestMethod(Object)
Xunit.Sdk.TestInvoker`1+<>c__DisplayClass47_0.<<InvokeTestMethodAsync>b__1>d.MoveNext()
... (xunit async test invocation pipeline) ...
Xunit.Sdk.XunitTestCase.RunAsync(IMessageSink, IMessageBus, Object[], ExceptionAggregator, CancellationTokenSource)
Microsoft.DotNet.XUnitExtensions.SkippedFactTestCase.<>n__0(...)
Microsoft.DotNet.XUnitExtensions.SkippedFactTestCase+<RunAsync>d__2.MoveNext()
... (xunit async test runner pipeline) ...
Xunit.Sdk.XunitTestAssemblyRunner+<RunTestCollectionAsync>d__18.MoveNext()
System.Threading.ExecutionContext.RunInternal(...)
... (async continuations) ...
System.Threading.Tasks.Task+CancellationPromise`1[[Boolean]].ITaskCompletionAction.Invoke(Task)
System.Threading.Tasks.CompletionActionInvoker.Execute()
System.Threading.ThreadPoolWorkQueue.Dispatch()
System.Threading.PortableThreadPool+WorkerThread.WorkerDoWork(PortableThreadPool)
System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
System.Threading.Thread.StartCallback(Thread*)
GC Thread Stack (Thread 12, OSID 0xccc)
System.GC.GetTotalAllocatedBytesPrecise() ← triggers GC
System.GC.GetTotalAllocatedBytes(Boolean)
System.Tests.GCExtendedTests.GetTotalAllocatedBytes() ← the test
... (xunit async pipeline via reflection invoke) ...
System.Threading.PortableThreadPool+WorkerThread.WorkerDoWork(PortableThreadPool)
System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
System.Threading.Thread.StartCallback(Thread*)
Sequence of Events
- Thread 12 runs
GCExtendedTests.GetTotalAllocatedBytes()→GC.GetTotalAllocatedBytesPrecise()→ triggers a GC collection - GC suspension begins. Thread 11 is in Cooperative mode running a test via
SkippedFactTestCase.RunAsync. The runtime redirects (hijacks) Thread 11 viaRedirectedHandledJITCaseForGCThreadControl_Stub(1st redirect) - Inside
RedirectedHandledJITCase→RestoreContextSimulated, the runtime callsThread::HandleThreadAbort— a pending thread abort (TS_AbortRequested) was set on Thread 11. The source of the abort is not captured in the dump. On modern .NET (6+),Thread.Abort()throwsPlatformNotSupportedException; thread aborts can only be initiated by the runtime internally or viaControlledExecution.Run HandleThreadAbort→CreateThrowable→CallDefaultConstructorforThreadAbortException— this runs managed code to construct the exception (loading the resource string "Arg_SystemException" viaSR.GetResourceString→ResourceReader.LoadString→BinaryReader.ReadByte)- The managed code runs in Cooperative mode, making Thread 11 eligible for GC hijacking again
- A second GC triggers (or GC suspension retries). The runtime attempts to redirect Thread 11 again via
RedirectedHandledJITCaseForGCThreadControl_Stub(2nd redirect) MarkRedirectContextInUsefindsm_RedirectContextInUse == true(still held from step 2) → assert fires
Key Finding
There is no separate cancellation thread visible in the dump. The ThreadAbort was a pending flag (TS_AbortRequested) already set on Thread 11. The source of the abort is not captured in the dump — on modern .NET, thread aborts can come from ControlledExecution.Run or other runtime-internal mechanisms. Thread::HandleThreadAbort processes this flag as part of the 1st redirect handler's normal flow (RestoreContextSimulated → HandleThreadAbort). The bug is that the 1st redirect handler runs managed code (exception construction) while still holding the redirect context, creating a window where a 2nd redirect can be attempted.
Root Cause
A double-redirect race condition on x86: the runtime hijacks a thread for GC suspension, processes a pending ThreadAbort inside the redirect handler, and the ThreadAbortException constructor runs managed code (resource string loading) in Cooperative mode. This makes the thread eligible for another GC redirect while the first redirect context is still held.
Why This Hasn't Been Seen Before
Libraries Test Run checked coreclr windows x86 Debugonly runs on PR builds, not main rolling builds- Main rolling builds run
Libraries Test Run checked coreclr windows x86 **Release**— Release libraries are faster, narrowing the timing window - Debug libraries' slower execution (resource loading, extra checks) widens the race window between the first redirect and the second attempt
- The
m_RedirectContextInUseassert only fires in Checked builds
Relation to PR #127595
Not related. The gc_unwind_x86.inl fix changes GC reporting logic inside EnumGcRefsX86. The Validate() change in interface.cpp adds a Debug assert in Promote. Neither affects thread redirect/hijack logic. This assert passed on the previous build of the same PR (build 1403154), confirming it's intermittent.
Originally posted by @mangod9 in #127595 (comment)
Activity
dotnet-policy-service commentedon May 1, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.
jkotas commentedon May 1, 2026
It is from
ControlledExecution.Runthat is a best-effort version of Thread.Abort for non-production uses.Fix `!m_RedirectContextInUse` assert in `RestoreContextSimulated` on …
Fix `!m_RedirectContextInUse` assert in `RestoreContextSimulated` on …