Description
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
Nameto 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
ContextstoAllContextsorGetAllContexts
Activity
sdmaclea commentedon Jan 23, 2019
#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
jeffschwMSFT commentedon Jan 24, 2019
@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?
sdmaclea commentedon Jan 24, 2019
#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.
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
Defaultcontext. 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 aTryLoadevent handler onDefault.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
TryLoadjkotas commentedon Jan 24, 2019
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.
jkoritzinsky commentedon Jan 24, 2019
Is there any particular reason that the
Contextsmember is anIEnumerator<AssemblyLoadContext>instead of anIEnumerable<AssemblyLoadContext>?sdmaclea commentedon Jan 24, 2019
@jkoritzinsky Thanks for pointing out the unintended inconsistency.
IEnumerable<T>for creatingIEnumerator<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
IEnumerator<T>, is semantically incorrect.using properties returningIEnumerable<T>seems clumsymethods returningIEnumerator<T>seems best.I will update top post.
foreach (AssemblyLoadContext alc in AssemblyLoadContext.Contexts.GetEnumerator())jkoritzinsky commentedon Jan 24, 2019
You don't need the
GetEnumerator()call in a foreach statement. You can doforeach (AssemblyLoadContext alc in AssemblyLoadContext.Contexts)ifContextsis anIEnumerable<AssemblyLoadContext>. I at least find it clumsier to useIEnumerator<T>directly. Others may feel differently though.GSPP commentedon Jan 24, 2019
Nowhere is
IEnumeratorever used directly if not absolutely necessary. This is best for convenience reasons and now also for consistency.sdmaclea commentedon Jan 24, 2019
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>sdmaclea commentedon Mar 8, 2019
/cc @lambdageek
terrajobst commentedon Mar 14, 2019
A few questions:
It seems weird that some ctors will default to collectible being
falsewhile the one taking a name defaults totrue. That seems inconsistent. Having or not having a name feels logically independent to whether or not you want to be collectible.You said For legacy usage, these would default to the Custom ALC type name.. How would a customer have observed the name?
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?
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-staticAssembliesproperty 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'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.
sdmaclea commentedon Mar 14, 2019
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.
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, butToString()includes the type name. I revised spec to be consistent with the prototype.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 ALCResolvingevent handler, then the AppDomainAssemblyResolveevent handler.The only reason to override the Load() method would be to get first shot at loading an assembly.
I intend both to be thread safe. With minimal locking. There is a prototype implementation in dotnet/corefx#22273. The
Contextsprototype 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.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.
vitek-karas commentedon Mar 14, 2019
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.sdmaclea commentedon Mar 19, 2019
I updated the proposal above in anticipation for the API review
Load() { return null; }is a logical/reasonable defaultDropped theContextsproperty. AddedParentandChildrenproperty to allow proper treeCommented on the expected hierarchical assembly load fallback algorithmExplicitContexttoActiveContext(wanted an english verb/noun logical pair). Revised the proposal with excerpts from the in progress design doc.61 remaining items
sdmaclea commentedon Mar 22, 2019
jeffschwMSFT commentedon Mar 22, 2019
vitek-karas commentedon Mar 22, 2019
jeffschwMSFT commentedon Mar 22, 2019
sdmaclea commentedon Mar 22, 2019
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.
jkotas commentedon Mar 22, 2019
sdmaclea commentedon Mar 22, 2019
terrajobst commentedon Mar 26, 2019
Video
AssemblyLoadContext.ContextswithAssemblyLoadContext.Allto makeforeacheasier to readUpdate AssemblyLoadContext ref assembly
Update AssemblyLoadContext ref assembly