Skip to content

Commit 8075f48

Browse files
committed
Add corrections to the GIL acquiring/releasing
In Runtime's Initialize and Shutdown.
1 parent 7e5ab52 commit 8075f48

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/runtime/runtime.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ public class Runtime
111111
/// <summary>
112112
/// Initialize the runtime...
113113
/// </summary>
114+
/// <remarks>When calling this method after a soft shutdown or a domain reload,
115+
/// this method acquires and releases the GIL. </remarks>
114116
internal static void Initialize(bool initSigs = false, ShutdownMode mode = ShutdownMode.Default)
115117
{
116118
if (_isInitialized)
@@ -149,7 +151,8 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
149151
}
150152
else
151153
{
152-
PyEval_InitThreads();
154+
// When initializing more than once (like on soft shutdown and domain
155+
// reload), the GIL might not be acquired by the current thread.
153156
state = PyGILState_Ensure();
154157
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
155158
}
@@ -366,6 +369,8 @@ internal static void Shutdown(ShutdownMode mode)
366369
// Some clr runtime didn't implement GC.WaitForFullGCComplete yet.
367370
}
368371
PyGILState_Release(state);
372+
// Then release the GIL for good.
373+
PyEval_SaveThread();
369374
}
370375
else
371376
{

0 commit comments

Comments
 (0)