python: Is there a downside to using faulthandler?
Asked Answered
R

1

39

Python 3.3 includes a module named faulthandler that displays helpful traceback information if a segfault occurs. (For Python versions prior to 3.3, the module can be obtained from PyPI.)

The module is not enabled by default. It is enabled like this:

import faulthandler
faulthandler.enable()

This feature is very useful. Is there any particular reason it isn't enabled by default? Does it have any negative effects on performance?

Ribose answered 12/2, 2014 at 16:34 Comment(1)
It changes the behaviour of programs, i.e. enabling it by default would be a backward incompatible change, although a good one. Still there could be programs that rely on current behaviour and that could break when using faulthandler. Note that you can enable the module via command-line options if you want to.Hedley
N
64

This feature is very useful. Is there any particular reason it isn't enabled by default? Does it have any negative effects on performance?

The reason is that faulthandler remembers the file descriptor of stderr, usually fd 2. The problem is that fd 2 may become something else, like a socket, a pipe, an important file, etc. There is no reliable way to detect this situation, and so it's safer to not enable faulthandler by default in Python.

faulthandler is safe in almost all cases, except when a file descriptor stored by faulthandler is replaced. Problem also described in the doc: https://docs.python.org/dev/library/faulthandler.html#issue-with-file-descriptors

Note: I wrote faulthandler.

Nettles answered 25/3, 2015 at 3:36 Comment(4)
how difficult is it to show more than 100 threads of stacktrace?Gavotte
faulthandler is limited to only 100 threads and I want to show more if possible.Gavotte
Currently, it's not possible. I suggest you to open a feature request on the Python bug tracker: github.com/python/cpython/issuesNettles
@Nettles Sometimes faulthandler does not print current thread information. Any reason why it could be happening like thisBurrstone

© 2022 - 2024 — McMap. All rights reserved.