8000 A better fix for releasing a re-acquiring the GIL · pythonnet/pythonnet@19d1379 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 19d1379

Browse files
committed
A better fix for releasing a re-acquiring the GIL
On shutdown and startup. The main python thread always havea state. After releasing the state on shutdown, restore the state of the main thread (thus also locking the GIL).
1 parent 07aefe6 commit 19d1379

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

src/runtime/runtime.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
151151
}
152152
else
153153
{
154-
// When initializing more than once (like on soft shutdown and domain
155-
// reload), the GIL might not be acquired by the current thread.
156-
state = PyGILState_Ensure();
154+
// If we're coming back from a domain reload or a soft shutdown,
155+
// we have previously released the thread state. Restore the main
156+
// thread state here.
157+
PyEval_RestoreThread(PyGILState_GetThisThreadState());
157158
MainManagedThreadId = Thread.CurrentThread.ManagedThreadId;
158159
}
159160

@@ -198,10 +199,6 @@ internal static void Initialize(bool initSigs = false, ShutdownMode mode = Shutd
198199
}
199200
XDecref(item);
200201
AssemblyManager.UpdatePath();
201-
if (state != IntPtr.Zero)
202-
{
203-
PyGILState_Release(state);
204-
}
205202
}
206203

207204
private static void InitPyMembers()

0 commit comments

Comments
 (0)
0