Skip to content

!m_RedirectContextInUse Assert on win-x86 #127637

Closed

Description

@jkotas

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

  1. Thread 12 runs GCExtendedTests.GetTotalAllocatedBytes() → GC.GetTotalAllocatedBytesPrecise() → triggers a GC collection
  2. GC suspension begins. Thread 11 is in Cooperative mode running a test via SkippedFactTestCase.RunAsync. The runtime redirects (hijacks) Thread 11 via RedirectedHandledJITCaseForGCThreadControl_Stub (1st redirect)
  3. Inside RedirectedHandledJITCase → RestoreContextSimulated, the runtime calls Thread::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() throws PlatformNotSupportedException; thread aborts can only be initiated by the runtime internally or via ControlledExecution.Run
  4. HandleThreadAbort → CreateThrowable → CallDefaultConstructor for ThreadAbortException — this runs managed code to construct the exception (loading the resource string "Arg_SystemException" via SR.GetResourceString → ResourceReader.LoadString → BinaryReader.ReadByte)
  5. The managed code runs in Cooperative mode, making Thread 11 eligible for GC hijacking again
  6. A second GC triggers (or GC suspension retries). The runtime attempts to redirect Thread 11 again via RedirectedHandledJITCaseForGCThreadControl_Stub (2nd redirect)
  7. MarkRedirectContextInUse finds m_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 Debug only 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_RedirectContextInUse assert 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

  1. added
    untriaged
    on May 1, 2026
  2. dotnet-policy-service commented on May 1, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
    See info in area-owners.md if you want to be subscribed.

  3. jkotas commented on May 1, 2026

    @jkotas
    MemberAuthor

    surprised that ThreadAbort is being throw here?

    It is from ControlledExecution.Run that is a best-effort version of Thread.Abort for non-production uses.

  4. added a commit that references this issue on May 1, 2026
    bd9e85e
  5. removed
    untriaged
    on May 1, 2026
  6. locked and limited conversation to collaborators on Jun 1, 2026
  7. added a commit that references this issue on Jul 15, 2026
    488b102
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions