Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
include MANIFEST.in
include LICENSE
include test_pytest_timeout.py
include test_pytest_timeout_samples.py
include failure_demo.py
include tox.ini
43 changes: 43 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,47 @@ Setting a timeout to 0 seconds disables the timeout, so if you have a
global timeout set you can still disable the timeout by using the
mark.

Slow-test samples
=================

Use ``--slow-test-sample=SECONDS`` to collect a non-fatal stack sample when
a test runs longer than a separate threshold. Sampling is disabled by
default and works independently of the hard timeout. For example, write
a sample after 15 seconds and interrupt the test after 60 seconds::

pytest --slow-test-sample=15 --timeout=60

Each slow test writes one UTF-8 ``.log`` file to a ``pytest-timeout``
subdirectory of the system temporary directory, such as
``/tmp/pytest-timeout/`` on Linux. ``--slow-test-sample-dir=PATH`` selects
another directory; relative paths are resolved from the working directory.
The filename contains a sanitized test name and a
unique suffix, and the file includes the complete test node ID and
sampling threshold. Repeated tests and parallel workers do not overwrite
each other's samples.

Samples reuse the same thread stack dumper as timeout output. They are
written directly to files without changing pytest's output capture or
interrupting the test. Sampling includes setup and teardown unless
``timeout_func_only`` or the timeout marker's ``func_only`` option is
enabled. Sampling also respects debugger detection and its disable setting.

Both settings are available in the pytest configuration file:

.. code:: ini

[pytest]
slow_test_sample = 15
slow_test_sample_dir = /tmp/custom-test-samples

Command-line values override the configuration file.
``--slow-test-sample=0`` disables sampling, while ``--timeout=0`` disables
only the hard timeout. A sample is taken once per slow test. Timers are
cancelled at test completion, and any in-progress sample finishes before
the next test starts. Output errors are reported to stderr without
failing the test. Set the sampling threshold earlier than a hard timeout
to allow the file to be written before the test or process is interrupted.

Timeout Methods
===============

Expand Down Expand Up @@ -395,6 +436,8 @@ Changelog
X.Y.Z
-----

- Add non-fatal slow-test stack samples using ``--slow-test-sample`` and
``--slow-test-sample-dir``, reusing the timeout diagnostics.
- Preserve capture-fixture output and disable fixture capturing when reporting
thread timeouts. Thanks Arham Begani.
- Add support Python3.15. Thanks Vladimir Roshchin.
Expand Down
135 changes: 125 additions & 10 deletions pytest_timeout.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,27 @@
"""

import inspect
import math
import os
import re
import signal
import sys
import tempfile
import threading
import time
import traceback
from collections import namedtuple
from contextlib import ExitStack
from pathlib import Path

import pytest
from _pytest._io import TerminalWriter

__all__ = ("Settings", "is_debugging")
SESSION_TIMEOUT_KEY = pytest.StashKey[float]()
SESSION_EXPIRE_KEY = pytest.StashKey[float]()
SAMPLE_SECONDS_KEY = pytest.StashKey[float]()
SAMPLE_DIR_KEY = pytest.StashKey[Path]()
PYTEST_FAILURE_MESSAGE = "Timeout (>%ss) from pytest-timeout."

HAVE_SIGALRM = hasattr(signal, "SIGALRM")
Expand Down Expand Up @@ -49,6 +56,14 @@
means no timeout. Timeout is checked between tests, and will not interrupt a test
in progress.
""".strip()
SAMPLE_DESC = """
Seconds before writing a non-fatal sample of the timeout stack diagnostics.
Default is 0 which disables sampling independently of the timeout.
""".strip()
SAMPLE_DIR_DESC = """
Directory for slow-test stack samples. Default is pytest-timeout under the
system temporary directory.
""".strip()

# bdb covers pdb, ipdb, and possibly others
# pydevd covers PyCharm, VSCode, and possibly others
Expand All @@ -66,6 +81,10 @@ def pytest_addoption(parser):
"Interrupt test run and dump stacks of all threads after a test times out",
)
group.addoption("--timeout", type=float, help=TIMEOUT_DESC)
group.addoption(
"--slow-test-sample", type=float, metavar="SECONDS", help=SAMPLE_DESC
)
group.addoption("--slow-test-sample-dir", metavar="PATH", help=SAMPLE_DIR_DESC)
group.addoption(
"--timeout_method",
action="store",
Expand Down Expand Up @@ -105,6 +124,12 @@ def pytest_addoption(parser):
default=False,
)
parser.addini("session_timeout", SESSION_TIMEOUT_DESC, type="float")
parser.addini("slow_test_sample", SAMPLE_DESC, type="float", default=0)
parser.addini(
"slow_test_sample_dir",
SAMPLE_DIR_DESC,
default=str(Path(tempfile.gettempdir()) / "pytest-timeout"),
)


class TimeoutHooks:
Expand Down Expand Up @@ -160,6 +185,19 @@ def pytest_configure(config):
config._env_timeout_func_only = settings.func_only
config._env_timeout_disable_debugger_detection = settings.disable_debugger_detection

