Skip to content

Fix native memory leaks with runtime-async methods - #137193

Merged
AaronRobinsonMSFT merged 14 commits into
dotnet:mainfrom
grzonu:fix/runtime-async-native-leaks
Nov 11, 2026
Merged

AaronRobinsonMSFT merged 14 commits into
dotnet:mainfrom
grzonu:fix/runtime-async-native-leaks

Conversation

@grzonu

@grzonu grzonu commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #137192

Two native (malloc) leaks around runtime-async methods. Together they leak ~300 KiB per load/unload cycle of an assembly with 300 runtime-async methods in a collectible AssemblyLoadContext, so native memory grows linearly with the number of cycles.

1. Free async and rich debug info arrays in ~CEECodeGenInfo

reportRichMappings and reportAsyncDebugInfo give the EE ownership of arrays the JIT allocated through allocateArray. CompressDebugInfo copies them into the loader heap, but only ResetForJitRetry freed the originals. Every compile of a runtime-async method with suspension points leaked its AsyncSuspensionPoint[] and AsyncContinuationVarInfo[] (in every app, on every tier), and with DOTNET_RichDebugInfo=1 every compile leaked its inline tree and rich mappings. The destructor now frees them like ResetForJitRetry does.

2. Free compile-time state of IL stubs in collectible LoaderAllocators on unload

CEEJitInfo::getAsyncResumptionStub creates an IL_STUB_AsyncResume stub through ILStubCache::CreateAndLinkNewILStubMethodDesc. Its ILStubResolver keeps the compile-time state (IL, locals sig, TokenLookupMap, target sig, EH section) on the native heap until the stub is compiled. A method that never suspends never resumes, so its stub is never compiled. On unload the resolver disappears with the loader heap and that state leaks, ~0.9 KiB per method per cycle.

CreateAndLinkNewILStubMethodDesc now records the resolvers of stubs created in a collectible LoaderAllocator in that allocator's ILStubCache, and LoaderAllocator::Terminate calls FreeCompileTimeState on them before the loader heaps are destroyed. FreeCompileTimeState is a no-op for stubs that were compiled. Non-collectible allocators are unchanged. The same applies to any other stub created through CreateAndLinkNewILStubMethodDesc in a collectible allocator that is never called. Stubs created through ILStubCache::GetStubMethodDesc (interop) are not covered here.

There is no new test: the leak is only visible in native heap usage, which the test suite cannot check reliably. Existing tests that unload collectible ALCs reach the new code in LoaderAllocator::Terminate once their LoaderAllocator is deleted.

Validation

The repro from the issue (300 runtime-async methods, collectible ALC, glibc mallinfo2 in-use KiB after each cycle), on 11.0.0-rc.3 vs. a libcoreclr.so built from the same source (dotnet/dotnet v11.0.0-rc.3.26522.148, src/runtime) with these two commits. "Suspend" is the same repro with each method awaiting a TaskCompletionSource task that is completed after the call, so the resumption stubs run and get compiled:

methods never suspend methods suspend and resume
11.0.0-rc.3 +300 KiB / cycle +29 KiB / cycle
with this PR bounded, 1.81 MiB after 100 cycles bounded, 1.80 MiB after 100 cycles

I built and ran this on linux-x64 Release only, not Debug/Checked. The affected code paths are the same on main as in rc.3.

🤖 Generated with Claude Code

@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Nov 9, 2026
@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.

@grzonu

grzonu commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@dotnet-policy-service agree

@grzonu
grzonu force-pushed the fix/runtime-async-native-leaks branch from 25ba122 to 868f041 Compare November 9, 2026 14:19
Comment thread src/coreclr/vm/ilstubcache.cpp Resolved
Comment thread src/coreclr/vm/ilstubcache.cpp Resolved
Comment thread src/coreclr/vm/ilstubcache.h Resolved
Comment thread src/coreclr/vm/ilstubcache.h Resolved
Comment thread src/coreclr/vm/jitinterface.h Resolved
Comment thread src/coreclr/vm/loaderallocator.cpp Resolved
Comment thread src/coreclr/vm/ilstubcache.cpp Resolved
grzonu and others added 4 commits November 10, 2026 00:30
The JIT hands the EE ownership of the inline tree, rich offset mappings and
async suspension point / continuation variable arrays through
reportRichMappings and reportAsyncDebugInfo. CompressDebugInfo copies them into
the loader heap, but only ResetForJitRetry freed the originals, so every
compiled runtime-async method leaked its async debug info arrays (and every
method its rich debug info when StoreRichDebugInfo is on), including on each
tier-up.

Free them in the destructor as well, matching ResetForJitRetry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n unload

IL stubs created through ILStubCache::CreateAndLinkNewILStubMethodDesc keep
their compile-time state (generated IL, token lookup map, stub target
signature, EH section) on the native heap until the stub is compiled. Stubs
that are never compiled, such as the runtime-async resumption stubs of methods
that never resume, kept that memory after their collectible LoaderAllocator was
unloaded, because the resolver itself lives on the loader heap that is
released wholesale.

Remember the resolvers of stubs created through CreateAndLinkNewILStubMethodDesc
in a collectible LoaderAllocator in its ILStubCache and free their compile-time
state in LoaderAllocator::Terminate, before the loader heaps are destroyed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Free the arrays in ~CEECodeGenInfo through ResetForJitRetry instead of
  duplicating its clean-up.
- Iterate the collectible stub resolvers with SArray::Iterator and clear the
  array once their compile-time state is freed.
- Shorten the comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… after FinalizeILStub

SArray::Begin and End are declared THROWS, so the contract violation has to
cover the loop in FreeCollectibleStubsCompileTimeState, not only Clear.

Record the resolver only once FinalizeILStub has succeeded. If it throws, the
compile-time state may be left half constructed, and freeing it on unload
could free its buffers a second time; such a stub keeps leaking as before.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@grzonu
grzonu force-pushed the fix/runtime-async-native-leaks branch from 4f44e63 to 10c49c1 Compare November 10, 2026 00:00
grzonu and others added 2 commits November 10, 2026 01:05
…lear

The destructor is the only caller of LoaderAllocator::Terminate, and nothing
reads the stub resolver array after it frees their compile-time state, so the
array no longer needs to be cleared; it is freed with the LoaderAllocator.
Also drop the stale comment saying Terminate is called from AppDomain::Terminate.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/coreclr/vm/ilstubcache.cpp Resolved
grzonu and others added 2 commits November 10, 2026 09:26
SArray::Begin and End go through SBuffer::Begin, which is declared THROWS, so
iterate by index and drop the contract violation. Drop the lock as well: the
method relies on LoaderAllocator::Terminate guaranteeing that nothing uses the
stubs anymore, like the other loader allocator cleanup methods.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Comment thread src/coreclr/vm/ilstubcache.cpp Resolved

@AaronRobinsonMSFT AaronRobinsonMSFT 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.

Thanks.

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

@grzonu Looks like we've got some merge conflicts. Can you please handle them?

…ive-leaks

# Conflicts:
#	src/coreclr/vm/loaderallocator.cpp

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 recorded for the runtime-async memory-leak cleanup.

No open issues 🎉

What changed in this PR

This PR addresses two native-memory leaks in CoreCLR runtime-async methods, including memory retained across collectible AssemblyLoadContext load/unload cycles.

Changes:

  • Reuses ResetForJitRetry in CEECodeGenInfo destruction to release async and rich debug-info arrays.
  • Tracks IL stub resolvers in collectible loader allocators and frees their remaining compile-time state before heap teardown.
  • Restricts LoaderAllocator::Terminate to destructor-driven cleanup.
File Description
src/​coreclr/​vm/​loaderallocator.hpp Moves Terminate out of the public interface and documents destructor-only use.
src/​coreclr/​vm/​loaderallocator.cpp Invokes stub-state cleanup before destroying loader heaps.
src/​coreclr/​vm/​jitinterface.h Reuses existing cleanup logic in the code-generation info destructor.
src/​coreclr/​vm/​ilstubcache.h Declares resolver tracking and collectible-stub cleanup.
src/​coreclr/​vm/​ilstubcache.cpp Registers collectible stub resolvers and releases their compile-time state on unload.

Review effort: Balanced

@grzonu

grzonu commented Nov 11, 2026

Copy link
Copy Markdown
Contributor Author

it's looks like pipeline is hanging

@AaronRobinsonMSFT
AaronRobinsonMSFT enabled auto-merge (squash) November 11, 2026 18:20
@AaronRobinsonMSFT
AaronRobinsonMSFT merged commit ea216b3 into dotnet:main Nov 11, 2026
101 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 12, 2026
@grzonu

grzonu commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

@AaronRobinsonMSFT thanks for the review. Would you be willing to backport this to .NET 11?

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

@AaronRobinsonMSFT thanks for the review. Would you be willing to backport this to .NET 11?

We can try. Since the async work is behind an experimental flag it might not make it, but I will give it a shot.

@AaronRobinsonMSFT

Copy link
Copy Markdown
Member

/backport to release/11.0

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/11.0 (link to workflow run)

@github-actions

Copy link
Copy Markdown
Contributor

@AaronRobinsonMSFT backporting to release/11.0 failed, the patch most likely resulted in conflicts. Please backport manually!

git am output
$ git cherry-pick ea216b361e6585e6e02fd0808b28c4d1312d2e31

Auto-merging src/coreclr/vm/ilstubcache.cpp
Auto-merging src/coreclr/vm/jitinterface.h
CONFLICT (content): Merge conflict in src/coreclr/vm/jitinterface.h
Auto-merging src/coreclr/vm/loaderallocator.cpp
CONFLICT (content): Merge conflict in src/coreclr/vm/loaderallocator.cpp
Auto-merging src/coreclr/vm/loaderallocator.hpp
error: could not apply ea216b361e6... Fix native memory leaks with runtime-async methods (#137193)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"


$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Free async and rich debug info arrays in ~CEECodeGenInfo
Using index info to reconstruct a base tree...
M	src/coreclr/vm/jitinterface.h
Falling back to patching base and 3-way merge...
Auto-merging src/coreclr/vm/jitinterface.h
CONFLICT (content): Merge conflict in src/coreclr/vm/jitinterface.h
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0001 Free async and rich debug info arrays in ~CEECodeGenInfo
Error: The process '/usr/bin/git' failed with exit code 128

Link to workflow output

@jkotas

jkotas commented Nov 12, 2026

Copy link
Copy Markdown
Member

the async work is behind an experimental flag

runtime-async is supported and it is not under experimental flag in .NET 11 (experimental flag removed in #124488)

(It is not on by default in .NET 11.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-VM-coreclr community-contribution Indicates that the PR has been added by a community member runtime-async

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Native memory leaks with runtime-async methods: async debug info arrays, and never-compiled resumption stubs in collectible ALCs

5 participants