From e62332b4c6a44fe3b22cf9f3fe0aadef4c9c0f2b Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 22:19:43 +0100 Subject: [PATCH] fix: tests rerun what autorun retries run_tests reran a module only when a signal killed it: a SystemError or an unknown opcode from this host's CPU failed the run, where an autorun step failing the same way ran again. host_misread moves to core.hostguard and both runners read it, a signal kill of any kind now counting for autorun too. --- scripts/core/hostguard.py | 14 +++++++++++++- scripts/devtools/testrunner.py | 12 +++++++++--- scripts/run_tests.py | 9 ++++++--- scripts/unattended/runner.py | 11 +---------- tests/test_autorun.py | 27 +++++++++++++++++++++++++++ wiki/testing-guide.md | 3 ++- wiki/tools.md | 2 +- 7 files changed, 59 insertions(+), 19 deletions(-) diff --git a/scripts/core/hostguard.py b/scripts/core/hostguard.py index 8612f9e93..2665330a5 100644 --- a/scripts/core/hostguard.py +++ b/scripts/core/hostguard.py @@ -1,4 +1,4 @@ -"""Host-wide guards every heavy job shares: the automation lock and free disk space.""" +"""Host-wide guards every heavy job shares: the automation lock, free disk space and the misread rule.""" from __future__ import annotations @@ -12,6 +12,9 @@ from pathlib import Path DEFAULT_LOCK = Path.home() / ".local" / "state" / "retrobios" / "auto.lock" LOCK_ENV = "RETROBIOS_AUTO_LOCK" LOCK_HELD_ENV = "RETROBIOS_AUTO_LOCK_HELD" +# A segfault as a shell reports it. Python reports the signal as -11. +SHELL_SEGFAULT = 139 +HOST_MISREAD_MARKERS = ("SystemError", "unknown opcode") class LockBusy(RuntimeError): @@ -49,3 +52,12 @@ def lock_busy(path: Path) -> bool: def free_gb(path: Path) -> float: return shutil.disk_usage(path).free / 1024 ** 3 + + +def host_misread(returncode: int, text: str) -> bool: + """A failure this host's CPU produces under load, worth one retry: a signal + kill, a segfault a shell reported, or an error sound hardware never raises. + """ + if returncode < 0 or returncode == SHELL_SEGFAULT: + return True + return returncode != 0 and any(marker in text for marker in HOST_MISREAD_MARKERS) diff --git a/scripts/devtools/testrunner.py b/scripts/devtools/testrunner.py index 523282650..dc7c99357 100644 --- a/scripts/devtools/testrunner.py +++ b/scripts/devtools/testrunner.py @@ -13,6 +13,7 @@ from concurrent.futures import ThreadPoolExecutor, as_completed from dataclasses import dataclass from pathlib import Path +from core.hostguard import host_misread from core.paths import REPO_ROOT as ROOT TESTS = ROOT / "tests" @@ -116,9 +117,9 @@ def run_all(order: Iterable[str], jobs: int, env: dict[str, str], logs: Path, def rerun_crashed(results: Iterable[Result], env: dict[str, str], logs: Path, say: Callable[[str], None]) -> None: - """A module a signal killed is the host's doing until it fails alone.""" + """A module the host misread is the host's doing until it fails alone.""" for result in results: - if result.code < 0: + if result.code != 0 and host_misread(result.code, _log_text(result)): say(f" again, alone: {result.module}") again = run_module(result.module, env, logs) result.crashed_then = again.code @@ -126,9 +127,14 @@ def rerun_crashed(results: Iterable[Result], env: dict[str, str], logs: Path, again.tests, again.failures, again.errors, again.skipped) +def _log_text(result: Result) -> str: + return result.log.read_text(encoding="utf-8", errors="replace") if result.log else "" + + def save_durations(durations: dict[str, float], results: Iterable[Result]) -> None: """The timings of this run over the previous ones, a crashed run left out.""" - timed = {**durations, **{r.module: round(r.seconds, 1) for r in results if r.code >= 0}} + timed = {**durations, **{r.module: round(r.seconds, 1) for r in results + if r.crashed_then is None}} DURATIONS.write_text(json.dumps(timed, indent=1, sort_keys=True), encoding="utf-8") diff --git a/scripts/run_tests.py b/scripts/run_tests.py index a12439a2e..e456b6ff6 100644 --- a/scripts/run_tests.py +++ b/scripts/run_tests.py @@ -15,8 +15,9 @@ tmp/test-durations.json, from the previous run, and a module never timed is weighed by its size. The host computes wrong and Python segfaults under load: a module killed by -a signal is run again alone at the end, and reported as passing only if that -run passes. Every module's output is kept in its own log, and the summary +a signal, or failing with an error sound hardware never raises (SystemError, +unknown opcode), is run again alone at the end, and reported as passing only +if that run passes. Every module's output is kept in its own log, and the summary counts the tests, the failures and the skips, a skip being no proof. """ @@ -73,7 +74,9 @@ def main() -> int: f"{time.monotonic() - started:.0f} s, {skipped} skipped") for result in crashed: outcome = "passed" if result.crashed_then == 0 else "failed" - print(f" {result.module}: killed by signal {-result.code}, then {outcome} alone") + why = (f"killed by signal {-result.code}" if result.code < 0 + else f"exit {result.code} on a host misread") + print(f" {result.module}: {why}, then {outcome} alone") for result in failed: print(f"FAILED {result.module}: {result.failures} failures, {result.errors} errors " f"(code {result.code if result.crashed_then is None else result.crashed_then}), " diff --git a/scripts/unattended/runner.py b/scripts/unattended/runner.py index 5d51abe8b..ee4645a9b 100644 --- a/scripts/unattended/runner.py +++ b/scripts/unattended/runner.py @@ -18,7 +18,7 @@ from dataclasses import dataclass, field from pathlib import Path from core.artifacts import TIMESTAMP_KEYS -from core.hostguard import LockBusy, free_gb, global_lock, lock_busy +from core.hostguard import LockBusy, free_gb, global_lock, host_misread, lock_busy from core.paths import AUTORUN_DIR, AUTORUN_WORKTREE from core.statefile import now_iso, save_state from unattended.breaker import may_push, refresh_breaker @@ -37,8 +37,6 @@ from unattended.worktree import ( ALERT_FREE_GB = 100 LOG_DAYS = 30 -SEGFAULT_CODES = (139, -11) -HOST_MISREAD_MARKERS = ("SystemError", "unknown opcode") @dataclass @@ -101,13 +99,6 @@ def wait_for_load(host: Host, max_wait: float) -> bool: host.sleep(min(host.poll, left)) -def host_misread(returncode: int, text: str) -> bool: - """A failure this host's CPU produces under load, worth one idle retry.""" - if returncode in SEGFAULT_CODES: - return True - return returncode != 0 and any(marker in text for marker in HOST_MISREAD_MARKERS) - - # Running commands diff --git a/tests/test_autorun.py b/tests/test_autorun.py index 60ebd10a9..f7117e300 100644 --- a/tests/test_autorun.py +++ b/tests/test_autorun.py @@ -34,6 +34,7 @@ from core import statefile from collection import integrity as bib from collection.gitrestore import GitFailed from devtools import diskcleanup as cd +from devtools import testrunner from release import sync REPO_ROOT = Path(__file__).resolve().parents[1] @@ -351,6 +352,32 @@ class Retry(Scratch): self.assertEqual(runner.run_with_retry(run, (0,), lambda: None).returncode, 1) self.assertEqual(len(calls), 1) + def test_an_abort_is_retried_like_a_segfault(self): + run, calls = self._runs(runner.Outcome(-6), runner.Outcome(0)) + self.assertEqual(runner.run_with_retry(run, (0,), lambda: None).returncode, 0) + self.assertEqual(len(calls), 2) + + def test_the_test_runner_reruns_what_autorun_retries(self): + """run_tests reran only signal kills: a module failing with exit 1 on + 'SystemError: unknown opcode' was reported FAILED, where an autorun + step failing the same way ran again.""" + logs = self.tmp / "logs" + logs.mkdir() + cases = {"tests.test_misread": (1, "SystemError: unknown opcode 220\n"), + "tests.test_signal": (-11, ""), + "tests.test_real": (1, "AssertionError: 1 != 2\n")} + results = [] + for module, (code, text) in cases.items(): + log = logs / f"{module}.log" + log.write_text(text, encoding="utf-8") + results.append(testrunner.Result(module, code, 1.0, log=log)) + again = testrunner.Result("x", 0, 1.0) + with unittest.mock.patch.object(testrunner, "run_module", return_value=again) as rerun: + testrunner.rerun_crashed(results, {}, logs, lambda _line: None) + self.assertEqual(sorted(c.args[0] for c in rerun.call_args_list), + ["tests.test_misread", "tests.test_signal"]) + self.assertEqual([r.passed for r in results], [True, True, False]) + def test_a_real_segfault_runs_again_and_the_job_passes(self): crash = ("import os, signal, pathlib\n" "p = pathlib.Path('crashed')\n" diff --git a/wiki/testing-guide.md b/wiki/testing-guide.md index 71ca83c34..fc1395b84 100644 --- a/wiki/testing-guide.md +++ b/wiki/testing-guide.md @@ -24,7 +24,8 @@ python -m unittest discover -s tests -t . -v ``` Or run it as parallel processes, one per module, each with its own -scratch folder. A module a host crash kills runs again alone at the end: +scratch folder. A module a host crash kills, or that fails with `SystemError` +or "unknown opcode", runs again alone at the end: ```bash python scripts/run_tests.py # a few modules at a time diff --git a/wiki/tools.md b/wiki/tools.md index e4d404d69..0b8e54a20 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -829,7 +829,7 @@ commit. | Lock | One host-wide `flock` on `~/.local/state/retrobios/auto.lock` (`RETROBIOS_AUTO_LOCK` moves it). A job that finds it held is `deferred`. | | Load | Waits while the 1-minute load is above a quarter of the CPUs, up to `max_load_wait` seconds, then `deferred`. | | Disk | Below the job's `min_free_gb` the job is `refused`. Under 100 GB free the `disk` alert is raised. | -| Retry | Exit 139 (or signal 11), `SystemError` or "unknown opcode" is run again once on an idle host. A second failure is a failure. | +| Retry | A signal kill, exit 139, `SystemError` or "unknown opcode" is run again once on an idle host, the rule `run_tests.py` applies to a test module. A second failure is a failure. | | Environment | A step, gate or recomputation inherits `PATH`, `HOME`, `LANG`, `LC_*`, `TZ` and `TMPDIR`, the runner's own markers and the variables its `env:` key names. A name with `TOKEN`, `SECRET` or `PASS` in it, `GH_TOKEN` and the RomStation account are dropped even when named, and a step naming one is refused when the registry loads. `GITHUB_TOKEN` goes to the push, the `gh` calls that read CI, and a step marked `github_token: true`. The registry accepts that key only on `python scripts/check_freshness.py`, `python scripts/refresh_stale.py` and `python -m scripts.scraper.*` tracked in this repository, and never in the `campaign` job. | | Double computation | Each `twice` entry recomputes an artifact the gate wrote and compares both, timestamps aside. A difference is recomputed once on an idle host, then the job fails with the `host-integrity` alert. |