Implement semaphore for plugin loading to prevent concurrent initialization

This commit is contained in:
Thorsten Sommer 2025-04-23 13:58:25 +02:00
parent 5fe3e1cb34
commit 9cf4f943fb
Signed by: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -13,6 +13,7 @@ public static partial class PluginFactory
private static readonly SettingsManager SETTINGS_MANAGER = Program.SERVICE_PROVIDER.GetRequiredService<SettingsManager>();
private static readonly List<IAvailablePlugin> AVAILABLE_PLUGINS = [];
private static readonly List<PluginBase> RUNNING_PLUGINS = [];
private static readonly SemaphoreSlim PLUGIN_LOAD_SEMAPHORE = new(1, 1);
private static bool IS_INITIALIZED;
private static string DATA_DIR = string.Empty;
@ -73,6 +74,11 @@ public static partial class PluginFactory
return;
}
if (!await PLUGIN_LOAD_SEMAPHORE.WaitAsync(0, cancellationToken))
return;
try
{
LOG.LogInformation("Start loading plugins.");
if (!Directory.Exists(PLUGINS_ROOT))
{
@ -127,6 +133,12 @@ public static partial class PluginFactory
// Start or restart all plugins:
await RestartAllPlugins(cancellationToken);
}
finally
{
PLUGIN_LOAD_SEMAPHORE.Release();
LOG.LogInformation("Finished loading plugins.");
}
}
private static async Task<PluginBase> Load(string pluginPath, string code, CancellationToken cancellationToken = default)
{