natemcmaster / DotNetCorePlugins

.NET Core library for dynamically loading code
Apache License 2.0
1.61k stars 227 forks source link

EnableHotReload error handling #222

Closed devna13 closed 1 year ago

devna13 commented 3 years ago

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

https://github.com/natemcmaster/DotNetCorePlugins/blob/452f8d306ef17c84c8b02b948e93eb95ef182be3/src/Plugins/PluginLoader.cs#L225

Thanks

natemcmaster commented 2 years ago

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.)

github-actions[bot] commented 1 year ago

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.

github-actions[bot] commented 1 year ago

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.