Repository navigation
Description
Description
When using Custom (extending) AssemblyLoadContext to load assemblies
and iterating over the Types of exported types of a loaded assembly
and trying to get interface T from the assembly where T is not within the types of the assembly with System.Type.GetInterface
a random ArgumentNullException is thrown (15% of time in certain cases)
System.ArgumentNullException: Invalid handle.
at System.RuntimeTypeHandle._GetUtf8Name(RuntimeType type)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.PopulateInterfaces(Filter filter)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.GetListByName(Char* pName, Int32 cNameLen, Byte* pUtf8Name, Int32 cUtf8Name, MemberListType listType, CacheType cacheType)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.Populate(String name, MemberListType listType, CacheType cacheType)
at System.RuntimeType.RuntimeTypeCache.MemberInfoCache`1.GetMemberList(MemberListType listType, String name, CacheType cacheType)
at System.RuntimeType.GetInterface(String fullname, Boolean ignoreCase)
This accumulates eventually to a crash after enough iterations:
at System.RuntimeTypeHandle.GetInterfaces(System.RuntimeType)
at System.RuntimeType+RuntimeTypeCache+MemberInfoCache`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].PopulateInterfaces(Filter)
at System.RuntimeType+RuntimeTypeCache+MemberInfoCache`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].GetListByName(Char*, Int32, Byte*, Int32, MemberListType, CacheType)
at System.RuntimeType+RuntimeTypeCache+MemberInfoCache`1[[System.__Canon, System.Private.CoreLib, Version=6.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].Populate(System.String, MemberListType, CacheType)
at System.RuntimeType.GetInterface(System.String, Boolean)
Reproduction Steps
Will append with repro github repo once available, but basically:
foreach (var assemblyType in assembly.GetExportedTypes())
assemblyType.GetInterface(typeof(T).Name, false);
Expected behavior
Should return null if the interface is not found.
Actual behavior
Throws exception 15% of the time
Regression?
No response
Known Workarounds
No response
Configuration
.net 6.0.2
windows/linux (all archs)
Other information
No response
[danmoseley added triple ticks for formatting]
Activity
danmoseley commentedon Mar 25, 2022
It would be great if you could check 5.0 too.
piizei commentedon Mar 28, 2022
Hi @danmoseley
Same result with
"sdk": {
"version": "5.0.406"
}
danmoseley commentedon Mar 28, 2022
@steveharter @vitek-karas should owners of ALC look at this, or reflection?
vitek-karas commentedon Mar 28, 2022
What is the target TFM of the app?
net6.0ornet5.0or something else (or did you try both?).Can you please provide a bit more detail about your
AssemblyLoadContextimplementation? (I don't really know what it means "Extensing" ALC).Is there anything interesting about the assemblies this happens on? For example, are they C++/CLI? Or this happens on exported types which don't resolve (they point to non-existing assembly/type)?
When you say it eventually "crashes", what is that specifically? Native code access violation? Seems you were able to catch that failure under something like WinDbg or similar - could you paste a native callstack?
@danmoseley - I think the fact that it fails is a reflection issue (at least on the surface), I don't think we should ever fail like this as it's unactionable by the caller. Even if the underlying problem is something like a corrupted type metadata: that should fail with an exception pointing to the type in question, and not an ArgumentNullException.
But it's possible the ALCs play a role in getting the system into the weird state.
danmoseley commentedon Mar 28, 2022
@piizei a minimal standalone repro would be very helpful here.
ericstj commentedon Mar 29, 2022
@piizei was able to share a repro offline and @janvorli is having a look.
janvorli commentedon Mar 30, 2022
I have found so far that for unloadability purposes we somehow don't keep interface types alive by types that implement those. So when attempting to enumerate interfaces on the class when the interface is in a separate assembly, we can end up getting a pointer to an already collected type and crash when trying to get some of its properties.
janvorli commentedon Apr 4, 2022
After further investigations, I have found that the actual issue is more general. When there are two collectible assembly load contexts, one of them resolves its assemblies (in its
Loadoverride or in theResolvingevent) using assemblies loaded into the other context, there is no link created between theLoaderAllocatorinstances of the requesting assembly and the resolved assembly. So when there are no instances of objects from the assembly load context that was used to resolve the references, that context is collected, leaving some of the references like the list of interfaces dangling. That results in the crash above.I have a tentative fix that adds proper reference between the
LoaderAllocators, but there still seem the be some loose end(s). It fixes the issues in the repro shared by @piizei, but seems to have yet another unloadability related issue.