Repository navigation
Description
Description
GetThreadLocalStaticBase in src/coreclr/vm/threadstatics.cpp can write through a freed InFlightTLSData entry. The process crashes with SIGSEGV in HndWriteBarrierWorker, or macOS malloc aborts on freelist corruption. It happens when all of these are true:
- A collectible type's thread static is created while that type's class constructor is still running, which leaves an entry in
t_ThreadStatics.pInFlightData. - The type's
AssemblyLoadContextis unloaded and collected before that thread accesses the static again, so the entry is never consumed. - A new collectible type is given the freed TLS index, and its first thread-static access happens on the same thread.
We hit this in the Unity Editor but it reproduces in a standalone dotnet app as well on main.
Reproduction Steps
Lib/Lib.cs
using System;
using System.Runtime.CompilerServices;
public static class A
{
[ThreadStatic] static object s_Value;
static A() { s_Value = new object(); }
[MethodImpl(MethodImplOptions.NoInlining)] public static void Touch() { }
}
public static class B
{
[ThreadStatic] static object s_Value;
[MethodImpl(MethodImplOptions.NoInlining)] public static void Set() { s_Value = new object(); GC.KeepAlive(s_Value); }
}
App/Program.cs
using System;
using System.Runtime.CompilerServices;
using System.Runtime.Loader;
using System.Threading;
static class Program
{
static int Main(string[] args)
{
string libPath = args[0];
// 1. Run A's class constructor in a collectible ALC on this thread, then unload the ALC.
WeakReference alc = RunInCollectibleAlc(libPath, "A", "Touch");
for (int i = 0; i < 100 && alc.IsAlive; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
}
// Give the LoaderAllocator time to be destroyed and free its TLS indices.
for (int i = 0; i < 20; i++)
{
GC.Collect();
GC.WaitForPendingFinalizers();
Thread.Sleep(10);
}
Console.WriteLine($"first ALC alive after unload: {alc.IsAlive}");
// 2. A new collectible type reuses the freed TLS index; first access on the same thread.
RunInCollectibleAlc(libPath, "B", "Set");
Console.WriteLine("PASS");
return 0;
}
[MethodImpl(MethodImplOptions.NoInlining)]
static WeakReference RunInCollectibleAlc(string libPath, string typeName, string methodName)
{
var context = new AssemblyLoadContext(typeName, isCollectible: true);
var assembly = context.LoadFromAssemblyPath(libPath);
assembly.GetType(typeName)!.GetMethod(methodName)!.Invoke(null, null);
context.Unload();
return new WeakReference(context);
}
}
Build & Run:
dotnet build -c Release Lib -o out/lib
dotnet build -c Release App -o out/app
dotnet out/app/App.dll $PWD/out/lib/Lib.dll
Expected behavior
The program prints PASS and exits with 0. B gets a new thread-static block.
Actual behavior
The program prints first ALC alive after unload: False, then crashes before it prints PASS. On .NET 10.0.12 (osx-arm64) it crashed in every run we tried. The exit code is either 139 (SIGSEGV) or 133 (SIGTRAP, macOS malloc detecting a corrupted freelist while the crash is being handled).
Main thread under lldb:
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x1ffffffffffffe0)
* frame #0: libcoreclr.dylib`HndWriteBarrierWorker(OBJECTHANDLE__*, Object*) + 36
frame #1: libcoreclr.dylib`GCHandleManager::StoreObjectInHandle(OBJECTHANDLE__*, Object*) + 48
frame #2: libcoreclr.dylib`GetThreadLocalStaticBase(TLSIndex) + 1600
frame #3: libcoreclr.dylib`GetThreadStaticsByMethodTable + 196
frame #4: <managed: B.Set>
...
frame #8: libcoreclr.dylib`RuntimeMethodHandle_InvokeMethod + 2152
A checked build hits this assert:
ASSERT FAILED
Expression: handle
Location: line 12 in src/coreclr/vm/gchandleutilities.cpp
Function: ValidateHandleAssignment
Regression?
Yes, this is a regression from .NET 8. We ran the same repro 5 times on each runtime (osx-arm64):
| Runtime | Result |
|---|---|
| .NET 8.0.31 | PASS, 5/5 |
| .NET 9.0.20 | SIGSEGV (exit 139), 5/5 |
| .NET 10.0.12 | crash (exit 139 or 133), 3/3 |
InFlightTLSData was added in #99183 ("Make normal statics simpler"), which shipped in .NET 9.
Known Workarounds
No response
Configuration
- .NET 10.0.12 (Microsoft build), also 10.0.9
- macOS 27 arm64
- The code is not platform-specific, so we expect other platforms to be affected too. We have only tested osx-arm64.
Other information
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
Activity
dotnet-policy-service commentedon Sep 30, 2026
Tagging subscribers to this area: @agocke
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon Sep 30, 2026
Probably the same issue as #134928
masonwheeler commentedon Sep 30, 2026
Wow, this does look like it's likely to be the same as my issue, and with a cleaner repro too. What are the odds that a bug like this has been around since .NET 9 and two entirely independent projects report it within 2 hours of each other? 😯
jkotas commentedon Sep 30, 2026
cc @davidwrighton
davidwrighton commentedon Sep 30, 2026
@masonwheeler it seems like this should be rare, but it happens fairly often that independent repros of longstanding bugs come out within a week or so. I have no idea what rules of probability make this happen.
UnityAlex commentedon Sep 30, 2026
We just happened to be expanding our usage of ALCs recently which is why we ran into it 😄
CloneXpert commentedon Oct 8, 2026
Hi, I've also run into this 3 weeks ago, but didn't think it would be due to a runtime bug. Glad to have found this issue and hope this gets fixed soon as there's no reasonable workaround available (that I know of). What would be the timeline for the fix and backport to .net 10?
Fix collectible thread-static lifetime and index reuse (dotnet#134983)
[release/10.0] Fix collectible thread-static lifetime and index reuse (…
[release/11.0] Fix collectible thread-static lifetime and index reuse (…