From 2d608fd39096cb4f20791293ea5bb8c829cf2649 Mon Sep 17 00:00:00 2001 From: Harenome Ranaivoarivony-Razanajato Date: Fri, 4 Dec 2026 00:25:06 +0100 Subject: [PATCH] fix(execution): record a step error during a stop as stopped --- app/nullius/execution.py | 14 ++++++----- app/tests/test_execution_stop.py | 42 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/nullius/execution.py b/app/nullius/execution.py index 42f82affd..5f2284f3c 100644 --- a/app/nullius/execution.py +++ b/app/nullius/execution.py @@ -1068,13 +1068,15 @@ def _main_resumed(self) -> None: self._fail(self.step or "run", f"{type(e).__name__}: {e}") def _fail(self, step: str, why: str) -> None: - if self._stop.is_set(): # a step cut short by a Stop is the stop, not a failure - return self._done(130) with self._turn: # its end and its record together, as ``_done``'s: a Delete finds both or neither - self._stage_end("failed", detail=why[:300]) - self.step, self.state, self.error, self.finished = step, "failed", why, time.time() - self.log(f"✕ {step}: {why}") - self._save() + stopped = self._stop.is_set() # read under the lock Stop sets it under: a Stop said meanwhile is seen + if not stopped: + self._stage_end("failed", detail=why[:300]) + self.step, self.state, self.error, self.finished = step, "failed", why, time.time() + self.log(f"✕ {step}: {why}") + self._save() + if stopped: # a step cut short by a Stop is the stop, not a failure + self._done(130) def _done(self, rc: int) -> None: """The run's end, recorded once: ``done`` or ``failed`` by ``rc``, diff --git a/app/tests/test_execution_stop.py b/app/tests/test_execution_stop.py index 748b7b515..38a850624 100644 --- a/app/tests/test_execution_stop.py +++ b/app/tests/test_execution_stop.py @@ -96,6 +96,48 @@ def test_a_stop_as_a_part_is_launched_is_never_written_over(self): self.assertEqual(ex.lines[-1], "✕ stopped before next run: exit 0") +class StopAsAStepRaises(Base): + """A step that raises while a Stop is being said - the Stop holding the + run's lock, its event not yet set - is the Stop's: the run ends + ``stopped``, as an ordinary Stop does, never ``failed``.""" + + def test_a_step_that_raises_during_a_stop_is_stopped(self): + gate = self.tmp / "gate"; gate.write_text(""); self.addCleanup(gate.unlink, missing_ok=True) # the part never outlives the test + part = {"id": "a", "label": "a", "script": f"while [ -e {gate} ]; do sleep 0.02; done"} + ex = execution.Execution(execution.Plan(name="r", script="", parts=[part]), root=str(self.tmp)) + stepping, holding, go_stop, go_step, raised, waits = (threading.Event() for _ in range(6)) + + class Gated(threading.Event): # Stop pauses here: the lock taken, its event not yet set + def set(self): + holding.set(); go_stop.wait(5); super().set() + ex._stop = Gated() + + class Turn: # says when the raised step's thread waits for the lock + def __init__(self, lock): self.lock = lock + def __enter__(self): + if raised.is_set() and threading.current_thread() is ex.thread: + waits.set() + return self.lock.__enter__() + def __exit__(self, *a): return self.lock.__exit__(*a) + ex._turn = Turn(ex._turn) + + def follow(part): + stepping.set(); go_step.wait(5); raised.set() + raise RuntimeError("the step broke") + ex._follow = follow + ex.start(); self.assertTrue(stepping.wait(5)) + stopper = threading.Thread(target=ex.stop); stopper.start() + self.assertTrue(holding.wait(5)) + go_step.set(); self.assertTrue(waits.wait(5)) + go_stop.set(); stopper.join(5) + self.wait(ex) + record = json.loads((self.tmp / "r" / "run.json").read_text()) + self.assertEqual((ex.state, record["state"], ex.exit_code), ("stopped", "stopped", 130), ex.lines) + self.assertIsNone(record.get("error")) + self.assertEqual(ex.lines[-1], "✕ stopped during a: exit 130") + self.assertFalse(any("the step broke" in l for l in ex.lines), ex.lines) + + class StopAtLaunch(LocalHost): """A Stop said while a part's launcher has not answered yet - an ssh that takes its time - cuts the launcher short with no pid known. Its detached