ci(molecule): stop a scenario that runs past its own time limit

A hung scenario held its lane until the job's 90-minute timeout, and the
cancel then lost every scenario's log, since the groups print only after
the final wait. Each molecule test now runs under coreutils timeout with
MOLECULE_SCENARIO_TIMEOUT seconds (default 1800), so the hang fails by
itself, its log says it timed out, and the other lanes finish.

timeout runs with --foreground so it and molecule stay in the script's
process group: without it timeout leads a group of its own, which a
cancel that kills the step's group no longer reaches (the killed-group
test fails). On expiry molecule alone is signalled; anything it leaves
ends with the run container (docker run --init).

Closes #474

Assisted-by: Claude Code
This commit is contained in:
Defame1297 committed 2026-11-29 11:42:09 +00:00
1 parent e8f739904a
commit 41820fd9a7
3 files changed
+60 -7

No files matched your search

+2 -2
View File
@@ -112,8 +112,8 @@ issue.
older than its timeout, which a cancelled run left, and finally its own. The scenarios of one older than its timeout, which a cancelled run left, and finally its own. The scenarios of one
run overlap (`scripts/run_molecule_scenarios.sh`, `MOLECULE_LANES` at a time, 2 unless a manual run overlap (`scripts/run_molecule_scenarios.sh`, `MOLECULE_LANES` at a time, 2 unless a manual
dispatch sets its `lanes` input), so no two scenarios may share a platform name. They start dispatch sets its `lanes` input), so no two scenarios may share a platform name. They start
longest first by the script's list of last full-run times, an unlisted one first; the logs stay longest first by the script's list of last full-run times, an unlisted one first; one still
in selection order. *Why:* a cancelled run is killed before it can remove its instances, a running after 30 min fails on its own; the logs stay in selection order. *Why:* a cancelled run is killed before it can remove its instances, a
younger container may belong to a run still going, and a long scenario started late sets the younger container may belong to a run still going, and a long scenario started late sets the
end of the run. *Held:* review; control: the start order by end of the run. *Held:* review; control: the start order by
`test_the_longest_scenarios_start_first` and the input by `LanesInputTest`, both in `test_the_longest_scenarios_start_first` and the input by `LanesInputTest`, both in
+16 -2
View File
@@ -2,7 +2,8 @@
# The Molecule workflow's "Run the scenarios" step: `molecule test` for each scenario directory on # The Molecule workflow's "Run the scenarios" step: `molecule test` for each scenario directory on
# stdin, MOLECULE_LANES (default 2, also when empty) at a time, longest first, each with its own # stdin, MOLECULE_LANES (default 2, also when empty) at a time, longest first, each with its own
# log and ANSIBLE_HOME under <work-dir>/<n>/ (no collection is installed there: # log and ANSIBLE_HOME under <work-dir>/<n>/ (no collection is installed there:
# .config/molecule/config.yml sets prerun false). # .config/molecule/config.yml sets prerun false). A scenario still running after
# MOLECULE_SCENARIO_TIMEOUT seconds (default 1800, also when empty) is stopped and fails.
# Exit 1 on a failure. # Exit 1 on a failure.
# #
# scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs # scripts/run_molecule_scenarios.sh <work-dir> < scenario-dirs
@@ -14,6 +15,13 @@ if ! [[ "$lanes" =~ ^[1-9][0-9]*$ ]]; then
echo "MOLECULE_LANES must be a positive integer, not '$lanes'" >&2 echo "MOLECULE_LANES must be a positive integer, not '$lanes'" >&2
exit 2 exit 2
fi fi
# 1800: the longest scenario takes about 8.5 min, and two hung ones plus the rest still end inside
# the job's 90 min, so a hang fails on its own with its log printed.
limit="${MOLECULE_SCENARIO_TIMEOUT:-1800}"
if ! [[ "$limit" =~ ^[1-9][0-9]*$ ]]; then
echo "MOLECULE_SCENARIO_TIMEOUT must be a positive integer, not '$limit'" >&2
exit 2
fi
if [ "$#" -ne 1 ]; then if [ "$#" -ne 1 ]; then
echo "usage: $0 <work-dir> < scenario-dirs" >&2 echo "usage: $0 <work-dir> < scenario-dirs" >&2
exit 2 exit 2
@@ -62,8 +70,14 @@ run_one() {
export ANSIBLE_HOME="$out/ansible" export ANSIBLE_HOME="$out/ansible"
# a role scenario runs from its role's directory, as molecule expects # a role scenario runs from its role's directory, as molecule expects
case "$dir" in roles/*) cd "${dir%/molecule/*}" || exit 1 ;; esac case "$dir" in roles/*) cd "${dir%/molecule/*}" || exit 1 ;; esac
molecule test -s "${dir##*/}" # --foreground keeps timeout and molecule in this script's process group, so a cancel that
# kills the group still reaches them; on expiry it signals molecule alone, not its children.
timeout --foreground --kill-after=30 "$limit" molecule test -s "${dir##*/}"
) >"$out/log" 2>&1 </dev/null || rc=$? ) >"$out/log" 2>&1 </dev/null || rc=$?
# 124 after TERM, 137 when the KILL of --kill-after was needed
if { [ "$rc" -eq 124 ] || [ "$rc" -eq 137 ]; } && [ "$((SECONDS - start))" -ge "$limit" ]; then
echo "timed out after $limit s" >>"$out/log"
fi
if ! remove_instances "$dir" >>"$out/log" 2>&1; then if ! remove_instances "$dir" >>"$out/log" 2>&1; then
echo "could not remove the containers of $dir" >>"$out/log" echo "could not remove the containers of $dir" >>"$out/log"
if [ "$rc" -eq 0 ]; then rc=1; fi if [ "$rc" -eq 0 ]; then rc=1; fi
+42 -3
View File
@@ -28,9 +28,10 @@ RUN_LABEL = "net.rkdr.mortis.molecule-run"
HAS_MOLECULE = importlib.util.find_spec("molecule") is not None HAS_MOLECULE = importlib.util.find_spec("molecule") is not None
NEEDS_MOLECULE = "the run script imports molecule to expand platform names" NEEDS_MOLECULE = "the run script imports molecule to expand platform names"
# Records what each run saw, then behaves as STUB_<scenario label> asks: fail, sleep, hang (and # Records what each run saw, then behaves as STUB_<scenario label> asks: fail, sleep, stall (past
# record its pids), detach (as an ansible worker does, into its own process group, whose orphaned # a short time limit), hang (and record its pids), detach (as an ansible worker does, into its own
# child no parent link reaches; record both), or read stdin (which must be empty). # process group, whose orphaned child no parent link reaches; record both), or read stdin (which
# must be empty).
MOLECULE_STUB = """#!/usr/bin/env bash MOLECULE_STUB = """#!/usr/bin/env bash
label="$(basename "$PWD")/$3" label="$(basename "$PWD")/$3"
[ "$PWD" = "$STUB_ROOT" ] && label="$3" [ "$PWD" = "$STUB_ROOT" ] && label="$3"
@@ -40,6 +41,7 @@ echo "output of $label"
case "${!key:-}" in case "${!key:-}" in
fail) echo "$label end $(date +%s.%N)" >> "$STUB_LOG"; exit 2 ;; fail) echo "$label end $(date +%s.%N)" >> "$STUB_LOG"; exit 2 ;;
sleep) sleep 1 ;; sleep) sleep 1 ;;
stall) exec sleep 30 ;;
hang) sleep 60 & echo "$$ $!" >> "$STUB_PIDS"; wait ;; hang) sleep 60 & echo "$$ $!" >> "$STUB_PIDS"; wait ;;
detach) detach)
setsid bash -c '(sleep 60 & echo $! >> "$STUB_PIDS"); echo $$ >> "$STUB_PIDS"; exec sleep 60' & setsid bash -c '(sleep 60 & echo $! >> "$STUB_PIDS"); echo $$ >> "$STUB_PIDS"; exec sleep 60' &
@@ -330,6 +332,43 @@ class RunMoleculeScenariosTest(unittest.TestCase):
self.assertIn("MOLECULE_LANES must be a positive integer", proc.stderr) self.assertIn("MOLECULE_LANES must be a positive integer", proc.stderr)
self.assertEqual(self.log.read_text(), "") self.assertEqual(self.log.read_text(), "")
@unittest.skipUnless(HAS_MOLECULE, NEEDS_MOLECULE)
def test_a_scenario_past_its_time_limit_fails_alone(self):
proc = self.run_script(
["roles/alpha/molecule/default", "roles/beta/molecule/default"],
MOLECULE_SCENARIO_TIMEOUT="2", STUB_alpha_default="stall",
) # fmt: skip
self.assertEqual(proc.returncode, 1)
self.assertIn("::error::Failed: alpha/default\n", proc.stdout)
self.assertIn("Scenarios run: 2", proc.stdout)
body = proc.stdout.split("::group::molecule test: alpha/default")[1].split(
"::endgroup::"
)[0]
self.assertIn("output of alpha/default", body)
self.assertIn("timed out after 2 s", body)
self.assertIn("output of beta/default", proc.stdout)
self.assertIn(
"remove local-alpha-instance force=True", self.docker_log.read_text()
)
def test_the_default_time_limit_is_inside_the_job_timeout(self):
# a limit past the job's own would let a hang take the job and every log with it
timeout = yaml.safe_load(WORKFLOW.read_text())["jobs"]["molecule"][
"timeout-minutes"
]
limit = re.findall(r"MOLECULE_SCENARIO_TIMEOUT:-(\d+)\}", SCRIPT.read_text())
self.assertEqual(len(limit), 1)
self.assertLess(int(limit[0]), timeout * 60)
def test_the_time_limit_must_be_a_positive_integer(self):
for limit in ("0", "-1", "ten", "04"):
proc = self.run_script(list(PLATFORMS), MOLECULE_SCENARIO_TIMEOUT=limit)
self.assertEqual(proc.returncode, 2, limit)
self.assertIn(
"MOLECULE_SCENARIO_TIMEOUT must be a positive integer", proc.stderr
)
self.assertEqual(self.log.read_text(), "")
def start_hung_run(self, count=4, **stubs): def start_hung_run(self, count=4, **stubs):
"""A run in its own process group whose molecule stubs hang, and the pids they record.""" """A run in its own process group whose molecule stubs hang, and the pids they record."""
pids = self.tmp / "pids" pids = self.tmp / "pids"