Skip to content

Commit

Permalink
faulthandler: don't dump all threads when running without the GIL
Browse files Browse the repository at this point in the history
It's not safe to access other threads stacks when running without the GIL.
  • Loading branch information
colesbury committed Apr 23, 2023
1 parent 964bb33 commit 9ab9696
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Lib/test/support/script_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def interpreter_requires_environment():
global __cached_interp_requires_environment
if __cached_interp_requires_environment is None:
# If PYTHONHOME is set, assume that we need it
if 'PYTHONHOME' in os.environ:
if 'PYTHONHOME' in os.environ or 'PYTHONGIL' in os.environ:
__cached_interp_requires_environment = True
return True
# cannot run subprocess, assume we don't need it
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def test_getitem_with_error(self):
r'Python runtime state: initialized\n'
r'ValueError: bug\n'
r'\n'
r'Current thread .* \(most recent call first\):\n'
r'(Current thread|Stack).* \(most recent call first\):\n'
r' File .*, line 6 in <module>\n'
r'\n'
r'Extension modules: _testcapi \(total: 1\)\n')
Expand Down
14 changes: 10 additions & 4 deletions Lib/test/test_faulthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,21 +95,25 @@ def check_error(self, code, lineno, fatal_error, *,
Raise an error if the output doesn't match the expected format.
"""
if sys.flags.nogil:
all_threads = False
if all_threads:
if know_current_thread:
header = 'Current thread 0x[0-9a-f]+'
else:
header = 'Thread 0x[0-9a-f]+'
else:
elif know_current_thread:
header = 'Stack'
regex = [f'^{fatal_error}']
if py_fatal_error:
regex.append("Python runtime state: initialized")
regex.append('')
regex.append(fr'{header} \(most recent call first\):')
if garbage_collecting:
if all_threads or know_current_thread:
regex.append(fr'{header} \(most recent call first\):')
if garbage_collecting and all_threads:
regex.append(' Garbage-collecting')
regex.append(fr' File "<string>", line {lineno} in {function}')
if all_threads or know_current_thread:
regex.append(fr' File "<string>", line {lineno} in {function}')
regex = '\n'.join(regex)

if other_regex:
Expand Down Expand Up @@ -691,6 +695,8 @@ def check_register(self, filename=False, all_threads=False,
Raise an error if the output doesn't match the expected format.
"""
if sys.flags.nogil:
all_threads = False
signum = signal.SIGUSR1
code = """
import faulthandler
Expand Down
5 changes: 5 additions & 0 deletions Modules/faulthandler.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ faulthandler_dump_traceback(int fd, int all_threads,

reentrant = 1;

if (_PyRuntime.preconfig.disable_gil) {
// It's not safe to dump all threads when running without the GIL.
all_threads = 0;
}

/* SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals and
are thus delivered to the thread that caused the fault. Get the Python
thread state of the current thread.
Expand Down
9 changes: 8 additions & 1 deletion Python/pylifecycle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2559,7 +2559,14 @@ _Py_FatalError_DumpTracebacks(int fd, PyInterpreterState *interp,
PUTS(fd, "\n");

/* display the current Python stack */
_Py_DumpTracebackThreads(fd, interp, tstate);
if (_PyRuntime.preconfig.disable_gil) {
if (tstate != NULL) {
_Py_DumpTraceback(fd, tstate);
}
}
else {
_Py_DumpTracebackThreads(fd, interp, tstate);
}
}

/* Print the current exception (if an exception is set) with its traceback,
Expand Down

0 comments on commit 9ab9696

Please sign in to comment.