From 193c712d67ef64edc44edcd781494e407349d987 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Mon, 1 Apr 2019 23:12:04 +0200 Subject: [PATCH] Remove s_isProcessExiting and Clear of ALC list at exit --- src/System.Private.CoreLib/Resources/Strings.resx | 3 --- .../System/Runtime/Loader/AssemblyLoadContext.cs | 18 +++++------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/src/System.Private.CoreLib/Resources/Strings.resx b/src/System.Private.CoreLib/Resources/Strings.resx index a912b83..d492403 100644 --- a/src/System.Private.CoreLib/Resources/Strings.resx +++ b/src/System.Private.CoreLib/Resources/Strings.resx @@ -1930,9 +1930,6 @@ Array.ConstrainedCopy will only work on array types that are provably compatible, without any form of boxing, unboxing, widening, or casting of each array element. Change the array types (i.e., copy a Derived[] to a Base[]), or use a mitigation strategy in the CER for Array.Copy's less powerful reliability contract, such as cloning the array or throwing away the potentially corrupt destination array. - - Cannot instantiate AssemblyLoadContext while the current process is exiting. - Cannot unload non-collectible AssemblyLoadContext. diff --git a/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs b/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs index 2c68a27..a2c8df0 100644 --- a/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs +++ b/src/System.Private.CoreLib/shared/System/Runtime/Loader/AssemblyLoadContext.cs @@ -30,7 +30,6 @@ namespace System.Runtime.Loader private static readonly Dictionary> s_allContexts = new Dictionary>(); private static long s_nextId; - private static bool s_isProcessExiting; // Indicates the state of this ALC (Alive or in Unloading state) private InternalState _state; @@ -74,19 +73,14 @@ namespace System.Runtime.Loader GC.SuppressFinalize(this); } + // If this is a collectible ALC, we are creating a weak handle tracking resurrection otherwise we use a strong handle + var thisHandle = GCHandle.Alloc(this, IsCollectible ? GCHandleType.WeakTrackResurrection : GCHandleType.Normal); + var thisHandlePtr = GCHandle.ToIntPtr(thisHandle); + _nativeAssemblyLoadContext = InitializeAssemblyLoadContext(thisHandlePtr, representsTPALoadContext, isCollectible); + // Add this instance to the list of alive ALC lock (s_allContexts) { - if (s_isProcessExiting) - { - throw new InvalidOperationException(SR.AssemblyLoadContext_Constructor_CannotInstantiateWhileUnloading); - } - - // If this is a collectible ALC, we are creating a weak handle tracking resurrection otherwise we use a strong handle - var thisHandle = GCHandle.Alloc(this, IsCollectible ? GCHandleType.WeakTrackResurrection : GCHandleType.Normal); - var thisHandlePtr = GCHandle.ToIntPtr(thisHandle); - _nativeAssemblyLoadContext = InitializeAssemblyLoadContext(thisHandlePtr, representsTPALoadContext, isCollectible); - _id = s_nextId++; s_allContexts.Add(_id, new WeakReference(this, true)); } @@ -385,7 +379,6 @@ namespace System.Runtime.Loader { lock (s_allContexts) { - s_isProcessExiting = true; foreach (var alcAlive in s_allContexts) { if (alcAlive.Value.TryGetTarget(out AssemblyLoadContext alc)) @@ -393,7 +386,6 @@ namespace System.Runtime.Loader alc.RaiseUnloadEvent(); } } - s_allContexts.Clear(); } } -- 2.7.4