Skip to content

AssemblyLoadContext .NET Core 3.0 improvements #28491

Closed
dotnet/corefx
#36455

Description

@sdmaclea

Motivations

Concrete Type

There is a desire to simplify System.Runtime.Loader.AssemblyLoadContext usage by making it a concrete type. (from #28397)

The existing class is abstract because the Load() method is not implemented. Recommend adding a default implementation of Load() as a method returning null and make the constructors public. This is a logical default implementation as it implies falling back to the AssemblyLoadContext.Default for first chance to load the assembly.

Naming ALCs

To simplify diagnostics for System.Runtime.Loader.AssemblyLoadContext (from #28395):

  • Add a Name to each ALC making understanding of role of each ALC easier

The name will make it more convenient to understand what is happening in the debugger. It will also make any tracing easier to understand.

  • The Default ALC would be constructed with name = "Default".
  • The Assembly.LoadFile(path) generated ALCs would be constructed with name = "Assembly.LoadFile(path)" .
  • Custom AssemblyLoadContext implementations could provide a logical name.
  • The legacy Custom AssemblyLoadContext using the current protected constructor will not set the name. In these cases the name will default to null.

Set of ALCs & Assemblies

Provide a mechanism to iterate through ALCs and their contents to create a graphical representation. These APIs are intended to help make understanding and diagnosing isolation designs (from #28395).

Add Contexts, and Assemblies properties.

Proposed API changes

namespace System.Runtime.Loader
{
    public partial class AssemblyLoadContext
    {
        // Add a public constructor
        // Allow naming each AssemblyLoadContext
        public AssemblyLoadContext(String name, bool isCollectible = false);

        // Add default Load() implementation
        protected virtual System.Reflection.Assembly Load(System.Reflection.AssemblyName assemblyName)
        {
            return null;
        }
          
        public String Name { get; }
          
        // Override the ToString to return Name, type name, and unique instance id.
        public override string ToString();

        // Set of ALCs
        public static IEnumerable<AssemblyLoadContext> Contexts { get; }

        // Set of Assemblies in the ALC
        public IEnumerable<System.Reflection.Assembly> Assemblies { get; }
    }
}

Open issues

  • Consider renaming Contexts to AllContexts or GetAllContexts

Activity

  1. self-assigned this
    on Jan 23, 2019
  2. sdmaclea commented on Jan 23, 2019

    @sdmaclea
    ContributorAuthor

    #28397 and #28395 have reached consensus. Since they are both affecting ALCs, it seemed best to close the original proposals to create a combined proposal.

    I have marked this ready for review. The API review will help set the final shape.

    /cc @jkotas @vitek-karas @swaroop-sridhar @jeffschwMSFT @terrajobst

  3. jeffschwMSFT commented on Jan 24, 2019

    @jeffschwMSFT
    Member

    @sdmaclea where did we land with the various extension events for missing Native/Managed assemblies? Can you link any other updates to ALC to this issue?

    Double checking that my 'wish list' of having an early load callback (eg. the equivalent of TryLoad) is still not part of any plan, correct?

  4. sdmaclea commented on Jan 24, 2019

    @sdmaclea
    ContributorAuthor

    where did we land with the various extension events for missing Native/Managed assemblies? Can you link any other updates to ALC to this issue?

    #27647 & dotnet/corefx#32015 are approved

    I closed #28398 because I couldn't justify the API. @jkotas wanted to make sure we were not creating another way to break the consistency requirements for the ALC. If we fail to load an assembly by name in one context, the result is cached. We won't even try again. If we succeed in one context, we will always use the cached result. My main argument for having the parent assembly was for Satellite assemblies. Since we are solving this another way for dotnet/coreclr#20979, it seemed irrational to use it as an argument here.

    Double checking that my 'wish list' of having an early load callback (eg. the equivalent of TryLoad) is still not part of any plan, correct?

    From the history @vitek-karas told me, it was in the original design but was removed before .NET Core 1.0 because it caused too many deadlock issues for the Default context. I think if we were to add it, it would probably need to go into a derived class. Or fire an exception if there is a TryLoad event handler on Default.

    We might be able to do it, but I think it could be controversial. I would like @jkotas & @vitek-karas opinions before seriously considering adding TryLoad

  5. jkotas commented on Jan 24, 2019

    @jkotas
    Member

    Correct, we had the Load extensibility point in the original .NET Core 1.0 implementation and it was full of infinite recursion land mines, impossible to use. It was no-brainer to pull that feature. If you think that we need something along these lines, I think we should start with discussing the concrete problems/scenarios that we are trying to solve.

  6. jkoritzinsky commented on Jan 24, 2019

    @jkoritzinsky
    Member

    Is there any particular reason that the Contexts member is an IEnumerator<AssemblyLoadContext> instead of an IEnumerable<AssemblyLoadContext>?

  7. sdmaclea commented on Jan 24, 2019

    @sdmaclea
    ContributorAuthor

    @jkoritzinsky Thanks for pointing out the unintended inconsistency.

    IEnumerable<T> for creating IEnumerator<T>. I think logically it is added because an enumerator is logically an instance which is disposed when it is no longer needed.

    Therefore, I think

    • using properties returning IEnumerator<T>, is semantically incorrect.
    • using properties returning IEnumerable<T> seems clumsy
    • methods returning IEnumerator<T> seems best.

    I will update top post.

    foreach (AssemblyLoadContext alc in AssemblyLoadContext.Contexts.GetEnumerator())

  8. jkoritzinsky commented on Jan 24, 2019

    @jkoritzinsky
    Member

    You don't need the GetEnumerator() call in a foreach statement. You can do foreach (AssemblyLoadContext alc in AssemblyLoadContext.Contexts) if Contexts is an IEnumerable<AssemblyLoadContext>. I at least find it clumsier to use IEnumerator<T> directly. Others may feel differently though.

  9. GSPP commented on Jan 24, 2019

    @GSPP

    Nowhere is IEnumerator ever used directly if not absolutely necessary. This is best for convenience reasons and now also for consistency.

  10. sdmaclea commented on Jan 24, 2019

    @sdmaclea
    ContributorAuthor

    Thanks... I am sure the API review would have fixed this, but it is better to be as close as possible.

    OK back, to properties both returning IEnumerable<T>

  11. sdmaclea commented on Mar 8, 2019

    @sdmaclea
    ContributorAuthor
  12. terrajobst commented on Mar 14, 2019

    @terrajobst
    Contributor

    A few questions:

    1. It seems weird that some ctors will default to collectible being false while the one taking a name defaults to true. That seems inconsistent. Having or not having a name feels logically independent to whether or not you want to be collectible.

    2. You said For legacy usage, these would default to the Custom ALC type name.. How would a customer have observed the name?

    3. I'm not entirely sure I understand why making the type concrete is useful if the default implementation can't resolve assemblies by name at all. Presumably, when I load one assembly by path, the defaut behavior should probe dependencies in at least that assembly's directory, no?

    4. Presumably both enumerators are protected against iteration when we're adding to them, correct? IOW, when I foreach over them and trigger a new context to be created or an assembly to be added, the enumeration will throw InvalidOperationException. I think this makes sense for the non-static Assemblies property but I don't think this is great for the static due to threading. I suspect you want snapshotting semantics instead, so I'd suggest the static property should really be a method that creates a copy of the collection as it exists at the moment.

    5. I'm not sure I understand the behavior of ExplicitContext. Why would I use it?

    Let's spend an hour on this on 3/26. I'll send out invites.

  13. sdmaclea commented on Mar 14, 2019

    @sdmaclea
    ContributorAuthor
    1. It seems weird that some ctors will default to collectible being false while the one taking a name defaults to true. That seems inconsistent. Having or not having a name feels logically independent to whether or not you want to be collectible.

    I don't have a particular bias for the collectible defaulting to true, it may not have been intentional at all. I did wonder why the default was false for other constructors. I will update above to keep consistent with existing defaults.

    1. You said For legacy usage, these would default to the Custom ALC type name.. How would a customer have observed the name?

    Legacy code will not set the name because the API was missing. This is trying to establish a mechanism for creating a reasonable name in these legacy cases.

    In the dotnet/corefx#22273 prototype I think I chose to leave the name null, but ToString() includes the type name. I revised spec to be consistent with the prototype.

    1. I'm not entirely sure I understand why making the type concrete is useful if the default implementation can't resolve assemblies by name at all. Presumably, when I load one assembly by path, the defaut behavior should probe dependencies in at least that assembly's directory, no?

    In many cases returning null is sufficient. When the Load() method returns null, the default ALC gets a chance to load the Assembly, then the Custom ALC Resolving event handler, then the AppDomain AssemblyResolve event handler.

    The only reason to override the Load() method would be to get first shot at loading an assembly.

    1. Presumably both enumerators are protected against iteration when we're adding to them, correct? IOW, when I foreach over them and trigger a new context to be created or an assembly to be added, the enumeration will throw InvalidOperationException. I think this makes sense for the non-static Assemblies property but I don't think this is great for the static due to threading. I suspect you want snapshotting semantics instead, so I'd suggest the static property should really be a method that creates a copy of the collection as it exists at the moment.

    I intend both to be thread safe. With minimal locking. There is a prototype implementation in dotnet/corefx#22273. The Contexts prototype makes a quick copy while locked. The Assemblies one doesn't at the moment, but iterates over the result of AppDomain.GetAssemblies(), which should be snapshotting already.

    1. I'm not sure I understand the behavior of ExplicitContext. Why would I use it?

    The current API in certain circumstances infer the ALC from the immediate caller's assembly and AssemblyLoadContext. This has proven problematic in certain circumstances. The deserialization of types by System.Xaml into a custom ALC was the poster child.

    I am working on a more detailed design proposal now. If there is not consensus by 3/26 we can defer to another API review for preview5.

  14. vitek-karas commented on Mar 14, 2019

    @vitek-karas
    Member
    1. seems weird that some ctors will default to collectible being false while the one taking a name defaults to true. That seems inconsistent. Having or not having a name feels logically independent to whether or not you want to be collectible.

    All constructors should default to non-collectible. Marking the context as collectible carries a performance penalty for code loaded into the context. It should be explicit opt-in.

    +1 on having a more detailed design proposal for the ExplicitContext.

  15. sdmaclea commented on Mar 19, 2019

    @sdmaclea
    ContributorAuthor

    I updated the proposal above in anticipation for the API review

    • Used markdown headers to break proposal into logical categories. Reorganized.
    • Added explanation why Load() { return null; } is a logical/reasonable default
    • Dropped the Contexts property. Added Parent and Children property to allow proper tree
    • Commented on the expected hierarchical assembly load fallback algorithm
    • Renamed ExplicitContext to ActiveContext (wanted an english verb/noun logical pair). Revised the proposal with excerpts from the in progress design doc.
    • Added a place holder for a citation to the design doc
  16. 61 remaining items

  17. sdmaclea commented on Mar 22, 2019

    @sdmaclea
    Author
  18. jeffschwMSFT commented on Mar 22, 2019

    @jeffschwMSFT
  19. vitek-karas commented on Mar 22, 2019

    @vitek-karas
  20. jeffschwMSFT commented on Mar 22, 2019

    @jeffschwMSFT
  21. sdmaclea commented on Mar 22, 2019

    @sdmaclea
    ContributorAuthor

    OK I split the API Proposal. Created dotnet/corefx#36236 & dotnet/corefx#36237. Minimized all conversations related to the now off topic proposals. Updated the top post to remove split content.

  22. jkotas commented on Mar 22, 2019

    @jkotas
  23. sdmaclea commented on Mar 22, 2019

    @sdmaclea
    Author
  24. terrajobst commented on Mar 26, 2019

    @terrajobst
    Contributor

    Video

    • Replace AssemblyLoadContext.Contexts with AssemblyLoadContext.All to make foreach easier to read
    • Other than that, looks good as proposed
  25. self-assigned this
    on Mar 26, 2019
  26. transferred this issue fromdotnet/corefxon Feb 1, 2020
  27. added this to the 3.0 milestone on Feb 1, 2020
  28. ghost locked as resolved and limited conversation to collaborators on Dec 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Runtimeblocking

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions