From 4adbbb9f5111d2386f1f0a9c5aaa173d041e9c90 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Sun, 24 Jan 2021 00:57:37 +0100 Subject: [PATCH 1/4] Catch TypeLoadException during plugin loading --- Emby.Server.Implementations/Plugins/PluginManager.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index 9f597f3ef3..42c76bd66a 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -122,6 +122,12 @@ namespace Emby.Server.Implementations.Plugins ChangePluginState(plugin, PluginStatus.Malfunctioned); continue; } + catch (TypeLoadException ex) + { + _logger.LogError(ex, "Failed to load assembly {Path}. Disabling plugin. This is probably caused by an incompatible plugin version.", file); + ChangePluginState(plugin, PluginStatus.Malfunctioned); + continue; + } _logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file); yield return assembly; From 80f3e20394bc249914ddcb430b4f6d63761ebd29 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 24 Jan 2021 13:22:04 +0100 Subject: [PATCH 2/4] Change plugin error message --- .../Plugins/PluginManager.cs | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index 42c76bd66a..9621b34b62 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -112,9 +112,16 @@ namespace Emby.Server.Implementations.Plugins { assembly = Assembly.LoadFrom(file); - // This force loads all reference dll's that the plugin uses in the try..catch block. - // Removing this will cause JF to bomb out if referenced dll's cause issues. - assembly.GetExportedTypes(); + try + { + assembly.GetExportedTypes(); + } + catch (TypeLoadException ex) // Undocumented exception + { + _logger.LogError(ex, "Failed to load assembly {Path}. This error occurs when a plugin references an incompatible version of one of the shared libraries. Disabling plugin.", file); + ChangePluginState(plugin, PluginStatus.NotSupported); + continue; + } } catch (FileLoadException ex) { @@ -122,12 +129,6 @@ namespace Emby.Server.Implementations.Plugins ChangePluginState(plugin, PluginStatus.Malfunctioned); continue; } - catch (TypeLoadException ex) - { - _logger.LogError(ex, "Failed to load assembly {Path}. Disabling plugin. This is probably caused by an incompatible plugin version.", file); - ChangePluginState(plugin, PluginStatus.Malfunctioned); - continue; - } _logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file); yield return assembly; From 677bba742e343271be39d4da6d10eca9720c0403 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 24 Jan 2021 13:34:22 +0100 Subject: [PATCH 3/4] Remove try-catch nesting --- .../Plugins/PluginManager.cs | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index 9621b34b62..de4b71433a 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -112,16 +112,7 @@ namespace Emby.Server.Implementations.Plugins { assembly = Assembly.LoadFrom(file); - try - { - assembly.GetExportedTypes(); - } - catch (TypeLoadException ex) // Undocumented exception - { - _logger.LogError(ex, "Failed to load assembly {Path}. This error occurs when a plugin references an incompatible version of one of the shared libraries. Disabling plugin.", file); - ChangePluginState(plugin, PluginStatus.NotSupported); - continue; - } + assembly.GetExportedTypes(); } catch (FileLoadException ex) { @@ -129,6 +120,12 @@ namespace Emby.Server.Implementations.Plugins ChangePluginState(plugin, PluginStatus.Malfunctioned); continue; } + catch (TypeLoadException ex) // Undocumented exception + { + _logger.LogError(ex, "Failed to load assembly {Path}. This error occurs when a plugin references an incompatible version of one of the shared libraries. Disabling plugin.", file); + ChangePluginState(plugin, PluginStatus.NotSupported); + continue; + } _logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file); yield return assembly; From b014f2309d546779d7e9530daa1ddd55741cbb32 Mon Sep 17 00:00:00 2001 From: David Ullmer Date: Mon, 25 Jan 2021 09:44:06 +0100 Subject: [PATCH 4/4] Update Emby.Server.Implementations/Plugins/PluginManager.cs Co-authored-by: Claus Vium --- Emby.Server.Implementations/Plugins/PluginManager.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index de4b71433a..adf62124a7 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -126,6 +126,14 @@ namespace Emby.Server.Implementations.Plugins ChangePluginState(plugin, PluginStatus.NotSupported); continue; } +#pragma warning disable CA1031 // Do not catch general exception types + catch (Exception ex) +#pragma warning restore CA1031 // Do not catch general exception types + { + _logger.LogError(ex, "Failed to load assembly {Path}. Unknown exception was thrown. Disabling plugin.", file); + ChangePluginState(plugin, PluginStatus.Malfunctioned); + continue; + } _logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file); yield return assembly;