Skip to content

Remove allocation path for collectible assemblies during stub generation - #137873

Merged
AaronRobinsonMSFT merged 2 commits into
mainfrom
AaronRobinsonMSFT-patch-1
Nov 19, 2026
Merged

AaronRobinsonMSFT merged 2 commits into
mainfrom
AaronRobinsonMSFT-patch-1

Conversation

@AaronRobinsonMSFT

@AaronRobinsonMSFT AaronRobinsonMSFT commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Remove all additional allocations when a collectible assembly is found during IL stub generation.

This is a rework of a suggested fix discovered during a bug fix backport to 11 - #137437.

This is a suggested fix that was discovered during a bug fix backport to 11 - #137437.
@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: @agocke
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.

🟡 Changes recommended

Token-map initialization is not exception-safe: if copying throws after allocating member arrays, constructor unwinding frees them and collectible allocator teardown can free them again.

1 open issue
What changed in this PR

This PR adjusts CoreCLR IL-stub initialization order, following the runtime-async memory-leak backport work in #137437.

Changes:

  • Moves FinalizeILStub after collectible-resolver registration, so the potentially throwing registration occurs before finalization.
File Description
src/​coreclr/​vm/​ilstubcache.cpp Registers collectible stub resolvers before finalizing their IL.

Review effort: Balanced

Comment thread src/coreclr/vm/ilstubcache.cpp Resolved
@AaronRobinsonMSFT
AaronRobinsonMSFT marked this pull request as draft November 18, 2026 23:22
auto-merge was automatically disabled November 18, 2026 23:22

Pull request was converted to draft

Registering a resolver before FinalizeILStub can double free its
compile-time state at unload if finalization throws. Registering after
can leak it if the SArray append throws. Linking through the resolver
can't fail, so finalize first and then link.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 6f9629d0-8acb-4cef-8ecb-aa3784c445a1
@AaronRobinsonMSFT
AaronRobinsonMSFT requested a balanced review from Copilot November 19, 2026 00:44
@AaronRobinsonMSFT AaronRobinsonMSFT changed the title Move FinalizeILStub call after collectible check Remove allocation path for collectible assemblies during stub generation Nov 19, 2026

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 change to allocation-free collectible resolver registration.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@github-project-automation github-project-automation Bot moved this to Done in AppModel Nov 19, 2026
@AaronRobinsonMSFT
AaronRobinsonMSFT restored the AaronRobinsonMSFT-patch-1 branch November 19, 2026 17:05
@AaronRobinsonMSFT
AaronRobinsonMSFT deleted the AaronRobinsonMSFT-patch-1 branch November 19, 2026 17:05
JulieLeeMSFT pushed a commit that referenced this pull request Nov 20, 2026
…37437)

Fixes Issue #137192 <!-- Issue Number -->

main PR #137193 and
#137873

# Description

<!-- Give a brief summary of the issue and how the pull request is
fixing it. -->

Backport the runtime-async native memory leak fixes to `release/11.0`.
Free JIT-owned async/rich debug information after compilation, and free
remaining IL-stub compile-time state before collectible loader heaps are
destroyed.

Resolved the two conflicts without importing main-only PGO cleanup or
removing .NET 11's loader-allocator GC-pressure cleanup. The
added/deleted lines in all five files match the original fix.

# Customer Impact

<!-- What is the impact to customers of not taking this fix? -->
- [x] Customer Reported
- [ ] Found internally

Runtime-async method compilation leaks native debug information,
including on tier-up. Repeated collectible assembly load/unload cycles
also leak compile-time state for resumption stubs that are never
compiled. The original PR measured approximately 300 KiB per cycle for
300 methods; affected long-running applications accumulate native
memory.

# Regression
- [x] Yes
- [ ] No  

<!-- Is this fixing a problem that was introduced in the most recent
release, ie., fixing a regression? -->

Yes, associated with runtime-async in .NET 11 and reproduced on
11.0.0-rc.3. The same issue reproduction without runtime-async does not
show the growth.

# Testing

Validated no steady memory state for the scenario.

# Risk

<!-- Please assess the risk of taking this fix. Provide details backing
up your assessment. -->

Low, but the changes affect native lifetime management. The fix reuses
existing cleanup routines: the qualified
`CEECodeGenInfo::ResetForJitRetry` call frees JIT-owned arrays, and
`ILStubResolver::FreeCompileTimeState` is already a no-op after
compilation. Resolver tracking is limited to collectible allocators and
cleanup runs before their heaps are destroyed. The backport preserves
release-specific teardown and passed Checked runtime assertions in the
focused tests.

---------

Co-authored-by: grzonu <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot-Session: a55d02a1-d641-4165-9e80-339edfabe2b4
Copilot-Session: 6f9629d0-8acb-4cef-8ecb-aa3784c445a1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants