Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[release/8.0] Fix crash when calling AssemblyLoadContext.Unload twice #91346

Merged
merged 2 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,25 +135,32 @@ private void InitiateUnload()
{
RaiseUnloadEvent();

InternalState previousState;

// When in Unloading state, we are not supposed to be called on the finalizer
// as the native side is holding a strong reference after calling Unload
lock (_unloadLock)
{
Debug.Assert(_state == InternalState.Alive);

var thisStrongHandle = GCHandle.Alloc(this, GCHandleType.Normal);
var thisStrongHandlePtr = GCHandle.ToIntPtr(thisStrongHandle);
// The underlying code will transform the original weak handle
// created by InitializeLoadContext to a strong handle
PrepareForAssemblyLoadContextRelease(_nativeAssemblyLoadContext, thisStrongHandlePtr);
previousState = _state;
if (previousState == InternalState.Alive)
{
var thisStrongHandle = GCHandle.Alloc(this, GCHandleType.Normal);
var thisStrongHandlePtr = GCHandle.ToIntPtr(thisStrongHandle);
// The underlying code will transform the original weak handle
// created by InitializeLoadContext to a strong handle
PrepareForAssemblyLoadContextRelease(_nativeAssemblyLoadContext, thisStrongHandlePtr);

_state = InternalState.Unloading;
_state = InternalState.Unloading;
}
}

Dictionary<long, WeakReference<AssemblyLoadContext>> allContexts = AllContexts;
lock (allContexts)
if (previousState == InternalState.Alive)
{
allContexts.Remove(_id);
Dictionary<long, WeakReference<AssemblyLoadContext>> allContexts = AllContexts;
lock (allContexts)
{
allContexts.Remove(_id);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ public partial class AssemblyLoadContextTest
// Tests related to Collectible assemblies

[MethodImpl(MethodImplOptions.NoInlining)]
static void CreateAndLoadContext(CollectibleChecker checker)
static void CreateAndLoadContext(CollectibleChecker checker, bool unloadTwice = false)
{
var alc = new ResourceAssemblyLoadContext(true);
checker.SetAssemblyLoadContext(0, alc);

alc.Unload();
if (unloadTwice)
{
alc.Unload();
}

// Check that any attempt to load an assembly after an explicit Unload will fail
Assert.Throws<InvalidOperationException>(() => alc.LoadFromAssemblyPath(Path.GetFullPath("none.dll")));
Expand All @@ -39,6 +43,19 @@ public static void Unload_CollectibleWithNoAssemblyLoaded()
checker.GcAndCheck();
}

[Fact]
[ActiveIssue("https://github.com/mono/mono/issues/15142", TestRuntimes.Mono)]
public static void DoubleUnload_CollectibleWithNoAssemblyLoaded()
{
// Use a collectible ALC + Unload
// Check that we receive the Unloading event

var checker = new CollectibleChecker(1);
CreateAndLoadContext(checker, unloadTwice: true);
checker.GcAndCheck();
}


[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsPreciseGcSupported))]
public static void Finalizer_CollectibleWithNoAssemblyLoaded()
{
Expand Down
Loading