Skip to content

[release/10.0] Fix collectible thread-static lifetime and index reuse - #136481

Merged
JulieLeeMSFT merged 1 commit into
dotnet:release/10.0from
davidwrighton:backport/134983-release-10.0
Nov 3, 2026
Merged

JulieLeeMSFT merged 1 commit into
dotnet:release/10.0from
davidwrighton:backport/134983-release-10.0

Conversation

@davidwrighton

Copy link
Copy Markdown
Member

Fixes Issue #134928 and #134931

main PR #134983

Description

Backports #134983 to release/10.0. The corresponding .NET 11 backport is #136478.

A collectible thread-static index could become available for reuse while surviving threads still held loader-allocator-relative handles, weak handles, or in-flight initialization state belonging to its previous owner. Reusing the index could then corrupt memory, return stale thread-static storage, or access freed initialization data.

The fix quarantines retired indices until synchronized cleanup has removed the old state from every thread. It also corrects in-flight entry ownership, makes index publication failure-atomic, and fixes related handle cleanup and diagnostic memory enumeration.

This is a manual backport of commit fd41fc4d7de02724f0009872e52928d69a2ea999. Release-specific adaptations preserve .NET 10's existing SyncClean hash-table cleanup, its two-argument RestartEE contract, and CLASS__THREAD_BLOCKING_INFO initialization. Cleanup is gated on successful EE suspension without importing newer interpreter or multithreading feature guards. Tests use .NET 10's existing capability checks and TestLibrary project reference to retain the intended CoreCLR coverage.

Customer Impact

  • Customer reported
  • Found internally

Reported in #134928 and #134931. Applications that unload collectible assemblies containing thread-static fields can experience process crashes, unexpected null references, or memory corruption when another collectible type reuses an index. Correct behavior is for the replacement type to receive independent storage, with no references to the unloaded owner's state.

Reports include Windows x64 and macOS arm64, including a Unity Editor scenario. One customer reports repeated crashes during testing that are blocking a release. The underlying implementation is platform-independent CoreCLR code; applications that do not use collectible assemblies are not the reported scenario.

Avoiding collectible assembly loading is a reported workaround, but prevents the affected assemblies from being unloaded.

Regression

  • Yes
  • No

Regression introduced in .NET 9 by #99183 ("Make normal statics simpler"), which introduced the relevant thread-static index reuse and in-flight state machinery. The reporter of #134931 verified that the same reproduction passed on .NET 8.0.31 and crashed on .NET 9.0.20 and .NET 10.0.12.

Testing

Local validation used Windows x86, Checked CoreCLR, and Release libraries. The .NET 10 backport was built from release/10.0 commit 00a171f44e0 plus this fix. The .NET 11 comparison used the exact #136478 head, 5a1c26d6de811cb7a77a782eb29377f035621040, rather than the development branch. Core_Root's coreclr.dll SHA256 was checked against each newly built product DLL before execution.

The following commands passed in both release worktrees:

.\build.cmd -subset clr+libs+host -arch x86 -rc Checked -c Release
.\src\tests\build.cmd -Test Loader\CollectibleAssemblies\Statics\CollectibleTLSStaticCollection.csproj x86 Checked -SkipNative
.\src\tests\build.cmd -GenerateLayoutOnly x86 Checked

Both full builds and both test/layout builds completed with zero warnings and zero errors. The test builds selected the requested project.

For the execution commands below, $root was the worktree's artifacts\tests\coreclr\windows.x86.Checked\Tests\Core_Root, and $dir was its artifacts\tests\coreclr\windows.x86.Checked\Loader\CollectibleAssemblies\Statics\CollectibleTLSStaticCollection directory:

& "$root\corerun.exe" "$dir\CollectibleTLSStaticCollection.dll"
& "$root\corerun.exe" "$dir\ValidationRunner.dll" "$dir\CollectibleTLSStaticCollection.dll" all 5
& "$root\corerun.exe" C:\git\runtime\artifacts\tmp\tls-backport-validation\customer\built\Host.dll 300 none finalizer 2 4000 collectible 10

ValidationRunner.dll is a local reflection harness, not a repository change. It explicitly invokes ReuseCollectibleThreadStaticIndex, CleanupCollectibleThreadStaticsWhileStartingThreads, and TestEntryPoint five times each, logging every invocation. The customer Host and Plugin were built from the attached reproduction source, retargeted to net10.0 so the same binaries could run on both versions.