sample_seconds = config.getoption("slow_test_sample")
if sample_seconds is None:
sample_seconds = config.getini("slow_test_sample")
if not math.isfinite(sample_seconds) or sample_seconds < 0:
raise pytest.UsageError(
"slow_test_sample must be a finite, non-negative number"
)
config.stash[SAMPLE_SECONDS_KEY] = sample_seconds
config.stash[SAMPLE_DIR_KEY] = Path(
config.getoption("slow_test_sample_dir")
or config.getini("slow_test_sample_dir")
).absolute()

timeout = config.getoption("session_timeout")
if timeout is None:
ini = config.getini("session_timeout")
Expand All @@ -185,11 +223,19 @@ def pytest_runtest_protocol(item):
hooks = item.config.pluginmanager.hook
settings = _get_item_settings(item)
is_timeout = settings.timeout is not None and settings.timeout > 0
if is_timeout and settings.func_only is False:
hooks.pytest_timeout_set_timer(item=item, settings=settings)
yield
if is_timeout and settings.func_only is False:
hooks.pytest_timeout_cancel_timer(item=item)
sample_timer = None
try:
if settings.func_only is False:
sample_timer = _start_sample_timer(item, settings)
if is_timeout:
hooks.pytest_timeout_set_timer(item=item, settings=settings)
yield
finally:
try:
if is_timeout and settings.func_only is False:
hooks.pytest_timeout_cancel_timer(item=item)
finally:
_cancel_sample_timer(sample_timer)

# check session timeout
expire_time = item.session.config.stash[SESSION_EXPIRE_KEY]
Expand All @@ -208,11 +254,19 @@ def pytest_runtest_call(item):
hooks = item.config.pluginmanager.hook
settings = _get_item_settings(item)
is_timeout = settings.timeout is not None and settings.timeout > 0
if is_timeout and settings.func_only is True:
hooks.pytest_timeout_set_timer(item=item, settings=settings)
yield
if is_timeout and settings.func_only is True:
hooks.pytest_timeout_cancel_timer(item=item)
sample_timer = None
try:
if settings.func_only is True:
sample_timer = _start_sample_timer(item, settings)
if is_timeout:
hooks.pytest_timeout_set_timer(item=item, settings=settings)
yield
finally:
try:
if is_timeout and settings.func_only is True:
hooks.pytest_timeout_cancel_timer(item=item)
finally:
_cancel_sample_timer(sample_timer)


@pytest.hookimpl(tryfirst=True)
Expand All @@ -230,6 +284,12 @@ def pytest_report_header(config):
session_timeout = config.getoption("session_timeout")
if session_timeout:
timeout_header.append(f"session timeout: {session_timeout}s")
sample_seconds = config.stash[SAMPLE_SECONDS_KEY]
if sample_seconds:
timeout_header.append(
f"slow test sample: {sample_seconds}s\n"
f"slow test sample directory: {config.stash[SAMPLE_DIR_KEY]}"
)
if timeout_header:
return timeout_header
return None
Expand Down Expand Up @@ -544,6 +604,61 @@ def timeout_timer(item, settings):
os._exit(1)


def _start_sample_timer(item, settings):
"""Start an independent, one-shot diagnostic timer, if enabled."""
seconds = item.config.stash[SAMPLE_SECONDS_KEY]
if not seconds:
return None
timer = threading.Timer(
seconds,
_sample_stacks,
(item.nodeid, seconds, item.config.stash[SAMPLE_DIR_KEY], settings),
)
timer.name = f"{__name__} sample {item.nodeid}"
timer.daemon = True
timer.start()
return timer


def _cancel_sample_timer(timer):
if timer is not None:
timer.cancel()
# Finish any in-progress write before advancing to another test.
timer.join()


def _sample_stacks(nodeid, seconds, directory, settings):
"""Write the normal timeout diagnostics without interrupting the test."""
if not settings.disable_debugger_detection and is_debugging():
return
try:
directory.mkdir(parents=True, exist_ok=True)
path, separator, test = nodeid.partition("::")
name = re.sub(
r"[^A-Za-z0-9_.-]", "_", path.rsplit("/", 1)[-1] + separator + test
)[:120]
# Unique suffixes prevent overwrites across parametrizations, repeated
# runs and parallel workers; bounded names remain portable.
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
prefix=f"{name}-",
suffix=".log",
dir=directory,
delete=False,
) as stream:
terminal = TerminalWriter(file=stream)
terminal.sep("+", title=f"Slow test sample after {seconds}s")
terminal.line(f"Test: {nodeid}")
dump_stacks(terminal)
terminal.sep("+", title="Slow test sample")
except Exception:
# A failed sample must not cause an unhandled timer exception or fail
# the test. Leave the independent hard timeout in control.
sys.stderr.write(f"pytest-timeout: could not write stack sample for {nodeid}\n")
traceback.print_exc()


def dump_stacks(terminal):
"""Dump the stacks of all threads except the current thread."""
current_ident = threading.current_thread().ident
Expand Down
Loading
Loading