Skip to content

Prevent inlining of the helper in the FileStream strategy finalization tests - #136726

Merged
JulieLeeMSFT merged 1 commit into
dotnet:mainfrom
JulieLeeMSFT:fix-133121-filestream-dispose-noinlining
Nov 2, 2026
Merged

JulieLeeMSFT merged 1 commit into
dotnet:mainfrom
JulieLeeMSFT:fix-133121-filestream-dispose-noinlining

Conversation

@JulieLeeMSFT

Copy link
Copy Markdown
Member

Mark EnsureFileStreamIsNotRooted in FileStream_Dispose.VerifyStrategyFinalization as NoInlining.

VerifyStrategyFinalization relies on the helper running in its own frame, so that nothing in the frame that calls GC.Collect() can reference the FileStream or its strategy. Without NoInlining the JIT is free to inline the helper, and dead references can then stay reachable from the caller's frame:

  • JitStress=2 selects (by method hash) the async root VerifyStrategyFinalization for the async inlining stress, which inlines EnsureFileStreamIsNotRooted into it.
  • With register allocator stress (JitStressRegs), the inlined value is spilled into a GC spill temp. The JIT reports GC spill temps as untracked stack slots, which are live for the whole method, so the object is still reachable at GC.Collect() and the weak reference survives. Object lifetimes are not guaranteed, so this is not a JIT bug.

This is the failure in #133121 and #136604 (Assert.False() Failure at FileStream/Dispose.cs:487) on the x86, arm and arm64 jitstress2_jitstressregs* legs.

Evidence, on x86 (Checked CoreCLR, Release libraries):

  • JitStress=2 alone, JitStressRegs alone and JitStress=1 with JitStressRegs=0x80 pass. JitStress=2 with JitStressRegs=0x80 or 2 fails. Adding DOTNET_JitStressModeNamesNot=STRESS_ASYNC_INLINE makes it pass again.
  • JitDisasm shows [INLINED: random accept ASYNC] ...EnsureFileStreamIsNotRooted. The GC info at the GC.Collect() call has an extra untracked TEMP_nn ref slot in the failing builds and none in the passing build.
  • An experimental JIT that zeroed GC spill temps after reload made the unmodified test pass in all failing configurations. That experiment is not part of this change.

Validation, all with /p:TargetArchitecture=x86 and the x86 testhost, running -class System.IO.Tests.FileStream_Dispose (30 tests):

Environment Before After
DOTNET_TieredCompilation=0 DOTNET_JitStress=2 DOTNET_JitStressRegs=0x80 14 failed 0 failed
DOTNET_TieredCompilation=0 DOTNET_JitStress=2 DOTNET_JitStressRegs=2 11 failed 0 failed
DOTNET_TieredCompilation=0 DOTNET_JitStressRegs=0x80 DOTNET_JitStressAsyncInlining=1 DOTNET_JitStressAsyncInliningPct=100 6 failed 0 failed
DOTNET_TieredCompilation=0 DOTNET_JitStressRegs=2 DOTNET_JitStressAsyncInlining=1 DOTNET_JitStressAsyncInliningPct=100 11 failed 0 failed
JitStressRegs = 1, 2, 3, 4, 8, 0x10, 0x80, 0x1000, 0x2000, each with JitStress=2 and with the JitStressAsyncInlining knobs (18 runs) 0 failed
DOTNET_TieredCompilation=1 DOTNET_JitStress=2 0 failed 0 failed

Build and test commands: build.cmd clr+libs -a x86 -rc checked -lc release, build.cmd -subset libs.pretest -a x86 -rc checked -lc release, then dotnet.cmd build src\libraries\System.Runtime\tests\System.IO.FileSystem.Tests\System.IO.FileSystem.Tests.csproj -c Release /p:TargetArchitecture=x86 /t:Test "/p:XUnitOptions=-class System.IO.Tests.FileStream_Dispose" (30 passed), and the full System.IO.FileSystem.Tests assembly with default settings (9,877 tests; the 25 failures are privilege-related Junction, mount volume and console input tests and none are in FileStream).

Not verified:

Contributes to #133121 and #136604.

Note

This pull request was created with the help of GitHub Copilot (AI-generated content). Please review it carefully.

…n tests

VerifyStrategyFinalization relies on EnsureFileStreamIsNotRooted running in its
own frame so that nothing in the frame of GC.Collect() can reference the
FileStream or its strategy. Without NoInlining the JIT is free to inline the
helper (async inlining stress picks it under JitStress=2), and then dead
references can stay reachable from the caller's frame, for example in GC spill
temps, which are reported to the GC as live for the entire method. That makes
the weak reference survive GC.Collect() and fails the tests on the x86, arm and
arm64 jitstress2 legs that also stress the register allocator.

Mark the helper NoInlining.

Related to dotnet#133121 and dotnet#136604.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: ea5600e2-9176-4e98-9306-8487dd7cbb5f
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
12 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: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the test-only change that preserves a separate helper stack frame during finalization checks.

No open issues 🎉

What changed in this PR

This PR stabilizes FileStream strategy finalization tests by preventing helper inlining from keeping objects reachable during garbage collection. It targets stress-test failures associated with #133121 and #136604 without changing production behavior.

Changes:

  • Adds MethodImplOptions.NoInlining to EnsureFileStreamIsNotRooted and imports its namespace.
  • Expands the comment to explain why the helper must retain a separate stack frame.
File Description
src/​libraries/​System.Runtime/​tests/​System.IO.FileSystem.Tests/​FileStream/​Dispose.cs Prevents helper inlining and documents the object-lifetime constraint in finalization tests.

Review effort: Balanced

@JulieLeeMSFT

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr libraries-jitstress, runtime-coreclr libraries-jitstress2-jitstressregs

Note

This comment was posted with the help of GitHub Copilot (AI-generated content).

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@JulieLeeMSFT

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr libraries-jitstress2-jitstressregs

Note

This comment was posted with the help of GitHub Copilot (AI-generated content).

@azure-pipelines

Copy link
Copy Markdown
No pipelines are associated with this pull request.

@JulieLeeMSFT

Copy link
Copy Markdown
Member Author

/azp list

Note

This comment was posted with the help of GitHub Copilot (AI-generated content).

@azure-pipelines

Copy link
Copy Markdown
Command 'list

' is not supported by Azure Pipelines.


Supported commands
  • help:
    • Get descriptions, examples and documentation about supported commands
    • Example: help "command_name"
  • list:
    • List all pipelines for this repository using a comment.
    • Example: "list"
  • run:
    • Run all pipelines or specific pipelines for this repository using a comment. Use this command by itself to trigger all related pipelines, or specify specific pipelines to run.
    • Example: "run" or "run pipeline_name, pipeline_name, pipeline_name"
  • where:
    • Report back the Azure DevOps orgs that are related to this repository and org
    • Example: "where"

See additional documentation.

@JulieLeeMSFT

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr libraries-jitstress2-jitstressregs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@JulieLeeMSFT

Copy link
Copy Markdown
Member Author

Note

The /azp run comments above were posted with the help of GitHub Copilot (AI-generated content). They start the two stress pipelines in which System.IO.Tests.FileStream_Dispose.*SuppressesStrategyFinalization* failed (#133121, #136604): runtime-coreclr libraries-jitstress (x86 jitstress2_tiered) and runtime-coreclr libraries-jitstress2-jitstressregs (x86 jitstress2_jitstressregs2 and 0x80, arm/arm64 jitstress2_jitstressregs3). The /azp list reply and the "No pipelines are associated" reply are artifacts of an appended note being parsed as part of the command; the pipelines are running.

@JulieLeeMSFT JulieLeeMSFT added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI and removed area-System.IO labels Oct 30, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for providing the fix @JulieLeeMSFT !

@JulieLeeMSFT
JulieLeeMSFT merged commit c9c97da into dotnet:main Nov 2, 2026
111 checks passed
@JulieLeeMSFT
JulieLeeMSFT deleted the fix-133121-filestream-dispose-noinlining branch November 3, 2026 20:01
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants