File tree 2 files changed +11
-2
lines changed
Misc/NEWS.d/next/Core and Builtins
2 files changed +11
-2
lines changed Original file line number Diff line number Diff line change
1
+ Fix deadlock at shutdown when clearing thread states if any finalizer tries to acquire the runtime head lock. Patch by Kumar Aditya.
Original file line number Diff line number Diff line change @@ -288,11 +288,19 @@ PyInterpreterState_Clear(PyInterpreterState *interp)
288
288
_PyErr_Clear (tstate );
289
289
}
290
290
291
+ // Clear the current/main thread state last.
291
292
HEAD_LOCK (runtime );
292
- for (PyThreadState * p = interp -> tstate_head ; p != NULL ; p = p -> next ) {
293
+ PyThreadState * p = interp -> tstate_head ;
294
+ HEAD_UNLOCK (runtime );
295
+ while (p != NULL ) {
296
+ // See https://github.com/python/cpython/issues/102126
297
+ // Must be called without HEAD_LOCK held as it can deadlock
298
+ // if any finalizer tries to acquire that lock.
293
299
PyThreadState_Clear (p );
300
+ HEAD_LOCK (runtime );
301
+ p = p -> next ;
302
+ HEAD_UNLOCK (runtime );
294
303
}
295
- HEAD_UNLOCK (runtime );
296
304
297
305
Py_CLEAR (interp -> audit_hooks );
298
306
You can’t perform that action at this time.
0 commit comments