10BC0 Fixes GIL grabbing during init and shutdown · pythonnet/pythonnet@ce8ee90 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce8ee90

Browse files
committed
Fixes GIL grabbing during init and shutdown
Addresses comment: #958
1 parent b35f441 commit ce8ee90

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/runtime/runtime.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
125125
}
126126
ShutdownMode = mode;
127127

128+
IntPtr state = IntPtr.Zero;
128129
if (Py_IsInitialized() == 0)
129130
{
130131
Py_InitializeEx(initSigs ? 1 : 0);
@@ -148,7 +149,8 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
148149
}
149150
else
150151
{
151-
PyGILState_Ensure();
152+
PyEval_InitThreads();
153+
state = PyGILState_Ensure();
152154
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
153155
}
1541 E820 56

@@ -193,6 +195,10 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
193195
}
194196
XDecref(item);
195197
AssemblyManager.UpdatePath();
198+
if (state != IntPtr.Zero)
199+
{
200+
PyGILState_Release(state);
201+
}
196202
}
197203

198204
private static void InitPyMembers()
@@ -314,7 +320,7 @@ internal static void Shutdown()
314320
}
315321
_isInitialized = false;
316322

317-
PyGILState_Ensure();
323+
var state = PyGILState_Ensure();
318324

319325
var mode = ShutdownMode;
320326
if (mode == ShutdownMode.Soft)
@@ -360,7 +366,7 @@ internal static void Shutdown()
360366
{
361367
// Some clr runtime didn't implement GC.WaitForFullGCComplete yet.
362368
}
363-
PyEval_SaveThread();
369+
PyGILState_Release(state);
364370
}
365371
else
366372
{

0 commit comments

Comments
 (0)
0