35

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?

sophros
  • 14,672
  • 11
  • 46
  • 75
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
  • 2
    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. – Bakuriu Feb 12 '14 at 17:19

1 Answers1

58

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.

vstinner
  • 1,274
  • 14
  • 7