Skip to content

Faulthandler not enabled when pytest run under Python Dev Mode (-X dev) #8258

@thomascobb

Description

@thomascobb

Description

I use Python Dev Mode as a handy shortcut for enabling faulthandler, enabling debug mode of asyncio, logging resource leaks, and other goodies. Unfortunately, pytest's faulthandler plugin doesn't install itself if something else has already enabled the faulthandler. This means the output of the faulthandler is swallowed unless pytest is run with -s

Minimal example

Here is a file with 2 tests, one which fails with an asyncio error when run under dev mode, and another which occasionally seg faults. I would like to see the asyncio error when it doesn't segfault, and the stack trace when it does.

import ctypes
import asyncio
import threading
import time

def test_wrong_thread():
    loop = asyncio.get_event_loop()
    t = threading.Thread(target=loop.run_forever)
    t.setDaemon(True)
    t.start()
    loop.call_soon(print)

def test_segfault():
    if time.time() % 1 < 0.3:
        ctypes.string_at(0)

Running

$ python -X dev -m pytest

Gives the error from asyncio:

RuntimeError: Non-thread-safe operation invoked on an event loop other than the current one

But when it segfaults the stack trace is swallowed unless run with -s

Suggestion

Either unconditionally enabling faulthandler in the plugin (regardless of whether it was already enabled) or adding an option to do so would fix this, I am happy to provide a PR if this is a valid solution.
This naive solution fixes things for my example:

--- faulthandler.py	2021-01-20 09:42:57.611168438 +0000
+++ faulthandler_patch.py	2021-01-20 09:43:30.767501381 +0000
@@ -25,7 +25,7 @@
 def pytest_configure(config: Config) -> None:
     import faulthandler
 
-    if not faulthandler.is_enabled():
+    if True:
         # faulthhandler is not enabled, so install plugin that does the actual work
         # of enabling faulthandler before each test executes.
         config.pluginmanager.register(FaultHandlerHooks(), "faulthandler-hooks")

Environment

$ pip list
Package       Version
------------- ----------
appdirs       1.4.3
attrs         20.3.0
CacheControl  0.12.6
certifi       2019.11.28
chardet       3.0.4
colorama      0.4.3
contextlib2   0.6.0
distlib       0.3.0
distro        1.4.0
html5lib      1.0.1
idna          2.8
iniconfig     1.1.1
ipaddr        2.2.0
lockfile      0.12.2
msgpack       0.6.2
packaging     20.8
pep517        0.8.2
pip           20.0.2
pkg-resources 0.0.0
pluggy        0.13.1
progress      1.5
py            1.10.0
pyparsing     2.4.7
pytest        6.2.1
pytoml        0.1.21
requests      2.22.0
retrying      1.3.3
setuptools    44.0.0
six           1.14.0
toml          0.10.2
urllib3       1.25.8
webencodings  0.5.1
wheel         0.34.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions