Skip to content

Commit 99c3469

Browse files
committed
revert code with lock
1 parent bdb7d18 commit 99c3469

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/Plugins/BotSharp.Plugin.PythonInterpreter/Services/PyCodeInterpreter.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,15 @@ public CodeInterpretResponse Run(string codeScript, CodeInterpretOptions? option
3131
{
3232
if (options?.UseLock == true)
3333
{
34-
return InnerRunWithLock(codeScript, options, cancellationToken);
34+
try
35+
{
36+
return InnerRunWithLock(codeScript, options, cancellationToken);
37+
}
38+
catch (Exception ex)
39+
{
40+
_logger.LogError(ex, $"Error when running code script with lock in {Provider}.");
41+
return new() { ErrorMsg = ex.Message };
42+
}
3543
}
3644

3745
return InnerRunCode(codeScript, options, cancellationToken);
@@ -94,11 +102,10 @@ public async Task<CodeGenerationResult> GenerateCodeScriptAsync(string text, Cod
94102
#region Private methods
95103
private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretOptions? options = null, CancellationToken cancellationToken = default)
96104
{
97-
var lockAcquired = false;
105+
_semLock.Wait(cancellationToken);
106+
98107
try
99108
{
100-
_semLock.Wait(cancellationToken);
101-
lockAcquired = true;
102109
return InnerRunCode(codeScript, options, cancellationToken);
103110
}
104111
catch (Exception ex)
@@ -108,10 +115,7 @@ private CodeInterpretResponse InnerRunWithLock(string codeScript, CodeInterpretO
108115
}
109116
finally
110117
{
111-
if (lockAcquired)
112-
{
113-
_semLock.Release();
114-
}
118+
_semLock.Release();
115119
}
116120
}
117121

@@ -157,6 +161,8 @@ private CodeInterpretResponse CoreRunScript(string codeScript, CodeInterpretOpti
157161

158162
var execTask = Task.Factory.StartNew(() =>
159163
{
164+
Thread.Sleep(100);
165+
160166
// For observation purpose
161167
var requestId = Guid.NewGuid();
162168
_logger.LogWarning($"Before acquiring Py.GIL for request {requestId}");

0 commit comments

Comments
 (0)