Skip to content

Commit d6f11fa

Browse files
committed
Add max_threads kwarg to faulthandler.register() too
Per @vstinner's follow-up: also wire the kwarg through register() / faulthandler_user. Stored in the per-signal user_signal_t struct; the user-signal handler now passes user->max_threads to faulthandler_dump_traceback instead of 0. Doc, whatsnew, and NEWS entries updated to list all four functions.
1 parent 553c3a4 commit d6f11fa

6 files changed

Lines changed: 56 additions & 19 deletions

File tree

Doc/library/faulthandler.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,12 @@ Dumping the tracebacks after a timeout
189189
Dumping the traceback on a user signal
190190
--------------------------------------
191191

192-
.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False)
192+
.. function:: register(signum, file=sys.stderr, all_threads=True, chain=False, *, max_threads=100)
193193

194194
Register a user signal: install a handler for the *signum* signal to dump
195195
the traceback of all threads, or of the current thread if *all_threads* is
196196
``False``, into *file*. Call the previous handler if chain is ``True``.
197+
*max_threads* caps the number of threads dumped.
197198

198199
The *file* must be kept open until the signal is unregistered by
199200
:func:`unregister`: see :ref:`issue with file descriptors <faulthandler-fd>`.
@@ -203,6 +204,9 @@ Dumping the traceback on a user signal
203204
.. versionchanged:: 3.5
204205
Added support for passing file descriptor to this function.
205206

207+
.. versionchanged:: next
208+
Added the *max_threads* keyword argument.
209+
206210
.. function:: unregister(signum)
207211

208212
Unregister a user signal: uninstall the handler of the *signum* signal

Doc/whatsnew/3.15.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -898,7 +898,8 @@ faulthandler
898898
------------
899899

900900
* Added the *max_threads* parameter in :func:`faulthandler.enable`,
901-
:func:`faulthandler.dump_traceback`, and :func:`faulthandler.dump_traceback_later`.
901+
:func:`faulthandler.dump_traceback`, :func:`faulthandler.dump_traceback_later`,
902+
and :func:`faulthandler.register`.
902903
(Contributed by Eric Froemling in :gh:`149085`.)
903904

904905

Include/internal/pycore_faulthandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ struct faulthandler_user_signal {
4242
int chain;
4343
_Py_sighandler_t previous;
4444
PyInterpreterState *interp;
45+
Py_ssize_t max_threads;
4546
};
4647
#endif /* FAULTHANDLER_USER */
4748

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
Add a *max_threads* keyword argument to :func:`faulthandler.dump_traceback`,
2-
:func:`faulthandler.dump_traceback_later`, and :func:`faulthandler.enable`.
2+
:func:`faulthandler.dump_traceback_later`, :func:`faulthandler.enable`, and
3+
:func:`faulthandler.register`.

Modules/clinic/faulthandler.c.h

Lines changed: 36 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Modules/faulthandler.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,8 @@ faulthandler_user(int signum)
961961
if (!user->enabled)
962962
return;
963963

964-
faulthandler_dump_traceback(user->fd, user->all_threads, user->interp, 0);
964+
faulthandler_dump_traceback(user->fd, user->all_threads, user->interp,
965+
user->max_threads);
965966

966967
#ifdef HAVE_SIGACTION
967968
if (user->chain) {
@@ -1011,17 +1012,21 @@ faulthandler.register as faulthandler_register_py
10111012
file: object(py_default="sys.stderr") = NULL
10121013
all_threads: bool = True
10131014
chain: bool = False
1015+
*
1016+
max_threads: Py_ssize_t = 100
10141017
10151018
Register a handler for the signal 'signum'.
10161019
10171020
Dump the traceback of the current thread, or of all threads if
1018-
all_threads is True, into file.
1021+
all_threads is True, into file. max_threads caps the number of threads
1022+
dumped.
10191023
[clinic start generated code]*/
10201024

10211025
static PyObject *
10221026
faulthandler_register_py_impl(PyObject *module, int signum, PyObject *file,
1023-
int all_threads, int chain)
1024-
/*[clinic end generated code: output=1f770cee150a56cd input=ae9de829e850907b]*/
1027+
int all_threads, int chain,
1028+
Py_ssize_t max_threads)
1029+
/*[clinic end generated code: output=d63a5b4f388dee5f input=c75096a20de502fe]*/
10251030
{
10261031
int fd;
10271032
user_signal_t *user;
@@ -1072,6 +1077,7 @@ faulthandler_register_py_impl(PyObject *module, int signum, PyObject *file,
10721077
user->all_threads = all_threads;
10731078
user->chain = chain;
10741079
user->interp = PyThreadState_GetInterpreter(tstate);
1080+
user->max_threads = max_threads;
10751081
user->enabled = 1;
10761082

10771083
Py_RETURN_NONE;

0 commit comments

Comments
 (0)