From 3201f793915a8ef1d3cc02918c2d0b48607345a5 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 12 Jan 2025 12:11:24 -0500 Subject: [PATCH 1/5] Don't print all threads in Py_FatalError --- Python/pylifecycle.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index 06418123d6dd9b..ca3d6f5f91761f 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -3023,7 +3023,18 @@ _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 + if (tstate == NULL) + { + /* _Py_DumpTraceback() prints out a message when tstate is null, + * whereas _Py_DumpTracebackThreads() does not. Early-return for + * consistency. */ + return; + } + _Py_DumpTraceback(fd, tstate); +#endif } /* Print the current exception (if an exception is set) with its traceback, From 6418696be22b8c74e798eb814a44b5390d8041fa Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 12 Jan 2025 12:18:10 -0500 Subject: [PATCH 2/5] Fix tests. --- Lib/test/test_faulthandler.py | 14 ++++++++------ Python/pylifecycle.c | 7 ------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 2088793cbb9387..917d193ab5f76b 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -101,8 +101,7 @@ def check_error(self, code, lineno, fatal_error, *, 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 @@ def check_error(self, code, lineno, fatal_error, *, 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("") regex.append(fr'{header} \(most recent call first\):') - if garbage_collecting and not all_threads_disabled: - regex.append(' Garbage-collecting') - regex.append(fr' File "", line {lineno} in {function}') + if py_fatal_error and not know_current_thread: + regex.append(" ") + else: + if garbage_collecting and not all_threads_disabled: + regex.append(' Garbage-collecting') + regex.append(fr' File "", line {lineno} in {function}') regex = '\n'.join(regex) if other_regex: diff --git a/Python/pylifecycle.c b/Python/pylifecycle.c index ca3d6f5f91761f..ffae21302b8a2a 100644 --- a/Python/pylifecycle.c +++ b/Python/pylifecycle.c @@ -3026,13 +3026,6 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp, #ifndef Py_GIL_DISABLED _Py_DumpTracebackThreads(fd, interp, tstate); #else - if (tstate == NULL) - { - /* _Py_DumpTraceback() prints out a message when tstate is null, - * whereas _Py_DumpTracebackThreads() does not. Early-return for - * consistency. */ - return; - } _Py_DumpTraceback(fd, tstate); #endif } From f523a8384bfa267572aafe32e2a03a43a0ad69cd Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 12 Jan 2025 12:19:57 -0500 Subject: [PATCH 3/5] Add blurb --- .../next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst 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 index 00000000000000..b9c117b269434c --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2025-01-12-12-19-51.gh-issue-128400.OwoIDw.rst @@ -0,0 +1,2 @@ +:c:func:`Py_FatalError` no longer shows all threads on the :term:`free +threaded ` build to prevent crashes. From ee106a2fc9689cdcca8b45c3809746ac5ed41012 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 12 Jan 2025 19:20:41 -0500 Subject: [PATCH 4/5] Fix tests for the default build. --- Lib/test/test_faulthandler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_faulthandler.py b/Lib/test/test_faulthandler.py index 917d193ab5f76b..75d303cd212c82 100644 --- a/Lib/test/test_faulthandler.py +++ b/Lib/test/test_faulthandler.py @@ -118,7 +118,7 @@ def check_error(self, code, lineno, fatal_error, *, if all_threads_disabled and not py_fatal_error: regex.append("") regex.append(fr'{header} \(most recent call first\):') - if py_fatal_error and not know_current_thread: + if support.Py_GIL_DISABLED and py_fatal_error and not know_current_thread: regex.append(" ") else: if garbage_collecting and not all_threads_disabled: From af6a910321a557db346bf6aed7a8a98885d04555 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Sun, 12 Jan 2025 19:27:51 -0500 Subject: [PATCH 5/5] Fix C API tests. --- Lib/test/test_capi/test_misc.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_capi/test_misc.py b/Lib/test/test_capi/test_misc.py index ada30181aeeca9..dc1c92c4a95fa1 100644 --- a/Lib/test/test_capi/test_misc.py +++ b/Lib/test/test_capi/test_misc.py @@ -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 @@ def test_return_null_without_error(self): r'Python runtime state: initialized\n' r'SystemError: ' r'returned NULL without setting an exception\n' - r'\n' - r'Current thread.*:\n' + r'\n' + + CURRENT_THREAD_REGEX + r' File .*", line 6 in \n') else: with self.assertRaises(SystemError) as cm: @@ -268,8 +270,8 @@ def test_return_result_with_error(self): r'SystemError: ' 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 \n') else: with self.assertRaises(SystemError) as cm: @@ -298,8 +300,8 @@ def test_getitem_with_error(self): 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 \n' r'\n' r'Extension modules: _testcapi \(total: 1\)\n')