Runtime Generated regression executable Explicit regression invocations Original customer reproduction
Fixed .NET 10 (10.0.14-dev) Passed, exit 100 Passed, 15/15, exit 0 Passed, 90 contexts / 900 rounds in 302 seconds, exit 0
Fixed .NET 11 (11.0.0-dev, exact backport head) Passed, exit 100 Passed, 15/15, exit 0 Passed, 191 contexts / 1,910 rounds in 301 seconds, exit 0

As negative controls, the same customer reproduction was run with the same arguments on official unfixed Windows x86 runtimes .NET 10.0.12 and .NET 11.0.0-rc.1.26425.128. Both failed before completing the requested 300 seconds, with unexpected NullReferenceException failures in SegmentedArrayBuilder<T>.Expand through LINQ/PLINQ, matching the customer report.

The backport includes coverage for unloading a collectible type with in-flight thread-static initialization and then accessing a replacement type, plus cleanup while threads are being started. Existing coverage exercises cleanup during thread termination. The collection helper asserts that the first assembly load context was collected, rather than allowing the test to pass without reaching the unload scenario.

These are bounded local reliability runs, not a proof against all possible races or performance measurements. Other architectures and operating systems were not run locally.

Risk

Medium. This changes native runtime lifetime management and synchronized cleanup, so the consequences of a mistake could be significant. The cleanup walk also adds work when collectible TLS indices have been retired.

The fix targets the reported lifetime violations: indices cannot be reused until old per-thread state has been cleared; cleanup runs only after successful EE suspension; partially initialized threads without relevant storage are skipped before taking their TLS spin lock; and publication reserves cleanup-list capacity before exposing a new index. The manual adaptations retain .NET 10's surrounding cleanup behavior. Regression tests cover the in-flight lifetime and thread-start interactions. There are no public API changes.

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.

Resolves #134928
Resolves #134931

Note

This backport and PR description were prepared with GitHub Copilot.

Backport fd41fc4 (dotnet#134983), adapting synchronization cleanup and regression-test capabilities to release/10.0.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 7553307e-916a-4ea6-9eac-8adeb08e0f55
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
13 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@JulieLeeMSFT JulieLeeMSFT added the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Oct 27, 2026
@JulieLeeMSFT

Copy link
Copy Markdown
Member

We are validating the fix internally to gain more confidence on the fix. No merge until then.

@davidwrighton

Copy link
Copy Markdown
Member Author

@masonwheeler I've been asked if you could validate that this change fixes your issues. If you have difficulty building a private build of the runtime, please feel free to contact me at privately at [email protected] and I can provide some guidance on how to use a private build for your scenario, or I can provide assistance here if you'd prefer communication to happen publicly through github.

@masonwheeler

Copy link
Copy Markdown
Contributor

@davidwrighton It's been a while, but I seem to recall that there's a build script in the root of the repo. The real part I don't know is, once I've run a build, how do I "install" it such that when I run MSBuild or a compile in Visual Studio, it uses the newly-built runtime?

@davidwrighton

Copy link
Copy Markdown
Member Author

@masonwheeler The simplest way is to use the build script to build a retail build of the runtime locally (build.cmd clr -c release, and then build your application as self-contained. Then copy the coreclr.dll/System.Private.CoreLib.dll/clrjit.dll/mscordaccore.dll from your local retail build over top of the corresponding files in your self-contained build of your test application. If you need an installable SDK or nupkgs of the runtime or whatnot, that's somewhat more complicated.

@masonwheeler

Copy link
Copy Markdown
Contributor

@davidwrighton Yeah, I really do need a solution that will work for development in general.

@davidwrighton

Copy link
Copy Markdown
Member Author

Well, you could overwrite the binaries in your computer wide install in the program files directory (please keep backups of the old code). In some managed environments this may not work due to various policies that can be enabled. However, note that I'm asking for validation this fixes your problem, not promising that a local build will fix all problems. We certainly intend to fix this issue and release it once we can validate it's quality. Your validation would serve as one data point.

Comment thread src/coreclr/vm/threadstatics.cpp Resolved
@masonwheeler

Copy link
Copy Markdown
Contributor

@davidwrighton Confirmed. This fixes the issue for me.

@rbhanda rbhanda added this to the 10.0.16 milestone Nov 2, 2026
@davidwrighton davidwrighton added the Servicing-approved Approved for servicing release label Nov 3, 2026
@JulieLeeMSFT JulieLeeMSFT removed the NO-MERGE The PR is not ready for merge yet (see discussion for detailed reasons) label Nov 3, 2026
@JulieLeeMSFT
JulieLeeMSFT merged commit 51d7001 into dotnet:release/10.0 Nov 3, 2026
125 of 129 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-coreclr Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants