Repository navigation
Fix native memory leaks with runtime-async methods - #137193
Conversation
|
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. |
|
Tagging subscribers to this area: @agocke |
|
@dotnet-policy-service agree |
25ba122 to
868f041
Compare
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>
4f44e63 to
10c49c1
Compare
…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>
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>
…eTimeState Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@grzonu Looks like we've got some merge conflicts. Can you please handle them? |
…ive-leaks # Conflicts: # src/coreclr/vm/loaderallocator.cpp
There was a problem hiding this comment.
🟢 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
ResetForJitRetryinCEECodeGenInfodestruction 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::Terminateto 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
|
it's looks like pipeline is hanging |
|
@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. |
|
/backport to release/11.0 |
|
Started backporting to |
|
@AaronRobinsonMSFT backporting to 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 |
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.) |
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
~CEECodeGenInforeportRichMappingsandreportAsyncDebugInfogive the EE ownership of arrays the JIT allocated throughallocateArray.CompressDebugInfocopies them into the loader heap, but onlyResetForJitRetryfreed the originals. Every compile of a runtime-async method with suspension points leaked itsAsyncSuspensionPoint[]andAsyncContinuationVarInfo[](in every app, on every tier), and withDOTNET_RichDebugInfo=1every compile leaked its inline tree and rich mappings. The destructor now frees them likeResetForJitRetrydoes.2. Free compile-time state of IL stubs in collectible LoaderAllocators on unload
CEEJitInfo::getAsyncResumptionStubcreates anIL_STUB_AsyncResumestub throughILStubCache::CreateAndLinkNewILStubMethodDesc. ItsILStubResolverkeeps 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.CreateAndLinkNewILStubMethodDescnow records the resolvers of stubs created in a collectible LoaderAllocator in that allocator'sILStubCache, andLoaderAllocator::TerminatecallsFreeCompileTimeStateon them before the loader heaps are destroyed.FreeCompileTimeStateis a no-op for stubs that were compiled. Non-collectible allocators are unchanged. The same applies to any other stub created throughCreateAndLinkNewILStubMethodDescin a collectible allocator that is never called. Stubs created throughILStubCache::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::Terminateonce their LoaderAllocator is deleted.Validation
The repro from the issue (300 runtime-async methods, collectible ALC, glibc
mallinfo2in-use KiB after each cycle), on 11.0.0-rc.3 vs. alibcoreclr.sobuilt from the same source (dotnet/dotnetv11.0.0-rc.3.26522.148,src/runtime) with these two commits. "Suspend" is the same repro with each method awaiting aTaskCompletionSourcetask that is completed after the call, so the resumption stubs run and get compiled:I built and ran this on linux-x64 Release only, not Debug/Checked. The affected code paths are the same on
mainas in rc.3.🤖 Generated with Claude Code