diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 9b8c77e2b0401f..93ae9b16766df5 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -945,7 +945,32 @@ faulthandler_user(int signum) if (!user->enabled) return; + int attach = 0; + PyThreadState *tstate = PyThreadState_GetUnchecked(); + if (!tstate) { + tstate = PyGILState_GetThisThreadState(); + if (tstate) { + attach = 1; + } + } + + if (attach) { + _PyThreadState_Attach(tstate); + } + + if (user->all_threads) { + assert (user->interp); + _PyEval_StopTheWorld(user->interp); + } faulthandler_dump_traceback(user->fd, user->all_threads, user->interp); + if (user->all_threads) { + assert (user->interp); + _PyEval_StartTheWorld(user->interp); + } + + if (attach) { + _PyThreadState_Detach(tstate); + } #ifdef HAVE_SIGACTION if (user->chain) {