Repository navigation
[release/10.0] Fix collectible thread-static lifetime and index reuse - #136481
Conversation
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: 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. |
|
Tagging subscribers to this area: @agocke |
|
We are validating the fix internally to gain more confidence on the fix. No merge until then. |
|
@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. |
|
@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? |
|
@masonwheeler The simplest way is to use the build script to build a retail build of the runtime locally ( |
|
@davidwrighton Yeah, I really do need a solution that will work for development in general. |
|
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. |
|
@davidwrighton Confirmed. This fixes the issue for me. |
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-argumentRestartEEcontract, andCLASS__THREAD_BLOCKING_INFOinitialization. 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
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
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.0commit00a171f44e0plus this fix. The .NET 11 comparison used the exact #136478 head,5a1c26d6de811cb7a77a782eb29377f035621040, rather than the development branch. Core_Root'scoreclr.dllSHA256 was checked against each newly built product DLL before execution.The following commands passed in both release worktrees:
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,
$rootwas the worktree'sartifacts\tests\coreclr\windows.x86.Checked\Tests\Core_Root, and$dirwas itsartifacts\tests\coreclr\windows.x86.Checked\Loader\CollectibleAssemblies\Statics\CollectibleTLSStaticCollectiondirectory:ValidationRunner.dllis a local reflection harness, not a repository change. It explicitly invokesReuseCollectibleThreadStaticIndex,CleanupCollectibleThreadStaticsWhileStartingThreads, andTestEntryPointfive times each, logging every invocation. The customer Host and Plugin were built from the attached reproduction source, retargeted tonet10.0so the same binaries could run on both versions.10.0.14-dev)11.0.0-dev, exact backport head)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
NullReferenceExceptionfailures inSegmentedArrayBuilder<T>.Expandthrough 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.