Skip to content

Fatal error HRESULT=0x80070057 when loading/unloading multiple times assemblies. #535

@cedricmelancon

Description

@cedricmelancon

General

I ran in a crash with HRESULT=0x80050057 when creating a plugin manager. The plugin manager is scanning a folder for plugins, loads all of them, get the metadata and unload. After some plugins, the application crashes.

I managed to reproduce the problem with the sample provided on github at the following location:
https://github.com/dotnet/samples/tree/master/core/tutorials/Unloading

To reproduce the behavior, I modified the Main.cs in Host Project as follow:

static void Main(string[] args)
{
   WeakReference hostAlcWeakRef;
   string currentAssemblyDirectory = 
   Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
#if DEBUG
   string configName = "Debug";
#else
   string configName = "Release";
#endif
   
   string pluginFullPath = Path.Combine(currentAssemblyDirectory, $"..\\..\\..\\..\\Plugin\\bin\\{configName}\\netcoreapp3.0\\Plugin.dll");

   for (var iteration = 0; iteration < 10; ++iteration)
   {
      ExecuteAndUnload(pluginFullPath, out hostAlcWeakRef);

      // Poll and run GC until the AssemblyLoadContext is unloaded. 
      // You don't need to do that unless you want to know when the context
      // got unloaded. You can just leave it to the regular GC.
      for (int i = 0; hostAlcWeakRef.IsAlive && (i < 10); i++)
      {
         GC.Collect();
         GC.WaitForPendingFinalizers();
      }

      Console.WriteLine($"Unload success: {!hostAlcWeakRef.IsAlive}");

      Thread.Sleep(1500);
   }
}

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions