From b5457d222bbbdce612214bd6295ac2d4538ebc30 Mon Sep 17 00:00:00 2001 From: Abdessamad Derraz <3028866+Abdess@users.noreply.github.com> Date: Mon, 23 Nov 2026 21:09:23 +0100 Subject: [PATCH] fix: leave each job's timeout to the runner --- scripts/unattended/runner.py | 8 ++++++++ scripts/unattended/timers.py | 4 +++- tests/test_autorun.py | 21 +++++++++++++++++++++ wiki/tools.md | 3 ++- 4 files changed, 34 insertions(+), 2 deletions(-) diff --git a/scripts/unattended/runner.py b/scripts/unattended/runner.py index 8f146176c..5d51abe8b 100644 --- a/scripts/unattended/runner.py +++ b/scripts/unattended/runner.py @@ -465,6 +465,7 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]: runner = Runner(ctx, job, state) started, clock = now_iso(), ctx.host.clock() lock = global_lock(ctx.lock_path) if job.lock else contextlib.nullcontext() + interrupted: SystemExit | None = None try: with lock: runner.note(f"start {name}") @@ -474,6 +475,11 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]: result, detail = exc.result, exc.detail except OSError as exc: result, detail = FAILED, f"{type(exc).__name__}: {exc}" + except SystemExit as exc: + # SIGTERM or SIGHUP (core.cli.run_cli): the run is recorded + # as failed, then the process ends with the signal's status. + interrupted = exc + result, detail = FAILED, f"interrupted, exit status {exc.code}" except LockBusy as exc: result, detail = DEFERRED, str(exc) runner.note(f"end {name}: {result} {detail}") @@ -500,6 +506,8 @@ def run_job(ctx: Context, name: str) -> tuple[str, str]: set_alert(state, f"job:{name}", bool(alert), str(alert or ""), now_iso()) prune_logs(ctx.home / "logs") save_state(ctx.state_path, state) + if interrupted is not None: + raise interrupted return result, detail diff --git a/scripts/unattended/timers.py b/scripts/unattended/timers.py index b0dd41327..0e35957db 100644 --- a/scripts/unattended/timers.py +++ b/scripts/unattended/timers.py @@ -27,7 +27,9 @@ def render_units( f"WorkingDirectory={repo}\n" "Environment=RETROBIOS_AUTO=1\n" f"ExecStart={python} {repo / 'scripts' / 'autorun.py'} run %i{flags}\n" - "TimeoutStartSec=6h\n" + # Each job's own `timeout` is its only limit: Runner stops the + # step and records the run, where a kill from systemd left none. + "TimeoutStartSec=infinity\n" "Nice=10\n" "IOSchedulingClass=idle\n" "SuccessExitStatus=75\n" diff --git a/tests/test_autorun.py b/tests/test_autorun.py index a188f1d2a..4f05f5d64 100644 --- a/tests/test_autorun.py +++ b/tests/test_autorun.py @@ -211,6 +211,19 @@ class Guards(Scratch): self.assertEqual(record["consecutive_failures"], 2) self.assertIsNone(record["last_success"]) + def test_a_run_ended_by_a_signal_is_recorded_then_exits(self): + """systemd's SIGTERM reached run_job as SystemExit and passed the + record: the run left no result, no failure count and no alert.""" + ctx = self.context({"x": {"worktree": False, "min_free_gb": 0, "steps": []}}) + with unittest.mock.patch.object(runner.Runner, "execute", side_effect=SystemExit(143)), \ + self.assertRaises(SystemExit) as caught: + runner.run_job(ctx, "x") + self.assertEqual(caught.exception.code, 143) + record = self.state(ctx)["jobs"]["x"] + self.assertEqual(record["result"], unattended_jobs.FAILED) + self.assertEqual(record["detail"], "interrupted, exit status 143") + self.assertEqual(record["consecutive_failures"], 1) + class StepEnvironment(Scratch): """A step can run third-party code: it never inherits a token or a password.""" @@ -677,6 +690,14 @@ class Units(Scratch): pushed = unattended_timers.render_units(jobs, Path("/r"), "py", push=True)["retrobios@.service"] self.assertIn("run %i --push\n", pushed) + def test_the_unit_leaves_the_time_limit_to_each_job(self): + """A fixed TimeoutStartSec=6h cut a job given 12 h, and killed a 6 h + job before Runner, whose clock starts later, could record it.""" + jobs = unattended_jobs.parse_jobs({"jobs": { + "long": {"schedule": "daily", "timeout": 43200}}}) + service = unattended_timers.render_units(jobs, Path("/r"), "py")["retrobios@.service"] + self.assertIn("TimeoutStartSec=infinity\n", service) + def test_units_are_written_and_enabled_only_when_asked(self): units = unattended_timers.render_units(unattended_jobs.load_jobs(), Path("/r"), "py") calls = [] diff --git a/wiki/tools.md b/wiki/tools.md index 58c2d8c2f..e4d404d69 100644 --- a/wiki/tools.md +++ b/wiki/tools.md @@ -879,7 +879,8 @@ python scripts/autorun.py install-timers --enable `install-timers` writes `retrobios@.service` and one `retrobios-.timer` per scheduled job into `--unit-dir` (default `~/.config/systemd/user`). `--enable` also runs `systemctl --user daemon-reload` and enables the timers. -`--with-push` writes units whose jobs run with `--push`. +`--with-push` writes units whose jobs run with `--push`. The service sets no time +limit of its own: a job's `timeout` ends it, and the run is recorded. ### cleanup_disk.py