faulthandler — Dump the Python traceback

New in version 3.3.


This module contains functions to dump Python tracebacks explicitly, on a fault,after a timeout, or on a user signal. Callfaulthandler.enable() toinstall fault handlers for theSIGSEGV,SIGFPE,SIGABRT,SIGBUS, andSIGILL signals. You can alsoenable them at startup by setting thePYTHONFAULTHANDLER environmentvariable or by using the-Xfaulthandler command line option.

The fault handler is compatible with system fault handlers like Apport or theWindows fault handler. The module uses an alternative stack for signal handlersif thesigaltstack() function is available. This allows it to dump thetraceback even on a stack overflow.

The fault handler is called on catastrophic cases and therefore can only usesignal-safe functions (e.g. it cannot allocate memory on the heap). Because ofthis limitation traceback dumping is minimal compared to normal Pythontracebacks:

  • Only ASCII is supported. Thebackslashreplace error handler is used onencoding.

  • Each string is limited to 500 characters.

  • Only the filename, the function name and the line number aredisplayed. (no source code)

  • It is limited to 100 frames and 100 threads.

  • The order is reversed: the most recent call is shown first.

By default, the Python traceback is written tosys.stderr. To seetracebacks, applications must be run in the terminal. A log file canalternatively be passed tofaulthandler.enable().

The module is implemented in C, so tracebacks can be dumped on a crash or whenPython is deadlocked.

Dumping the traceback

faulthandler.dump_traceback(file=sys.stderr,all_threads=True)

Dump the tracebacks of all threads intofile. Ifall_threads isFalse, dump only the current thread.

Changed in version 3.5:Added support for passing file descriptor to this function.

Fault handler state

faulthandler.enable(file=sys.stderr,all_threads=True)

Enable the fault handler: install handlers for theSIGSEGV,SIGFPE,SIGABRT,SIGBUS andSIGILLsignals to dump the Python traceback. Ifall_threads isTrue,produce tracebacks for every running thread. Otherwise, dump only the currentthread.

Thefile must be kept open until the fault handler is disabled: seeissue with file descriptors.

Changed in version 3.5:Added support for passing file descriptor to this function.

Changed in version 3.6:On Windows, a handler for Windows exception is also installed.

faulthandler.disable()

Disable the fault handler: uninstall the signal handlers installed byenable().

faulthandler.is_enabled()

Check if the fault handler is enabled.

Dumping the tracebacks after a timeout

faulthandler.dump_traceback_later(timeout,repeat=False,file=sys.stderr,exit=False)

Dump the tracebacks of all threads, after a timeout oftimeout seconds, oreverytimeout seconds ifrepeat isTrue. Ifexit isTrue, call_exit() with status=1 after dumping the tracebacks. (Note_exit() exits the process immediately, which means it doesn’t do anycleanup like flushing file buffers.) If the function is called twice, the newcall replaces previous parameters and resets the timeout. The timer has asub-second resolution.

Thefile must be kept open until the traceback is dumped orcancel_dump_traceback_later() is called: seeissue with filedescriptors.

This function is implemented using a watchdog thread.

Changed in version 3.7:This function is now always available.

Changed in version 3.5:Added support for passing file descriptor to this function.

faulthandler.cancel_dump_traceback_later()

Cancel the last call todump_traceback_later().

Dumping the traceback on a user signal

faulthandler.register(signum,file=sys.stderr,all_threads=True,chain=False)

Register a user signal: install a handler for thesignum signal to dumpthe traceback of all threads, or of the current thread ifall_threads isFalse, intofile. Call the previous handler if chain isTrue.

Thefile must be kept open until the signal is unregistered byunregister(): seeissue with file descriptors.

Not available on Windows.

Changed in version 3.5:Added support for passing file descriptor to this function.

faulthandler.unregister(signum)

Unregister a user signal: uninstall the handler of thesignum signalinstalled byregister(). ReturnTrue if the signal was registered,False otherwise.

Not available on Windows.

Issue with file descriptors

enable(),dump_traceback_later() andregister() keep thefile descriptor of theirfile argument. If the file is closed and its filedescriptor is reused by a new file, or ifos.dup2() is used to replacethe file descriptor, the traceback will be written into a different file. Callthese functions again each time that the file is replaced.

Example

Example of a segmentation fault on Linux with and without enabling the faulthandler:

$python3-c"import ctypes; ctypes.string_at(0)"Segmentation fault$python3-q-Xfaulthandler>>> import ctypes>>> ctypes.string_at(0)Fatal Python error: Segmentation faultCurrent thread 0x00007fb899f39700 (most recent call first):  File "/home/python/cpython/Lib/ctypes/__init__.py", line 486 in string_at  File "<stdin>", line 1 in <module>Segmentation fault