Skip to content

Commit ab2d991

Browse files
committed
Ensure cancellation even on module creation errors
1 parent ea40f0c commit ab2d991

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Bonsai.Scripting.Python/RuntimeManager.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@ internal static DynamicModule CreateModule(string name = "", string scriptPath =
4747
var module = new DynamicModule(name);
4848
if (!string.IsNullOrEmpty(scriptPath))
4949
{
50-
var code = File.ReadAllText(scriptPath);
51-
module.Exec(code);
50+
try
51+
{
52+
var code = File.ReadAllText(scriptPath);
53+
module.Exec(code);
54+
}
55+
catch (Exception)
56+
{
57+
module.Dispose();
58+
throw;
59+
}
5260
}
5361
return module;
5462
}
@@ -100,9 +108,12 @@ public void Dispose()
100108
{
101109
if (PythonEngine.IsInitialized)
102110
{
103-
using (Py.GIL())
111+
if (MainModule != null)
104112
{
105-
MainModule.Dispose();
113+
using (Py.GIL())
114+
{
115+
MainModule.Dispose();
116+
}
106117
}
107118
PythonEngine.EndAllowThreads(threadState);
108119
PythonEngine.Shutdown();

0 commit comments

Comments
 (0)