Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions app/nullius/execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``,
Expand Down
42 changes: 42 additions & 0 deletions app/tests/test_execution_stop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading