Repository navigation
Description
While investigating CI failures for #34677, I ran across some unexpected behavior with AssemblyLoadContext.
The issue is that an Assembly loaded in an ALC do not root the ALC, so the ALC can be garbage collected and finalized prematurely.
This seems like unexpected behavior from an ALC. I would expect the ALC to be rooted by any assemblies loaded within it and it wouldn't be collected until either I called Unload or all assemblies were also unrooted.
Further, my concern is that even if this is expected, it's not a very diagnosable crash. In a release build it results in a nullref during an assembly operation, which is translated to a FileLoadException. I am worried that users who aren't developers on the runtime team would have trouble diagnosing the root cause.
Repro Steps
Here is a sample app that shows the behavior (run with COMPLUS_TieredCompilation=0 because it depends on the Main method being optimized:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using System.IO;
using System.Runtime.Loader;
namespace UnloadApp
{
class Program
{
public static void GCThreadProc()
{
while (true)
{
Thread.Sleep(TimeSpan.FromMilliseconds(10));
GC.Collect(2, GCCollectionMode.Forced, true, true);
GC.WaitForPendingFinalizers();
}
}
static void Main(string[] args)
{
string tiered = Environment.GetEnvironmentVariable("COMPLUS_TieredCompilation");
if (tiered == null || tiered != "0")
{
Console.WriteLine("You must disable tiered compilation for this repro to work");
}
Thread gcThread = new Thread(new ThreadStart(GCThreadProc))
{
IsBackground = true
};
gcThread.Start();
LoadCollectibleAssembly();
Console.WriteLine("Done!");
}
private static void LoadCollectibleAssembly()
{
var collectibleContext = new AssemblyLoadContext("Collectible", true);
Thread.Sleep(TimeSpan.FromSeconds(3));
var asmDir = Path.GetDirectoryName(typeof(Program).Assembly.Location);
var dynamicLibrary = collectibleContext.LoadFromAssemblyPath(Path.Combine( asmDir, "unloadlibrary") );
var testType = dynamicLibrary.GetType("UnloadLibrary.TestClass");
object instance = Activator.CreateInstance(testType);
Console.WriteLine(instance.GetHashCode());
}
}
}
The crash can be prevented by adding code that roots the ALC at the end of the LoadCollectibleAssembly method, after the type is loaded.
The collectible code is unimportant, but this is what I am using for it:
using System;
using System.Collections.Generic;
namespace UnloadLibrary
{
public class TestClass : MarshalByRefObject
{
static string staticString = "A static string";
string[] instanceStrings;
static int s_count;
static List<TestClass> instances = new List<TestClass>();
public TestClass()
{
instanceStrings = new string[100];
for (int i = 0; i < instanceStrings.Length; i++)
{
instanceStrings[i] = staticString + (++s_count);
}
Console.WriteLine("Class1 constructed");
instances.Add(this);
}
}
}
The code that gets gets UnloadLibrary.TestClass will crash 100% of the time in a checked/debug build and occasionally crash on a release build (running in a loop gets a crash within a minute usually). The crash is due to the ALC being collected and finalized before the GetType/Activator.CreateInstance call.
In a release build it will result in a FileLoadException:
2:009> !pe -nested
Exception object: 000001798000f2f0
Exception type: System.IO.FileLoadException
Message: Could not load file or assembly 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Invalid pointer (0x80004003 (E_POINTER))
InnerException: System.NullReferenceException, Use !PrintException 000001798000CEF8 to see more.
StackTrace (generated):
<none>
StackTraceString: <none>
HResult: 80004003
2:009> !PrintException /d 000001798000CEF8
Exception object: 000001798000cef8
Exception type: System.NullReferenceException
Message: Object reference not set to an instance of an object.
InnerException: <none>
StackTrace (generated):
SP IP Function
0000003B3B3B68B0 00007FF8C7CADFD0 System_Private_CoreLib!System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr, System.Reflection.AssemblyName)+0x20
StackTraceString: <none>
With this stack:
2:009> k
# Child-SP RetAddr Call Site
00 0000003b`3b3bd2b0 00007ff8`dd4c79e1 KERNELBASE!RaiseException+0x69
01 0000003b`3b3bd390 00007ff8`dd5bd9b0 CoreCLR!RaiseTheExceptionInternalOnly+0x28d [C:\git\runtime\src\coreclr\src\vm\excep.cpp @ 2952]
02 0000003b`3b3bd4a0 00007ff8`dd593faa CoreCLR!UnwindAndContinueRethrowHelperAfterCatch+0x38 [C:\git\runtime\src\coreclr\src\vm\excep.cpp @ 8190]
03 0000003b`3b3bd4e0 00007ff8`c7b441ed CoreCLR!AssemblyNative::GetType+0x8e8ba [C:\git\runtime\src\coreclr\src\vm\assemblynative.cpp @ 476]
04 0000003b`3b3bd5b0 00007ff8`c7ced4fd System.Reflection.RuntimeAssembly.GetType(System.Runtime.CompilerServices.QCallAssembly, System.String, Boolean, Boolean, System.Runtime.CompilerServices.ObjectHandleOnStack, System.Runtime.CompilerServices.ObjectHandleOnStack, System.Runtime.CompilerServices.ObjectHandleOnStack)+0xbd
05 0000003b`3b3bd6c0 00007ff8`c7ce0b54 System_Private_CoreLib!System.Reflection.RuntimeAssembly.GetType(System.String, Boolean, Boolean)+0x8d [C:\git\runtime\src\coreclr\src\System.Private.CoreLib\src\System\Reflection\RuntimeAssembly.cs @ 182]
06 0000003b`3b3bd760 00007ff8`7d985a09 System_Private_CoreLib!System.Reflection.Assembly.GetType(System.String)+0x14 [C:\git\runtime\src\libraries\System.Private.CoreLib\src\System\Reflection\Assembly.cs @ 95]
07 0000003b`3b3bd790 00007ff8`7d980d27 unloadapp!UnloadApp.Program.LoadCollectibleAssembly()+0xb9*** WARNING: Unable to verify checksum for C:\git\tempprofiler\repro\unloadapp.dll
[C:\git\tempprofiler\sampleapp\Program.cs @ 42]
08 0000003b`3b3bd7c0 00007ff8`dd514313 unloadapp!UnloadApp.Program.Main(System.String[])+0x77 [C:\git\tempprofiler\sampleapp\Program.cs @ 33]
09 0000003b`3b3bd800 00007ff8`dd400aa3 CoreCLR!CallDescrWorkerInternal+0x83 [C:\git\runtime\src\coreclr\src\vm\amd64\CallDescrWorkerAMD64.asm @ 101]
0a 0000003b`3b3bd840 00007ff8`dd4744a7 CoreCLR!MethodDescCallSite::CallTargetWorker+0x263 [C:\git\runtime\src\coreclr\src\vm\callhelpers.cpp @ 554]
0b (Inline Function) --------`-------- CoreCLR!MethodDescCallSite::Call+0xb [C:\git\runtime\src\coreclr\src\vm\callhelpers.h @ 459]
0c 0000003b`3b3bd980 00007ff8`dd4742f2 CoreCLR!RunMainInternal+0x11f [C:\git\runtime\src\coreclr\src\vm\assembly.cpp @ 1488]
0d 0000003b`3b3bdab0 00007ff8`dd474031 CoreCLR!RunMain+0xd2 [C:\git\runtime\src\coreclr\src\vm\assembly.cpp @ 1559]
0e 0000003b`3b3bdb60 00007ff8`dd474944 CoreCLR!Assembly::ExecuteMainMethod+0x1cd [C:\git\runtime\src\coreclr\src\vm\assembly.cpp @ 1671]
0f 0000003b`3b3bdf00 00007ff8`dd4eeaaf CoreCLR!CorHost2::ExecuteAssembly+0x264 [C:\git\runtime\src\coreclr\src\vm\corhost.cpp @ 389]
10 0000003b`3b3be060 00007ff6`badb2f3a CoreCLR!coreclr_execute_assembly+0xef [C:\git\runtime\src\coreclr\src\dlls\mscoree\unixinterface.cpp @ 407]
11 (Inline Function) --------`-------- CoreRun!HostEnvironment::ExecuteAssembly+0x32 [C:\git\runtime\src\coreclr\src\hosts\corerun\corerun.cpp @ 354]
12 0000003b`3b3be100 00007ff6`badb3b1e CoreRun!ExecuteAssembly+0x3d6 [C:\git\runtime\src\coreclr\src\hosts\corerun\corerun.cpp @ 644]
13 0000003b`3b3be590 00007ff6`badb3fe4 CoreRun!TryRun+0x9de [C:\git\runtime\src\coreclr\src\hosts\corerun\corerun.cpp @ 791]
14 0000003b`3b3bf8f0 00007ff6`badbb668 CoreRun!wmain+0x13c [C:\git\runtime\src\coreclr\src\hosts\corerun\corerun.cpp @ 870]
15 (Inline Function) --------`-------- CoreRun!invoke_main+0x22 [d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 90]
16 0000003b`3b3bf960 00007ff9`20fd7bd4 CoreRun!__scrt_common_main_seh+0x10c [d:\agent\_work\4\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl @ 288]
17 0000003b`3b3bf9a0 00007ff9`22d2ced1 KERNEL32!BaseThreadInitThunk+0x14
18 0000003b`3b3bf9d0 00000000`00000000 ntdll!RtlUserThreadStart+0x21
In a checked build it will trigger an assert in the following callstack:
Assert failure(PID 15324 [0x00003bdc], Thread: 12488 [0x30c8]): !CREATE_CHECK_STRING(!"Detected use of a corrupted OBJE CTREF. Possible GC hole.")
00 00000026`c9d6edb0 00007ff8`b576ae82 CoreCLR!Object::GetGCSafeMethodTable+0x2b [C:\git\runtime\src\coreclr\src\vm\object.h @ 449]
01 00000026`c9d6ede0 00007ff8`b576ad76 CoreCLR!Object::ValidateInner+0x92 [C:\git\runtime\src\coreclr\src\vm\object.cpp @ 553]
02 00000026`c9d6f020 00007ff8`b5762fc3 CoreCLR!Object::Validate+0x136 [C:\git\runtime\src\coreclr\src\vm\object.cpp @ 533]
03 00000026`c9d6f0a0 00007ff8`b52f517b CoreCLR!OBJECTREF::OBJECTREF+0xd3 [C:\git\runtime\src\coreclr\src\vm\object.cpp @ 1122]
04 00000026`c9d6f110 00007ff8`b5c21029 CoreCLR!ObjectFromHandle+0x5b [C:\git\runtime\src\coreclr\src\vm\gchandleutilities.h @ 45]
05 00000026`c9d6f160 00007ff8`d0668b67 CoreCLR!MarshalNative::GCHandleInternalGet+0xd9 [C:\git\runtime\src\coreclr\src\vm\marshalnative.cpp @ 610]
06 00000026`c9d6f3a0 00007ff8`d06659e2 System_Private_CoreLib!System.Runtime.InteropServices.GCHandle.get_Target()+0x47 [C:\git\runtime\src\libraries\System.Private.CoreLib\src\System\Runtime\InteropServices\GCHandle.cs @ 94]
07 00000026`c9d6f3f0 00007ff8`b5c09103 System_Private_CoreLib!System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr, System.Reflection.AssemblyName)+0x42 [C:\git\runtime\src\libraries\System.Private.CoreLib\src\System\Runtime\Loader\AssemblyLoadContext.cs @ 560]
08 00000026`c9d6f450 00007ff8`b57f5c92 CoreCLR!CallDescrWorkerInternal+0x83 [C:\git\runtime\src\coreclr\src\vm\amd64\CallDescrWorkerAMD64.asm @ 101]
09 00000026`c9d6f490 00007ff8`b57f6910 CoreCLR!CallDescrWorkerWithHandler+0x142 [C:\git\runtime\src\coreclr\src\vm\callhelpers.cpp @ 73]
0a 00000026`c9d6f4f0 00007ff8`b5443d92 CoreCLR!MethodDescCallSite::CallTargetWorker+0xc70 [C:\git\runtime\src\coreclr\src\vm\callhelpers.cpp @ 549]
0b 00000026`c9d6fd50 00007ff8`b568cb15 CoreCLR!MethodDescCallSite::Call_RetOBJECTREF+0x42 [C:\git\runtime\src\coreclr\src\vm\callhelpers.h @ 462]
0c 00000026`c9d6fda0 00007ff8`b5e5a3bd CoreCLR!RuntimeInvokeHostAssemblyResolver+0x505 [C:\git\runtime\src\coreclr\src\vm\appdomain.cpp @ 6219]
0d 00000026`c9d70870 00007ff8`b5e4c215 CoreCLR!BINDER_SPACE::AssemblyBinder::BindUsingHostAssemblyResolver+0x8d [C:\git\runtime\src\coreclr\src\binder\assemblybinder.cpp @ 1264]
0e 00000026`c9d708e0 00007ff8`b5e60469 CoreCLR!CLRPrivBinderAssemblyLoadContext::BindAssemblyByName+0x2c5 [C:\git\runtime\src\coreclr\src\binder\clrprivbinderassemblyloadcontext.cpp @ 88]
0f 00000026`c9d70a10 00007ff8`b577c8c8 CoreCLR!BINDER_SPACE::Assembly::BindAssemblyByName+0x79 [C:\git\runtime\src\coreclr\src\binder\assembly.cpp @ 188]
10 00000026`c9d70a50 00007ff8`b5654f82 CoreCLR!AssemblySpec::Bind+0xa58 [C:\git\runtime\src\coreclr\src\vm\coreassemblyspec.cpp @ 147]
11 00000026`c9d71980 00007ff8`b5430842 CoreCLR!AppDomain::BindAssemblySpec+0xdd2 [C:\git\runtime\src\coreclr\src\vm\appdomain.cpp @ 4843]
12 00000026`c9d72c70 00007ff8`b55def2b CoreCLR!PEFile::LoadAssembly+0x642 [C:\git\runtime\src\coreclr\src\vm\pefile.cpp @ 1545]
13 00000026`c9d73080 00007ff8`b57e5d1e CoreCLR!Module::LoadAssembly+0x70b [C:\git\runtime\src\coreclr\src\vm\ceeload.cpp @ 4989]
14 00000026`c9d73550 00007ff8`b54f68c5 CoreCLR!Assembly::FindModuleByTypeRef+0xfce [C:\git\runtime\src\coreclr\src\vm\assembly.cpp @ 1101]
15 00000026`c9d73c70 00007ff8`b54f0d25 CoreCLR!ClassLoader::LoadTypeDefOrRefThrowing+0xb25 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 2675]
16 00000026`c9d742a0 00007ff8`b54f01e2 CoreCLR!ClassLoader::LoadApproxTypeThrowing+0xa25 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3080]
17 00000026`c9d74770 00007ff8`b5af375b CoreCLR!ClassLoader::LoadApproxParentThrowing+0x2e2 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3134]
18 00000026`c9d74930 00007ff8`b54e4c74 CoreCLR!ClassLoader::CreateTypeHandleForTypeDefThrowing+0x9cb [C:\git\runtime\src\coreclr\src\vm\methodtablebuilder.cpp @ 12075]
19 00000026`c9d775b0 00007ff8`b54e63e2 CoreCLR!ClassLoader::CreateTypeHandleForTypeKey+0x4b4 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3261]
1a 00000026`c9d77c00 00007ff8`b54f9bfa CoreCLR!ClassLoader::DoIncrementalLoad+0x622 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3189]
1b 00000026`c9d78070 00007ff8`b54f855a CoreCLR!ClassLoader::LoadTypeHandleForTypeKey_Body+0xdda [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3944]
1c 00000026`c9d786e0 00007ff8`b54f7e64 CoreCLR!ClassLoader::LoadTypeHandleForTypeKey+0x57a [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 3670]
1d 00000026`c9d78a60 00007ff8`b54fb5fd CoreCLR!ClassLoader::LoadTypeDefThrowing+0x1124 [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 2565]
1e 00000026`c9d79250 00007ff8`b5a9f6ce CoreCLR!ClassLoader::LoadTypeHandleThrowing+0xbbd [C:\git\runtime\src\coreclr\src\vm\clsload.cpp @ 1804]
1f 00000026`c9d79850 00007ff8`b5a9f468 CoreCLR!TypeName::GetTypeHaveAssemblyHelper+0x24e [C:\git\runtime\src\coreclr\src\vm\typeparse.cpp @ 1413]
20 00000026`c9d79f70 00007ff8`b5aa0cfc CoreCLR!TypeName::GetTypeHaveAssembly+0x68 [C:\git\runtime\src\coreclr\src\vm\typeparse.h @ 414]
21 00000026`c9d79fc0 00007ff8`b5a9fe73 CoreCLR!TypeName::GetTypeWorker+0x69c [C:\git\runtime\src\coreclr\src\vm\typeparse.cpp @ 1211]
22 00000026`c9d7af20 00007ff8`b5c4444a CoreCLR!TypeName::GetTypeManaged+0x4d3 [C:\git\runtime\src\coreclr\src\vm\typeparse.cpp @ 958]
23 00000026`c9d7c1c0 00007ff8`d03c1b0d CoreCLR!AssemblyNative::GetType+0x47a [C:\git\runtime\src\coreclr\src\vm\assemblynative.cpp @ 468]
24 00000026`c9d7c5f0 00007ff8`d06e364e System.Reflection.RuntimeAssembly.GetType(System.Runtime.CompilerServices.QCallAssembly, System.String, Boolean, Boolean, System.Runtime.CompilerServices.ObjectHandleOnStack, System.Runtime.CompilerServices.ObjectHandleOnStack, System.Runtime.CompilerServices.ObjectHandleOnStack)+0xbd
25 00000026`c9d7c700 00007ff8`d06cc644 System_Private_CoreLib!System.Reflection.RuntimeAssembly.GetType(System.String, Boolean, Boolean)+0x15e [C:\git\runtime\src\coreclr\src\System.Private.CoreLib\src\System\Reflection\RuntimeAssembly.cs @ 173]
26 00000026`c9d7c7e0 00007ff8`56b59e51 System_Private_CoreLib!System.Reflection.Assembly.GetType(System.String)+0x34 [C:\git\runtime\src\libraries\System.Private.CoreLib\src\System\Reflection\Assembly.cs @ 95]
27 00000026`c9d7c820 00007ff8`56b5765f unloadapp!UnloadApp.Program.LoadCollectibleAssembly()+0xb1*** WARNING: Unable to verify checksum for C:\git\tempprofiler\repro\unloadapp.dll
[C:\git\tempprofiler\sampleapp\Program.cs @ 48]
Activity
davmason commentedon Apr 10, 2020
cc @janvorli
janvorli commentedon Apr 14, 2020
I'll look into it.
janvorli commentedon Apr 15, 2020
@davmason I am unable to repro the problem. I've tried it with a very recent .NET Core 5.0 SDK (5.0.0-preview.4.20213.10) and both of your repro apps. I've published the app and replaced the runtime with a debug build from the same commit hash. I've tried even with COMPlus_GCStress=3.
janvorli commentedon Apr 16, 2020
@davmason has shared repro binaries with me and I was finally able to repro the issue. I understand what's going on now:
I am looking into a reasonable way to fix that.