File: shutdown-deadlock.diff

package info (click to toggle)
python3.11 3.11.2-6%2Bdeb12u6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 113,292 kB
  • sloc: python: 660,794; ansic: 553,003; xml: 31,209; sh: 5,453; cpp: 3,978; makefile: 1,987; asm: 1,486; objc: 761; lisp: 502; javascript: 118; csh: 12
file content (30 lines) | stat: -rw-r--r-- 1,130 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
Description: [3.11] GH-102126: fix deadlock at shutdown when clearing thread states (GH-102222)
 (cherry picked from commit 5f11478ce7fda826d399530af4c5ca96c592f144)
Author: Kumar Aditya
Origin: upstream, https://github.com/python/cpython/commit/026faf20cc9d1d5913ff7c01a93d8934594d7fec
Bug-Debian: https://bugs.debian.org/1032019
Bug-Upstream: https://github.com/python/cpython/issues/102126
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -396,11 +396,19 @@
         _PyErr_Clear(tstate);
     }
 
+    // Clear the current/main thread state last.
     HEAD_LOCK(runtime);
-    for (PyThreadState *p = interp->threads.head; p != NULL; p = p->next) {
+    PyThreadState *p = interp->threads.head;
+    HEAD_UNLOCK(runtime);
+    while (p != NULL) {
+        // See https://github.com/python/cpython/issues/102126
+        // Must be called without HEAD_LOCK held as it can deadlock
+        // if any finalizer tries to acquire that lock.
         PyThreadState_Clear(p);
+        HEAD_LOCK(runtime);
+        p = p->next;
+        HEAD_UNLOCK(runtime);
     }
-    HEAD_UNLOCK(runtime);
 
     Py_CLEAR(interp->audit_hooks);