From fa9a9df3104d24449c35153d3d94ecf2f5fd99bb Mon Sep 17 00:00:00 2001 From: Thorsten Sommer Date: Wed, 23 Apr 2025 13:58:58 +0200 Subject: [PATCH] Implement semaphore for hot reloading to prevent concurrent file processing --- .../Tools/PluginSystem/PluginFactory.HotReload.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs index 0a32c17d..91641af6 100644 --- a/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs +++ b/app/MindWork AI Studio/Tools/PluginSystem/PluginFactory.HotReload.cs @@ -2,6 +2,8 @@ namespace AIStudio.Tools.PluginSystem; public static partial class PluginFactory { + private static readonly SemaphoreSlim HOT_RELOAD_SEMAPHORE = new(1, 1); + public static void SetUpHotReloading() { if (!IS_INITIALIZED) @@ -20,7 +22,13 @@ public static partial class PluginFactory HOT_RELOAD_WATCHER.Filter = "*.lua"; HOT_RELOAD_WATCHER.Changed += async (_, args) => { - LOG.LogInformation($"File changed: {args.FullPath}"); + if (!await HOT_RELOAD_SEMAPHORE.WaitAsync(0)) + { + LOG.LogInformation($"File changed ({args.ChangeType}): {args.FullPath}. Already processing another change."); + return; + } + + LOG.LogInformation($"File changed ({args.ChangeType}): {args.FullPath}. Reloading plugins..."); await LoadAll(); await messageBus.SendMessage(null, Event.PLUGINS_RELOADED); };