]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-128400: Only show the current thread in `Py_FatalError` on the free-threaded build...
authorPeter Bierma <zintensitydev@gmail.com>
Mon, 13 Jan 2025 14:36:54 +0000 (09:36 -0500)
committerGitHub <noreply@github.com>
Mon, 13 Jan 2025 14:36:54 +0000 (20:06 +0530)
Lib/test/test_capi/test_misc.py
Lib/test/test_faulthandler.py
Misc/NEWS.d/next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst [new file with mode: 0644]
Python/pylifecycle.c

index b62bc4c2ecd980af5715b7af7fc5822e447faa4e..99d9a757759dcdca12f395459ba929ab29195af3 100644 (file)
@@ -75,6 +75,8 @@ class InstanceMethod:
     id = _testcapi.instancemethod(id)
     testfunction = _testcapi.instancemethod(testfunction)
 
+CURRENT_THREAD_REGEX = r'Current thread.*:\n' if not support.Py_GIL_DISABLED else r'Stack .*:\n'
+
 class CAPITest(unittest.TestCase):
 
     def test_instancemethod(self):
@@ -234,8 +236,8 @@ class CAPITest(unittest.TestCase):
                 r'Python runtime state: initialized\n'
                 r'SystemError: <built-in function return_null_without_error> '
                     r'returned NULL without setting an exception\n'
-                r'\n'
-                r'Current thread.*:\n'
+                r'\n' +
+                CURRENT_THREAD_REGEX +
                 r'  File .*", line 6 in <module>\n')
         else:
             with self.assertRaises(SystemError) as cm:
@@ -268,8 +270,8 @@ class CAPITest(unittest.TestCase):
                     r'SystemError: <built-in '
                         r'function return_result_with_error> '
                         r'returned a result with an exception set\n'
-                    r'\n'
-                    r'Current thread.*:\n'
+                    r'\n' +
+                    CURRENT_THREAD_REGEX +
                     r'  File .*, line 6 in <module>\n')
         else:
             with self.assertRaises(SystemError) as cm:
@@ -298,8 +300,8 @@ class CAPITest(unittest.TestCase):
                         r'with an exception set\n'
                     r'Python runtime state: initialized\n'
                     r'ValueError: bug\n'
-                    r'\n'
-                    r'Current thread .* \(most recent call first\):\n'
+                    r'\n' +
+                    CURRENT_THREAD_REGEX +
                     r'  File .*, line 6 in <module>\n'
                     r'\n'
                     r'Extension modules: _testcapi \(total: 1\)\n')
index 2088793cbb93873423afdcc266a043f407f454c6..75d303cd212c822fe5b2ce99209ba5e418dba646 100644 (file)
@@ -101,8 +101,7 @@ class FaultHandlerTests(unittest.TestCase):
         Raise an error if the output doesn't match the expected format.
         """
         all_threads_disabled = (
-            (not py_fatal_error)
-            and all_threads
+            all_threads
             and (not sys._is_gil_enabled())
         )
         if all_threads and not all_threads_disabled:
@@ -116,12 +115,15 @@ class FaultHandlerTests(unittest.TestCase):
         if py_fatal_error:
             regex.append("Python runtime state: initialized")
         regex.append('')
-        if all_threads_disabled:
+        if all_threads_disabled and not py_fatal_error:
             regex.append("<Cannot show all threads while the GIL is disabled>")
         regex.append(fr'{header} \(most recent call first\):')
-        if garbage_collecting and not all_threads_disabled:
-            regex.append('  Garbage-collecting')
-        regex.append(fr'  File "<string>", line {lineno} in {function}')
+        if support.Py_GIL_DISABLED and py_fatal_error and not know_current_thread:
+            regex.append("  <tstate is freed>")
+        else:
+            if garbage_collecting and not all_threads_disabled:
+                regex.append('  Garbage-collecting')
+            regex.append(fr'  File "<string>", line {lineno} in {function}')
         regex = '\n'.join(regex)
 
         if other_regex:
diff --git a/Misc/NEWS.d/next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst b/Misc/NEWS.d/next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst
new file mode 100644 (file)
index 0000000..b9c117b
--- /dev/null
@@ -0,0 +1,2 @@
+:c:func:`Py_FatalError` no longer shows all threads on the :term:`free
+threaded <free threading>` build to prevent crashes.
index 8a15a09a01dbf747a688e9f22a79b93eeab61344..9a8a92aaceb0be11e0bbb4c72ebe8115da1de1ff 100644 (file)
@@ -3034,7 +3034,11 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
     PUTS(fd, "\n");
 
     /* display the current Python stack */
+#ifndef Py_GIL_DISABLED
     _Py_DumpTracebackThreads(fd, interp, tstate);
+#else
+    _Py_DumpTraceback(fd, tstate);
+#endif
 }
 
 /* Print the current exception (if an exception is set) with its traceback,