Skip to content

The agent that grows with you

fix(tests): stop the code_kernel reader-thread leak that OOM-killed test workers; cap worker heap

teknium1439ebe0ae9f1
diff --git a/AGENTS.md b/AGENTS.md
index 28bd5356167..3ea098b15e2 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -340,7 +340,9 @@ scripts/run_tests.sh -v --tb=long # pytest flags pass thro
```
- **Flake policy:** a failing FILE is retried once in a fresh subprocess (`--file-retries`;
- `HERMES_TEST_FILE_RETRIES=0` disables). Pass-on-retry is green but printed under `⚠ FLAKY`
+ `HERMES_TEST_FILE_RETRIES=0` disables); a worker killed by signal or the file timeout is never
+ retried. Each worker runs under an 8 GiB heap cap on Linux (`HERMES_TEST_WORKER_MEM_GB`, 0
+ disables) so a runaway loop dies with a `MemoryError` traceback instead of swapping the host. Pass-on-retry is green but printed under `⚠ FLAKY`
with both outputs — a bug to fix, not noise. Timing tests must not assume a quiet runner:
wall-clock bounds ≥ 2s, event-based sync, no `assert not _wait_until(...)` races.
- **Placement mirrors the source tree.** A test lives in `tests/<top-level source dir>/` (`tests/hermes_cli/`,
diff --git a/scripts/run_tests.sh b/scripts/run_tests.sh
index 77eedc6d1cc..d2e9aedb495 100755
--- a/scripts/run_tests.sh
+++ b/scripts/run_tests.sh
@@ -143,7 +143,7 @@ done
# credential can leak" property stays auditable at a glance.
TEST_ENV=()
for _test_var in HERMES_TEST_IMAGE HERMES_TEST_WORKERS HERMES_TEST_PATHS \
- HERMES_TEST_FILE_TIMEOUT HERMES_TEST_FILE_RETRIES HERMES_TEST_SLICE; do
+ HERMES_TEST_FILE_TIMEOUT HERMES_TEST_FILE_RETRIES HERMES_TEST_WORKER_MEM_GB HERMES_TEST_SLICE; do
if [ -n "${!_test_var:-}" ]; then
TEST_ENV+=("$_test_var=${!_test_var}")
fi
diff --git a/scripts/run_tests_parallel.py b/scripts/run_tests_parallel.py
index af1c08f626d..5acf053d6b5 100755
--- a/scripts/run_tests_parallel.py
+++ b/scripts/run_tests_parallel.py
@@ -117,6 +117,13 @@ _DEFAULT_FILE_TIMEOUT_SECONDS = 300.0
# Set to 0 to disable (env: HERMES_TEST_FILE_RETRIES).
_DEFAULT_FILE_RETRIES = 1
+# Per-worker heap cap in GiB (Linux RLIMIT_DATA: brk + private anonymous mmap; inherited by the
+# worker's children). A runaway allocation loop then dies with a MemoryError traceback in seconds
+# instead of swapping the host for 20 GB with no stack (five OOM incidents from one leaking test
+# thread). RLIMIT_AS is deliberately NOT used: browsers spawned by tests reserve huge address
+# space. Set to 0 to disable (env: HERMES_TEST_WORKER_MEM_GB).
+_DEFAULT_WORKER_MEM_GB = 8.0
+
# Duration cache: maps relative file paths to last-observed subprocess
# wall-clock seconds. Used by ``--slice`` to distribute files across
# CI jobs by estimated total time, so no one job gets all the slow files.
@@ -414,6 +421,10 @@ def _run_one_file(
file, pytest_args, repo_root, file_timeout
)
attempt = 0
+ # A worker killed by signal (OOM, SIGKILL) or the file timeout is a runaway, not a flake:
+ # relaunching it doubles the damage while the first tree is still being reaped.
+ if rc < 0 or rc == 124:
+ retries = 0
while rc != 0 and attempt < retries:
attempt += 1
first_output = output
@@ -441,6 +452,25 @@ _FLAKY_RESULTS: List[Tuple[Path, str]] = []
_flaky_lock = threading.Lock()
+def _worker_memory_cap():
+ """``preexec_fn`` applying the per-worker heap cap, or None when disabled / unsupported."""
+ if sys.platform != "linux":
+ return None
+ try:
+ gib = float(os.environ.get("HERMES_TEST_WORKER_MEM_GB", _DEFAULT_WORKER_MEM_GB))
+ except ValueError:
+ gib = _DEFAULT_WORKER_MEM_GB
+ if gib <= 0:
+ return None
+ limit = int(gib * (1 << 30))
+
+ def _apply():
+ import resource
+ resource.setrlimit(resource.RLIMIT_DATA, (limit, limit))
+
+ return _apply
+
+
def _run_one_file_once(
file: Path,
pytest_args: List[str],
@@ -482,6 +512,7 @@ def _run_one_file_once(
stderr=subprocess.STDOUT,
text=True, encoding="utf-8", errors="replace",
env=env,
+ preexec_fn=_worker_memory_cap(),
# POSIX: place the child at the head of its own process group so
# _kill_tree can SIGKILL the group atomically.
# Windows: this maps to CREATE_NEW_PROCESS_GROUP in CPython 3.12+;
diff --git a/tests/tools/test_local_env_blocklist.py b/tests/tools/test_local_env_blocklist.py
index 3f6b062c515..4fefee77809 100644
--- a/tests/tools/test_local_env_blocklist.py
+++ b/tests/tools/test_local_env_blocklist.py
@@ -976,8 +976,13 @@ class TestPythonpathSelectiveStrip:
captured["env"] = kwargs.get("env", {})
captured["staging"] = os.path.dirname(cmd[1])
proc = MagicMock()
+ # The kernel's reader threads drain with read1(); a bare MagicMock never returns
+ # EOF there, so the stderr thread spins forever appending mocks (a 1 GB/min leak
+ # that outlived the test and OOM-killed the worker five times).
proc.stdout.read.return_value = b""
+ proc.stdout.read1.return_value = b""
proc.stderr.read.return_value = b""
+ proc.stderr.read1.return_value = b""
proc.wait.return_value = 0
proc.returncode = 0
proc.poll.return_value = 0