Skip to content

Random exceptions when calling System.Type.GetInterface #67144

Closed

Description

@piizei

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

  1. danmoseley commented on Mar 25, 2022

    @danmoseley
    Contributor

    It would be great if you could check 5.0 too.

  2. piizei commented on Mar 28, 2022

    @piizei
    Author

    Hi @danmoseley
    Same result with
    "sdk": {
    "version": "5.0.406"
    }

  3. danmoseley commented on Mar 28, 2022

    @danmoseley
    Contributor

    @steveharter @vitek-karas should owners of ALC look at this, or reflection?

  4. vitek-karas commented on Mar 28, 2022

    @vitek-karas
    Member

    What is the target TFM of the app? net6.0 or net5.0 or something else (or did you try both?).

    Can you please provide a bit more detail about your AssemblyLoadContext implementation? (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.

  5. danmoseley commented on Mar 28, 2022

    @danmoseley
    Contributor

    @piizei a minimal standalone repro would be very helpful here.

  6. self-assigned this
    on Mar 29, 2022
  7. added
    needs-author-action
    on Mar 29, 2022
  8. removed
    needs-author-action
    on Mar 29, 2022
  9. ericstj commented on Mar 29, 2022

    @ericstj
    Member

    @piizei was able to share a repro offline and @janvorli is having a look.

  10. janvorli commented on Mar 30, 2022

    @janvorli
    Member

    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.

  11. janvorli commented on Apr 4, 2022

    @janvorli
    Member

    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 Load override or in the Resolving event) using assemblies loaded into the other context, there is no link created between the LoaderAllocator instances 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.

  12. ghost added
    in-pr
    on Apr 12, 2022
  13. ghost removed
    in-pr
    on Apr 15, 2022
  14. ghost locked as resolved and limited conversation to collaborators on May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions