Skip to content

Calling AssemblyLoadContext.Unload multiple times causes fatal crash  #91087

@maxima120

Description

@maxima120

Description

Unfortunately I cant provide repro case (yet). But at least it is reproducible every time I try certain scenario in my app.

I am getting this exception in WPF .NET 7 app. It happens when I try to unload a dll loaded in run-time.. The unload works if called correctly.

But if it tries to call apploadingContext.Unload second time (there is a bug somewhere in my code) - the exception ExecutionEngineException is shown in VS if I debug and if I run it standalone I get this error in event log:

Faulting application name: qqq.exe, version: 0.4.0.1, time stamp: 0x64b07659
Faulting module name: coreclr.dll, version: 7.0.1023.36312, time stamp: 0x64b06d6c
Exception code: 0xc0000005
Fault offset: 0x0000000000133fa2
Faulting process ID: 0xaea4
Faulting application start time: 0x01d9d69df3fe6c1e
Faulting application path: qqqq\bin\Debug\net7.0-windows\qqq.exe
Faulting module path: C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.10\coreclr.dll
Report ID: 9616639b-d2b2-4669-95da-65f25f23f4e7
Faulting package full name: 
Faulting package-relative application ID: 

Reproduction Steps

N/A

Expected behavior

Since this exception marked as obsolete I think it should not be thrown. Perhaps new design should react on fatal conditions somehow differently.. Having said that I want to point that dll I am loading is few lines C# compiled in run-time with Roslyn and it does not do any funny stuff like unmanaged memory access, so I suspect that this exception is a side-effect from a bug in AssemblyContextLoader..

Actual behavior

Application crashes and no way to recover from it.

Regression?

N/A

Known Workarounds

None

Configuration

.NET 7.0 on Windows 10 x64 build 19045

Other information

the code for the loader is taken from MSDN code:

    public class MyAssemblyLoader : AssemblyLoadContext//, IDisposable
    {
        private AssemblyDependencyResolver resolver;

        public MyAssemblyLoader (string pluginPath) : base(pluginPath, true)
        {
            resolver = new AssemblyDependencyResolver(pluginPath);
        }

        protected override Assembly Load(AssemblyName assemblyName)
        {
            string assemblyPath = resolver.ResolveAssemblyToPath(assemblyName);
            if (assemblyPath != null)
            {
                return LoadFromAssemblyPath(assemblyPath);
            }

            return null;
        }

        protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
        {
            string libraryPath = resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
            if (libraryPath != null)
            {
                return LoadUnmanagedDllFromPath(libraryPath);
            }

            return IntPtr.Zero;
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions