natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

EnableHotReload error handling

devna13 opened this issue · comments

I'm new to this awesome repo, and using EnableHotReload to update plugins on the fly similar to your hot reload sample

  loader = PluginLoader.CreateFromAssemblyFile(
                            pluginDllPath,
                            sharedTypes: new[] {
                               typeof(IPluginMessageHandler),
                               //typeof(IServiceCollection),
                               typeof(ILogger)
                            },
                            isUnloadable: true,
                            configure: config => config.EnableHotReload = true);

everything works great but I ran into an edge case; If I publish a bad plugin, that doesn't have all of it's dependencies, it causes the host app to crash with this CLR exception

Fatal error. Internal CLR error. (0x80131506)
   at System.Runtime.Loader.AssemblyLoadContext.PrepareForAssemblyLoadContextRelease(IntPtr, IntPtr)
   at System.Runtime.Loader.AssemblyLoadContext.InitiateUnload()
   at System.Runtime.Loader.AssemblyLoadContext.Unload()
   at McMaster.NETCore.Plugins.PluginLoader.Reload()
   at McMaster.NETCore.Plugins.Internal.Debouncer+<>c__DisplayClass4_0.<Execute>b__0(System.Threading.Tasks.Task)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(System.Threading.Thread, System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(System.Threading.Tasks.Task ByRef, System.Threading.Thread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()

I was wondering if wrapping this line in try catch would resolve it, or there is a better way to prevent host from crashing

Reloaded?.Invoke(this, new PluginReloadedEventArgs(this));

Thanks

I'll mark this as 'help wanted'. There are probably better ways to handle these errors and would be open to contributions to improve it.

(FYI - this project is in maintenance mode right now. See #117 for detail on what the applied labels indicate.)

This issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please comment if you believe this should remain open, otherwise it will be closed in 14 days. Thank you for your contributions to this project.

Closing due to inactivity.
If you are looking at this issue in the future and think it should be reopened, please make a commented here and mention natemcmaster so he sees the notification.