runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Add new API for collectible AssemblyLoadContext

Open xoofx opened this issue 9 years ago • 34 comments

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();

xoofx avatar Dec 27 '16 19:12 xoofx

cc: @davidwrighton @jkotas

karelz avatar Dec 27 '16 23:12 karelz

cc: @gkhanna79 @kouvel

xoofx avatar Dec 27 '16 23:12 xoofx

Before we submit this for review, @davidwrighton @jkotas @gkhanna79 @kouvel -- any comments on this API?

AlexGhiondea avatar Jan 04 '17 18:01 AlexGhiondea

This API is part of the collectible load context work @xoofx has been driving.

CC @rahku @kouvel

gkhanna79 avatar Jan 04 '17 18:01 gkhanna79

Looks good to me

kouvel avatar Jan 04 '17 19:01 kouvel

What's the behavior of Unload?

  • Presumbaly, Unload will 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 Unload work for any context and simply result in disallowing future loads?

terrajobst avatar Jan 13 '17 19:01 terrajobst

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)

xoofx avatar Jan 13 '17 19:01 xoofx

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.

jkotas avatar Jan 13 '17 20:01 jkotas

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...

xoofx avatar Jan 13 '17 20:01 xoofx

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.

terrajobst avatar Jan 16 '17 21:01 terrajobst

What about a new CollectibleAssemblyLoadContext instead?

xoofx avatar Jan 17 '17 08:01 xoofx

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.

kouvel avatar Jan 17 '17 18:01 kouvel

Any further thoughts on this?

kouvel avatar Jan 25 '17 20:01 kouvel

@terrajobst for question above. Would like to get this to review (or not)

danmoseley avatar Feb 04 '17 21:02 danmoseley

Setting ready for review so that Immo sees it

danmoseley avatar Mar 14 '17 16:03 danmoseley

@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();
    }
}

terrajobst avatar Mar 14 '17 17:03 terrajobst

Looks fine to me.

gkhanna79 avatar Mar 14 '17 17:03 gkhanna79

Same here

kouvel avatar Mar 14 '17 17:03 kouvel

Looks good

chuckeng-isp avatar Apr 07 '17 13:04 chuckeng-isp

@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?

karelz avatar Apr 07 '17 18:04 karelz