Answer principles round 2: 0 🔴, 1 🟡 (0 misread); remove ci.sh's container when a patch cut stops it, show task-bench's count defaults where they apply, and take ci.sh's bound from the forge's wait
CI / tests (pull_request) Successful in 4m47s

This commit is contained in:
2026-11-27 12:09:14 +01:00
parent 4fc4763710
commit be96c36591
6 changed files with 101 additions and 30 deletions
+34 -11
View File
@@ -4,33 +4,43 @@ import os
import pathlib
import signal
import subprocess
import typing
from deploy_release.patch import Ci, CiRan
from failure import Failure
from deploy_release.trace import Say
from forge.pulls import WAIT_SECONDS
# As long as forge.pulls waits for a pull request's checks, which run the same ci.sh.
CI_SECONDS = 30 * 60
# How long a stopped ci.sh has to end on SIGINT, which a container's CLI passes on to its container, before it is killed.
# As long as a pull request's checks get, since they run the same ci.sh.
CI_SECONDS = WAIT_SECONDS
# How long a stopped ci.sh has to end on SIGINT before it is killed.
GRACE_SECONDS = 10
# How long docker has to remove a stopped ci.sh's container.
REMOVE_SECONDS = 60
# Where ci.sh reads the name to give its test container.
CONTAINER = "CI_CONTAINER"
# Removes the container of a name, running or not; a Failure where it may still run.
Remove = typing.Callable[[str], Failure | None]
def ci_runner(log: pathlib.Path, seconds: float, say: Say, grace: float = GRACE_SECONDS) -> Ci:
"""Runs ci.sh in a worktree with its output in log, as it comes. A Failure where it can't start, where log can't be written, or where it still runs after seconds, which stops it and each process it started."""
def ci_runner(log: pathlib.Path, seconds: float, say: Say, container: str, remove: Remove, grace: float = GRACE_SECONDS) -> Ci:
"""Runs ci.sh in a worktree with its output in log, as it comes, and its test container named container. A Failure where it can't start, where log can't be written, or where it still runs after seconds, which stops it, each process it started and that container."""
def run(worktree: pathlib.Path) -> CiRan | Failure:
say(f"ci.sh's output goes to {log}; it is stopped where it still runs after {lasting(seconds)}")
try:
with log.open("w", encoding="utf-8") as output:
# In a session of its own, ci.sh and each process it starts share one process group, which `stopped` ends whole.
process = subprocess.Popen(["sh", "ci.sh"], cwd=worktree, stdin=subprocess.DEVNULL, stdout=output, stderr=subprocess.STDOUT, start_new_session=True)
process = subprocess.Popen(["sh", "ci.sh"], cwd=worktree, stdin=subprocess.DEVNULL, stdout=output, stderr=subprocess.STDOUT, start_new_session=True, env={**os.environ, CONTAINER: container})
try:
code = process.wait(timeout=seconds)
except subprocess.TimeoutExpired:
stopped(process, grace)
return Failure(f"ci.sh still ran after {lasting(seconds)} and was stopped; {log} holds its output")
left = stopped(process, grace, container, remove)
running = f", but its container {container} may still run: {left.reason}" if left else ""
return Failure(f"ci.sh still ran after {lasting(seconds)} and was stopped{running}; {log} holds its output")
except BaseException:
# The cut is ending, as on Ctrl-C, which reaches only this process's group.
stopped(process, grace)
stopped(process, grace, container, remove)
raise
return CiRan(code, log.read_text(encoding="utf-8", errors="replace"))
except OSError as failure:
@@ -38,8 +48,8 @@ def ci_runner(log: pathlib.Path, seconds: float, say: Say, grace: float = GRACE_
return run
def stopped(process: subprocess.Popen[bytes], grace: float) -> None:
"""End process's whole group: SIGINT first, then SIGKILL once grace seconds have passed."""
def stopped(process: subprocess.Popen[bytes], grace: float, container: str, remove: Remove) -> Failure | None:
"""End process's whole group, SIGINT first, then SIGKILL once grace seconds have passed, and then its container; a Failure where the container may still run."""
try:
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGINT)
@@ -49,6 +59,19 @@ def stopped(process: subprocess.Popen[bytes], grace: float) -> None:
with contextlib.suppress(ProcessLookupError):
os.killpg(process.pid, signal.SIGKILL)
process.wait()
# The daemon runs the container, outside the group, and no signal to ci.sh's docker CLI ends the tests in it. It is removed last, once nothing in the group can start it.
left = remove(container)
return left
def removed_container(container: str, run: typing.Callable[..., subprocess.CompletedProcess[str]] = subprocess.run) -> Failure | None:
"""Removes container with docker, running or not; None too where no such container exists."""
argv = ["docker", "rm", "--force", container]
try:
ran = run(argv, stdin=subprocess.DEVNULL, capture_output=True, text=True, timeout=REMOVE_SECONDS)
except (OSError, subprocess.TimeoutExpired) as failure:
return Failure(f"{' '.join(argv)} failed: {failure}")
return Failure(f"{' '.join(argv)} failed: {ran.stderr.strip()}") if ran.returncode else None
def lasting(seconds: float) -> str:
+2 -2
View File
@@ -70,7 +70,7 @@ import shlex
import sys
import time
from deploy_release.ci_script import ci_runner, CI_SECONDS
from deploy_release.ci_script import ci_runner, CI_SECONDS, removed_container
from deploy_release.cut import cut
from deploy_release.patch import cut_patch
from deploy_release.deploy import deploy
@@ -104,7 +104,7 @@ def main(argv: list[str]) -> int:
file = None if isinstance(begun, Failure) else begun
say = trace.saying(file, now, sys.stderr)
if commits is not None:
moved = cut_patch(clone, checkout, commits, time.sleep, say, ci_runner(folder / f"{name}-ci.log", CI_SECONDS, say))
moved = cut_patch(clone, checkout, commits, time.sleep, say, ci_runner(folder / f"{name}-ci.log", CI_SECONDS, say, f"deploy-release-ci-{name}", removed_container))
else:
moved = cut(clone, checkout, time.sleep, say)
if isinstance(moved, Failure):
+40 -4
View File
@@ -734,6 +734,8 @@ class PatchCi(unittest.TestCase):
self.addCleanup(subprocess.run, ["rm", "-rf", str(self.worktree)])
self.log = self.worktree / "ci.log"
self.said: list[str] = []
self.removed: list[str] = []
self.remove = lambda container: self.removed.append(container)
def test_passes_where_ci_sh_exits_zero(self):
(self.worktree / "ci.sh").write_text("exit 0\n")
@@ -759,7 +761,7 @@ class PatchCi(unittest.TestCase):
def test_the_runner_writes_ci_shs_output_to_the_log_as_it_runs_and_gives_its_exit_code(self):
(self.worktree / "ci.sh").write_text("echo out\necho err >&2\nexit 3\n")
ran = ci_script.ci_runner(self.log, 30, self.said.append)(self.worktree)
ran = ci_script.ci_runner(self.log, 30, self.said.append, "ci-of-a-test", self.remove)(self.worktree)
self.assertEqual(ran, patch.CiRan(3, "out\nerr\n"))
self.assertEqual(self.log.read_text(), "out\nerr\n")
self.assertEqual(self.said, [f"ci.sh's output goes to {self.log}; it is stopped where it still runs after 30 seconds"])
@@ -767,7 +769,7 @@ class PatchCi(unittest.TestCase):
def test_the_runner_stops_a_ci_sh_that_hangs_with_what_it_started_and_says_so(self):
(self.worktree / "ci.sh").write_text("echo started\nsleep 60 &\necho $! >child.pid\nwait\n")
began = time.monotonic()
ran = ci_script.ci_runner(self.log, 0.5, self.said.append, grace=0.2)(self.worktree)
ran = ci_script.ci_runner(self.log, 0.5, self.said.append, "ci-of-a-test", self.remove, grace=0.2)(self.worktree)
self.assertLess(time.monotonic() - began, 20)
self.assertEqual(ran, Failure(f"ci.sh still ran after 0.5 seconds and was stopped; {self.log} holds its output"))
self.assertEqual(self.log.read_text(), "started\n")
@@ -777,15 +779,49 @@ class PatchCi(unittest.TestCase):
time.sleep(0.05)
self.assertFalse(still_runs(child), "the process ci.sh started still runs")
def test_the_runner_names_ci_shs_container_and_removes_it_where_it_stops_ci_sh(self):
(self.worktree / "ci.sh").write_text('echo "$CI_CONTAINER"\nsleep 60\n')
ran = ci_script.ci_runner(self.log, 0.5, self.said.append, "ci-of-a-test", self.remove, grace=0.2)(self.worktree)
self.assertEqual(ran, Failure(f"ci.sh still ran after 0.5 seconds and was stopped; {self.log} holds its output"))
self.assertEqual(self.log.read_text(), "ci-of-a-test\n")
self.assertEqual(self.removed, ["ci-of-a-test"])
def test_the_runner_says_where_the_container_of_a_stopped_ci_sh_may_still_run(self):
(self.worktree / "ci.sh").write_text("sleep 60\n")
ran = ci_script.ci_runner(self.log, 0.5, self.said.append, "ci-of-a-test", lambda container: Failure("docker rm --force ci-of-a-test failed: no daemon"), grace=0.2)(self.worktree)
self.assertEqual(ran, Failure(f"ci.sh still ran after 0.5 seconds and was stopped, but its container ci-of-a-test may still run: docker rm --force ci-of-a-test failed: no daemon; {self.log} holds its output"))
def test_the_runner_removes_no_container_where_ci_sh_ends_in_time(self):
(self.worktree / "ci.sh").write_text("exit 0\n")
ci_script.ci_runner(self.log, 30, self.said.append, "ci-of-a-test", self.remove)(self.worktree)
self.assertEqual(self.removed, [])
def test_removing_a_container_gives_a_failure_where_docker_fails_and_none_where_it_removes(self):
asked: list[list[str]] = []
def docker(code: int, stderr: str):
return lambda argv, **options: asked.append(argv) or subprocess.CompletedProcess(argv, code, "", stderr)
self.assertIsNone(ci_script.removed_container("ci-of-a-test", docker(0, "")))
self.assertEqual(asked, [["docker", "rm", "--force", "ci-of-a-test"]])
self.assertEqual(ci_script.removed_container("ci-of-a-test", docker(1, "Cannot connect to the Docker daemon\n")), Failure("docker rm --force ci-of-a-test failed: Cannot connect to the Docker daemon"))
def test_removing_a_container_gives_a_failure_where_docker_cannot_start(self):
def no_docker(argv, **options):
raise FileNotFoundError("docker")
self.assertEqual(ci_script.removed_container("ci-of-a-test", no_docker), Failure("docker rm --force ci-of-a-test failed: docker"))
def test_ci_sh_names_its_container_only_where_it_is_given_a_name(self):
script = (pathlib.Path(__file__).resolve().parents[2] / "ci.sh").read_text()
self.assertIn('${CI_CONTAINER:+--name "$CI_CONTAINER"}', script)
def test_the_runner_gives_a_failure_where_ci_sh_cannot_start(self):
ran = ci_script.ci_runner(self.log, 30, self.said.append)(self.worktree / "gone")
ran = ci_script.ci_runner(self.log, 30, self.said.append, "ci-of-a-test", self.remove)(self.worktree / "gone")
self.assertIsInstance(ran, Failure)
self.assertIn("ci.sh could not run: ", ran.reason)
self.assertIn(str(self.worktree / "gone"), ran.reason)
def test_the_runner_gives_a_failure_where_the_log_cannot_be_written(self):
(self.worktree / "ci.sh").write_text("exit 0\n")
ran = ci_script.ci_runner(self.worktree / "no-folder" / "ci.log", 30, self.said.append)(self.worktree)
ran = ci_script.ci_runner(self.worktree / "no-folder" / "ci.log", 30, self.said.append, "ci-of-a-test", self.remove)(self.worktree)
self.assertIsInstance(ran, Failure)
self.assertIn(str(self.worktree / "no-folder" / "ci.log"), ran.reason)