Add new API for collectible AssemblyLoadContext
This is related to the undergoing work on coreclr: Collectible assemblies and AssemblyLoadContext
I would like to add the following API to the System.Runtime.Loader.AssemblyLoadContext:
// Allow to create a Collectible ALC. The default constructor will call this method with false
protected AssemblyLoadContext(bool isCollectible);
// Returns true if this ALC is collectible
public bool IsCollectible {get; }
// Allows to explicitly unload an ALC. Once this method is called,
// any call to LoadFromXXX method will throw an exception
public void Unload();
cc: @davidwrighton @jkotas
cc: @gkhanna79 @kouvel
Before we submit this for review, @davidwrighton @jkotas @gkhanna79 @kouvel -- any comments on this API?
This API is part of the collectible load context work @xoofx has been driving.
CC @rahku @kouvel
Looks good to me
What's the behavior of Unload?
- Presumbaly,
Unloadwill throw an exception if the context isn't collectible. In that case it seems a bit odd that we use both "collectible" and "unload". We should probably use one terminology. - Or does
Unloadwork for any context and simply result in disallowing future loads?
Currently, the behavior is to throw an exception if the ALC is not collectible. Do you think of using something like Collect()? (with the associating event Collecting - which would break the current Unloading event introduced).
We could also use the IDisposable pattern instead (I'm not a particular supporter of the Unload API actually) and keep the Unloading event as it is, but still the behavior for non collectible would not be clear (it would just disallow access to LoadFromXXX methods, but would not unload anything)
If It was possible, I would ditch IsCollectible/Unload at all and make ALC always collectible (and would fix the remaining issues that makes collectible assemblies less efficient than regular assemblies... a work that I would like to do anyway)
If It was possible, I would ditch IsCollectible/Unload at all and make ALC always collectible
Unloadability will always have runtime overhead. It is important that folks pay this overhead only if they need it.
Unloadability will always have runtime overhead.
Never looked exactly but while checking this, Is it mainly related to the empty CGCDescSeries *pSeries that are inserted in the code? (attached to FEATURE_COLLECTIBLE_TYPES)... looks like indeed the overhead should be avoided then...
To me "unload" assumes a user-initiated action. "Collection" implies an automated clean-up. We should be consistent with out nomenclature. If we call the feature "collectible load context" having an "Unload" that shows up on all load context but will only work when it's collectible load context seems surprising.
What about a new CollectibleAssemblyLoadContext instead?
We already have the event called Unloading that was part of the API surface for 1.0.0 and it would be a breaking change to change its name. Maybe we should stick with "unload" and call the parameter "canBeUnloaded", and the property maybe could also be "CanBeUnloaded"? The Unload method is user-initiated, but like Dispose, it may take some time for objects to be finalized after the Dispose call (and only after all references are released). And also like Dispose, I assume AssemblyLoadContext instances can be finalized before Unload is called. It seems to me like Unload has similar functionality to Dispose but with a more applicable name for this type.
Any further thoughts on this?
@terrajobst for question above. Would like to get this to review (or not)
Setting ready for review so that Immo sees it
@kouvel
Your explanation makes total sense. So what about this?
namespace System.Runtime.Loader
{
public class AssemblyLoadContext
{
// Allow to create an unloadable ALC. The default constructor
// will call this method with false
protected AssemblyLoadContext(bool unloadable);
// Returns true if this ALC is collectible
public bool Unloadable {get; }
// Allows to explicitly unload an ALC. Once this method is called,
// any call to LoadFromXXX method will throw an exception
public void Unload();
}
}
Looks fine to me.
Same here
Looks good
@terrajobst @kouvel why is it marked as "api-approved", while it seems we're still discussing API shape? Or did I miss some point in the discussion?