Fix cs_loader memory leak on failed script load.#630
Closed
ROKUMATE wants to merge 4 commits intometacall:developfrom
Closed
Fix cs_loader memory leak on failed script load.#630ROKUMATE wants to merge 4 commits intometacall:developfrom
ROKUMATE wants to merge 4 commits intometacall:developfrom
Conversation
Contributor
Author
Built MetaCall with ASAN enabled inside Docker (.docker-compose.sh test-address-sanitizer), which compiles everything with -fsanitizer=address and runs the full test suite. ASAN instruments every memory allocation at runtime and reports leaks at process exit. metacall-cs-test — the test that loads and executes C# scripts through the cs_loader — passed with no leak report, confirming the 53 MB leak from the original issue is gone. |
968fdaf to
e5364ab
Compare
Member
Member
Contributor
Author
|
looking into it 👍🏼 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
When
LoadFromSourceFunctionsin the C# loader encountered a compilation error, it returnedfalseimmediately without releasing theCSharpCompilationobject or itsMetadataReferencearray. EachMetadataReference.CreateFromFileholds a nativeSystem.Reflection.MetadataPE reader backing store. Summed across all runtime DLLs this produced the ~53 MB leak reported by AddressSanitizer.Changes:
LoaderBase.cs: null outreferencesandcompilationon every failure path and force a GC cycle before returning, so the native backing stores are freed before process exit.LoaderV2.cs: addCollectibleAssemblyLoadContextand switchMakeAssemblyto usealc.LoadFromStream()instead ofAssembly.Load(byte[]). This scopes compiled assemblies to a collectible context so they can be unloaded by the GC rather than being permanently rooted in the default context.lsan.supp: update comment to reflect that the Roslyn-induced leak is fixed at source; theSystem.Private.CoreLib.dllsuppression now covers only intentional CoreCLR static-lifetime allocations.Fixes #378
Type of change
Checklist:
make testorctest -VV -R <test-name>)../docker-compose.sh test &> outputand attached the output.OPTION_BUILD_ADDRESS_SANITIZERor./docker-compose.sh test-address-sanitizer &> outputandOPTION_TEST_MEMORYCHECK.OPTION_BUILD_THREAD_SANITIZERor./docker-compose.sh test-thread-sanitizer &> output.Helgrindin case my code works with threading.make clang-formatin order to format my code and my code follows the style guidelines.