Skip to content

gh-128400: Only show the current thread in Py_FatalError on the free-threaded build #128758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -234,8 +236,8 @@ def test_return_null_without_error(self):
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:
Expand Down Expand Up @@ -268,8 +270,8 @@ def test_return_result_with_error(self):
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:
Expand Down Expand Up @@ -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 <module>\n'
r'\n'
r'Extension modules: _testcapi \(total: 1\)\n')
Expand Down
14 changes: 8 additions & 6 deletions Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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("<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:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:c:func:`Py_FatalError` no longer shows all threads on the :term:`free
threaded <free threading>` build to prevent crashes.
4 changes: 4 additions & 0 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -3023,7 +3023,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,
Expand Down
Loading