Unloading
}
- private static readonly Dictionary<long, WeakReference<AssemblyLoadContext>> s_contextsToUnload = new Dictionary<long, WeakReference<AssemblyLoadContext>>();
+ private static readonly Dictionary<long, WeakReference<AssemblyLoadContext>> s_allContexts = new Dictionary<long, WeakReference<AssemblyLoadContext>>();
private static long s_nextId;
private static bool s_isProcessExiting;
// Indicates the state of this ALC (Alive or in Unloading state)
private InternalState _state;
- // Id used by s_contextsToUnload
+ // Id used by s_allContexts
private readonly long _id;
// synchronization primitive to protect against usage of this instance while unloading
}
// Add this instance to the list of alive ALC
- lock (s_contextsToUnload)
+ lock (s_allContexts)
{
if (s_isProcessExiting)
{
_nativeAssemblyLoadContext = InitializeAssemblyLoadContext(thisHandlePtr, representsTPALoadContext, isCollectible);
_id = s_nextId++;
- s_contextsToUnload.Add(_id, new WeakReference<AssemblyLoadContext>(this, true));
+ s_allContexts.Add(_id, new WeakReference<AssemblyLoadContext>(this, true));
}
}
if (!s_isProcessExiting)
{
- lock (s_contextsToUnload)
+ lock (s_allContexts)
{
- s_contextsToUnload.Remove(_id);
+ s_allContexts.Remove(_id);
}
}
}
AssemblyLoadContext d = AssemblyLoadContext.Default; // Ensure default is initialized
List<WeakReference<AssemblyLoadContext>> alcList = null;
- lock (s_contextsToUnload)
+ lock (s_allContexts)
{
// To make this thread safe we need a quick snapshot while locked
- alcList = new List<WeakReference<AssemblyLoadContext>>(s_contextsToUnload.Values);
+ alcList = new List<WeakReference<AssemblyLoadContext>>(s_allContexts.Values);
}
foreach (WeakReference<AssemblyLoadContext> weakAlc in alcList)
internal static void OnProcessExit()
{
- lock (s_contextsToUnload)
+ lock (s_allContexts)
{
s_isProcessExiting = true;
- foreach (var alcAlive in s_contextsToUnload)
+ foreach (var alcAlive in s_allContexts)
{
- if (alcAlive.Value.TryGetTarget(out AssemblyLoadContext alc))
+ if (alcAlive.Value.TryGetTarget(out AssemblyLoadContext alc) && alc.IsCollectible)
{
// Should we use a try/catch?
alc.InitiateUnload();
}
}
- s_contextsToUnload.Clear();
+ s_allContexts.Clear();
}
}