diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs index 88d57a18b..b5a874431 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs @@ -13,7 +13,7 @@ public class PyCodeInterpreter : ICodeProcessor private readonly IServiceProvider _services; private readonly ILogger _logger; private readonly CodingSettings _settings; - private static SemaphoreSlim _semLock = new(initialCount: 1, maxCount: 1); + private static readonly SemaphoreSlim _semLock = new(initialCount: 1, maxCount: 1); public PyCodeInterpreter( IServiceProvider services, @@ -95,6 +95,7 @@ public async Task GenerateCodeScriptAsync(string text, Cod private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretOptions? options = null, CancellationToken cancellationToken = default) { var lockAcquired = false; + try { _semLock.Wait(cancellationToken); @@ -103,7 +104,7 @@ private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretO } catch (Exception ex) { - _logger.LogError(ex, $"Error in {nameof(InnerRunWithLock)}"); + _logger.LogError(ex, $"Error in {nameof(InnerRunWithLock)} in {Provider}"); return new() { ErrorMsg = ex.Message }; } finally @@ -157,6 +158,8 @@ private CodeInterpretResponse CoreRunScript(string codeScript, CodeInterpretOpti var execTask = Task.Factory.StartNew(() => { + Thread.Sleep(100); + // For observation purpose var requestId = Guid.NewGuid(); _logger.LogWarning($"Before acquiring Py.GIL for request {requestId}");