From 99c346952871128ae5b12c81e36f0c3f2322ff6e Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Thu, 13 Nov 2025 19:18:45 -0600 Subject: [PATCH 1/2] revert code with lock --- .../Services/PyCodeInterpreter.cs | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs index 88d57a18b..0ada21d4a 100644 --- a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs +++ b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs @@ -31,7 +31,15 @@ public CodeInterpretResponse Run(string codeScript, CodeInterpretOptions? option { if (options?.UseLock == true) { - return InnerRunWithLock(codeScript, options, cancellationToken); + try + { + return InnerRunWithLock(codeScript, options, cancellationToken); + } + catch (Exception ex) + { + _logger.LogError(ex, $"Error when running code script with lock in {Provider}."); + return new() { ErrorMsg = ex.Message }; + } } return InnerRunCode(codeScript, options, cancellationToken); @@ -94,11 +102,10 @@ public async Task GenerateCodeScriptAsync(string text, Cod #region Private methods private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretOptions? options = null, CancellationToken cancellationToken = default) { - var lockAcquired = false; + _semLock.Wait(cancellationToken); + try { - _semLock.Wait(cancellationToken); - lockAcquired = true; return InnerRunCode(codeScript, options, cancellationToken); } catch (Exception ex) @@ -108,10 +115,7 @@ private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretO } finally { - if (lockAcquired) - { - _semLock.Release(); - } + _semLock.Release(); } } @@ -157,6 +161,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}"); From cf9b7fac307499b1db7d22195abb34b3db72e063 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Thu, 13 Nov 2025 19:25:53 -0600 Subject: [PATCH 2/2] revert --- .../Services/PyCodeInterpreter.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs b/src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs index 0ada21d4a..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, @@ -31,15 +31,7 @@ public CodeInterpretResponse Run(string codeScript, CodeInterpretOptions? option { if (options?.UseLock == true) { - try - { - return InnerRunWithLock(codeScript, options, cancellationToken); - } - catch (Exception ex) - { - _logger.LogError(ex, $"Error when running code script with lock in {Provider}."); - return new() { ErrorMsg = ex.Message }; - } + return InnerRunWithLock(codeScript, options, cancellationToken); } return InnerRunCode(codeScript, options, cancellationToken); @@ -102,20 +94,25 @@ public async Task GenerateCodeScriptAsync(string text, Cod #region Private methods private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretOptions? options = null, CancellationToken cancellationToken = default) { - _semLock.Wait(cancellationToken); + var lockAcquired = false; try { + _semLock.Wait(cancellationToken); + lockAcquired = true; return InnerRunCode(codeScript, options, cancellationToken); } 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 { - _semLock.Release(); + if (lockAcquired) + { + _semLock.Release(); + } } }