-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
69 lines (55 loc) · 2.26 KB
/
conftest.py
File metadata and controls
69 lines (55 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import os
import resource
import signal
import sys
import threading
# ---------------------------------------------------------------------------
# Memory guard: limit to 32GB to prevent OOM-killing tmux/codex
# ---------------------------------------------------------------------------
_MAX_RSS_BYTES = 32 * 1024 * 1024 * 1024 # 32GB
try:
resource.setrlimit(resource.RLIMIT_AS, (_MAX_RSS_BYTES, _MAX_RSS_BYTES))
except (ValueError, resource.error):
pass # Some systems don't support RLIMIT_AS
# ---------------------------------------------------------------------------
# Hard watchdog: kill the process if a single test hangs beyond timeout.
#
# pytest-timeout uses signal or thread method, but neither can reliably
# interrupt code stuck inside C extensions (like the Rust VM). This watchdog
# is the last resort — it kills the entire process with SIGKILL.
#
# The watchdog resets at the start of each test (via the pytest hook).
# If no test starts within WATCHDOG_TIMEOUT seconds, the process dies.
# ---------------------------------------------------------------------------
_WATCHDOG_TIMEOUT = int(os.environ.get("PYTEST_WATCHDOG_TIMEOUT", "90"))
_watchdog_timer: threading.Timer | None = None
def _watchdog_kill():
"""Last resort: kill the process if a test hangs beyond all timeouts."""
print(
f"\n\nWATCHDOG: Test hung for {_WATCHDOG_TIMEOUT}s beyond all timeouts. "
f"Killing process with SIGKILL.\n",
file=sys.stderr,
flush=True,
)
os.kill(os.getpid(), signal.SIGKILL)
def _reset_watchdog():
global _watchdog_timer
if _watchdog_timer is not None:
_watchdog_timer.cancel()
_watchdog_timer = threading.Timer(_WATCHDOG_TIMEOUT, _watchdog_kill)
_watchdog_timer.daemon = True
_watchdog_timer.start()
def _stop_watchdog():
global _watchdog_timer
if _watchdog_timer is not None:
_watchdog_timer.cancel()
_watchdog_timer = None
def pytest_runtest_setup(item):
"""Reset watchdog at the start of each test."""
_reset_watchdog()
def pytest_runtest_teardown(item, nextitem):
"""Reset watchdog after each test (covers slow teardown)."""
_reset_watchdog()
def pytest_sessionfinish(session, exitstatus):
"""Stop watchdog when pytest finishes."""
_stop_watchdog()