qtconsole icon indicating copy to clipboard operation
qtconsole copied to clipboard

Faulthandler doesn't work

Open impact27 opened this issue 6 years ago • 1 comments

When writing a command that sefaults with faulthandler enabled, no messages are printed:

In [1]: import faulthandler; import ctypes; faulthandler.enable(); ctypes.string_at(0)

Kernel died, restarting

I would expect this to happen:

In [1]: import faulthandler; import ctypes; faulthandler.enable(); ctypes.string_at(0)

Fatal Python error: Segmentation fault

Current thread 0x000000010e0c4dc0 (most recent call first):
  File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/ctypes/__init__.py", line 489 in string_at
  File "<string>", line 1 in <module>

Kernel died, restarting

The correct behavior can be achieved with:

import subprocess

crash_str = "import faulthandler; import ctypes; faulthandler.enable(); ctypes.string_at(0)"

process = subprocess.Popen([
    'python3',
    '-c',
    crash_str],
    stderr=subprocess.PIPE)

print(process.communicate()[1].decode())

impact27 avatar Feb 05 '20 08:02 impact27

@impact27 i suspect this might be because of the way faulthandler works with stderr/stdout. We noticed similar behavior in our projects where enabling faulthandler might cause the application/kernel to crash because someone/somewhere hijacked the stderr/stdout.

One easy way around this is to provide a filehandle to faulthandler.enable() - that way, faulthandler prints to the file instead of using stdout/stderr, which might cause issues.

The way we debugged this issue was to check who/where the stderr/stdout was being modified and a) undo these modifications of python globals or 2) ensure that whatever stdout/stderr are replaced with follow the file protocol.

By default, the Python traceback is written to sys.stderr. To see tracebacks, applications must be run in the terminal. A log file can alternatively be passed to faulthandler.enable().

Ref : https://docs.python.org/3/library/faulthandler.html and https://github.com/python/cpython/blob/f02de961b9f19a5db0ead56305fe0057a78787ae/Modules/faulthandler.c#L143-L202

rahulporuri avatar Aug 17 '20 13:08 rahulporuri