From ada0e99dfc63c5a9061a140c1a0759a48e9a0800 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 00:18:09 -0800 Subject: [PATCH 1/2] CI: the cold cache clear fails on a late warm leg before its job times out The script's 160-minute wait is timed from the cache warm leg's start, and the leg starts only after gate / select legs and a free runner. The job's 200-minute timeout left 40 minutes for that; a push to main on 2026-11-28 took 44 minutes from the run's start to the leg's start. A later start ended the job with a timeout instead of the script's error. The script now fails with its own error when the leg has not started 120 minutes after the script started, and the job's timeout is 290 minutes, longer than the start wait and the 160-minute save wait together. A test reads both timeouts from ci.yml and checks them against the script's limits. --- .github/workflows/ci.yml | 9 +++--- scripts/ci-clear-rolling-cache-tests.py | 40 +++++++++++++++++++++---- scripts/ci-clear-rolling-cache.py | 37 ++++++++++++++++++----- 3 files changed, 68 insertions(+), 18 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b61d2429d0..e7ae30d3ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -89,14 +89,15 @@ jobs: # 10 GB cache limit and get the corpus, SonarCloud and Windows ARM64 entries evicted. This deletes # the old entries when the `cache warm` leg starts saving (scripts/ci-clear-rolling-cache.py). # When the leg fails or is canceled, before its save or during it, this job fails too, so "Re-run - # failed jobs" runs it again beside the re-run leg. The script's 160-minute wait starts when the leg - # starts, which is after `gate / select legs` and a free runner, so this job's timeout leaves - # 40 minutes for that. + # failed jobs" runs it again beside the re-run leg. The leg starts after `gate / select legs` and a + # free runner: the script fails when the leg has not started within 120 minutes, and then waits + # up to 160 minutes from the leg's start. This job's timeout is longer than both together, so the + # script reports a leg that starts late with its own error before the job times out. cold-cache-clear: name: cold cache clear if: github.event_name == 'schedule' runs-on: ubuntu-24.04 - timeout-minutes: 200 + timeout-minutes: 290 permissions: actions: write steps: diff --git a/scripts/ci-clear-rolling-cache-tests.py b/scripts/ci-clear-rolling-cache-tests.py index 734073aba3..8ccc8a5adc 100644 --- a/scripts/ci-clear-rolling-cache-tests.py +++ b/scripts/ci-clear-rolling-cache-tests.py @@ -9,6 +9,7 @@ import importlib.util import io import os from pathlib import Path +import re import subprocess import unittest from unittest import mock @@ -78,15 +79,42 @@ class Deadline(unittest.TestCase): before_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS - 60) after_limit = clear.parse_time("2026-11-28T04:00:00Z") + timedelta(seconds=clear.GIVE_UP_SECONDS + 60) self.assertGreater((before_limit - self.began).total_seconds(), clear.GIVE_UP_SECONDS) - self.assertFalse(clear.gave_up(late, before_limit, self.began)) - self.assertTrue(clear.gave_up(late, after_limit, self.began)) + self.assertFalse(clear.gave_up(late, before_limit)) + self.assertTrue(clear.gave_up(late, after_limit)) + self.assertFalse(clear.start_overdue(late, after_limit, self.began)) def test_a_leg_that_never_starts_is_timed_from_the_script_start(self): queued = warm_job("queued", started_at="2026-11-28T03:38:00Z") - later = self.began + timedelta(seconds=clear.GIVE_UP_SECONDS + 60) - self.assertFalse(clear.gave_up(None, self.began + timedelta(minutes=10), self.began)) - self.assertTrue(clear.gave_up(None, later, self.began)) - self.assertTrue(clear.gave_up(queued, later, self.began)) + before_limit = self.began + timedelta(seconds=clear.START_SECONDS - 60) + after_limit = self.began + timedelta(seconds=clear.START_SECONDS + 60) + self.assertFalse(clear.start_overdue(None, before_limit, self.began)) + self.assertTrue(clear.start_overdue(None, after_limit, self.began)) + self.assertTrue(clear.start_overdue(queued, after_limit, self.began)) + self.assertFalse(clear.gave_up(queued, after_limit)) + + +class Workflow(unittest.TestCase): + ci = (Path(__file__).resolve().parent.parent / ".github" / "workflows" / "ci.yml").read_text() + + def timeout_minutes(self, job): + """The first `timeout-minutes` under `job` in ci.yml.""" + lines = self.ci.splitlines() + start = lines.index(f" {job}:") + 1 + for line in lines[start:]: + if re.match(r" \S", line): + break + match = re.match(r"\s+timeout-minutes: (\d+)$", line) + if match: + return int(match.group(1)) + self.fail(f"no timeout-minutes under {job}") + + def test_the_job_outlasts_the_start_wait_and_the_save_wait(self): + # Five minutes are left for the steps before the script and for the deletion. + spare = self.timeout_minutes("cold-cache-clear") * 60 - clear.START_SECONDS - clear.GIVE_UP_SECONDS + self.assertGreaterEqual(spare, 5 * 60) + + def test_the_save_wait_outlasts_the_gate(self): + self.assertGreater(clear.GIVE_UP_SECONDS, self.timeout_minutes("gate") * 60) class Jobs(unittest.TestCase): diff --git a/scripts/ci-clear-rolling-cache.py b/scripts/ci-clear-rolling-cache.py index e9b52d2493..3c27809187 100644 --- a/scripts/ci-clear-rolling-cache.py +++ b/scripts/ci-clear-rolling-cache.py @@ -16,8 +16,8 @@ save step started saves nothing: its old entries stay, and this exits 1, so "Re- runs it again beside the re-run leg. A save step that is canceled or fails after the old entries were deleted leaves no rolling entry, so the next run starts cold, and this exits 1 too. A failure in a later post step does not count once the save step has succeeded. This also exits 1 when the -leg has not reached its save, or its save has not finished, GIVE_UP_SECONDS after the leg started. -An absent warm leg is treated as finished only +leg has not started START_SECONDS after this script started, and when the leg has not reached its +save, or its save has not finished, GIVE_UP_SECONDS after the leg started. An absent warm leg is treated as finished only when the most recent earlier attempt that lists it reports success and the current attempt lists no `cache prune` job, which runs from the start of "Re-run all jobs". Otherwise this keeps polling because reusable-workflow dependencies may still be scheduling it. @@ -46,6 +46,12 @@ PAGE_SIZE = 100 # Longer than the gate's `timeout-minutes` in ci.yml and timed from the leg's own start, so a cold # leg that starts late and runs to its limit still reaches its save before this gives up. GIVE_UP_SECONDS = 160 * 60 +# How long the leg may take to start, timed from this script's start: the leg waits for +# `gate / select legs` and then for a runner, which took 44 minutes on a push to main on +# 2026-11-28. The `cold-cache-clear` job's `timeout-minutes` in ci.yml is longer than +# START_SECONDS and GIVE_UP_SECONDS together, so this exits with its own error before the job +# times out. +START_SECONDS = 120 * 60 STOPPED = ('failure', 'cancelled', 'timed_out') # Steps with continue-on-error in craft-common-actions .github/workflows/gate.yml. TOLERATED_FAILURES = ('Trim the cache entry to this build',) @@ -85,11 +91,22 @@ def save_finished(job): return job.get('status') == 'completed' or (step is not None and step['status'] == 'completed') -def gave_up(job, now, began): - """True once GIVE_UP_SECONDS have passed since the warm leg started, or since `began` (this - script's start) while the leg has not started.""" +def started_at(job): + """The warm leg's start time, or None while the leg has not started.""" started = job.get('started_at') if job and job.get('status') in ('in_progress', 'completed') else None - return (now - (parse_time(started) if started else began)).total_seconds() > GIVE_UP_SECONDS + return parse_time(started) if started else None + + +def start_overdue(job, now, began): + """True once START_SECONDS have passed since `began` (this script's start) and the warm leg + has not started.""" + return started_at(job) is None and (now - began).total_seconds() > START_SECONDS + + +def gave_up(job, now): + """True once GIVE_UP_SECONDS have passed since the warm leg started.""" + started = started_at(job) + return started is not None and (now - started).total_seconds() > GIVE_UP_SECONDS def all_caches(fetch): @@ -171,7 +188,11 @@ def main(): return 1 print('the cache warm leg finished without a save step; nothing deleted') return - if gave_up(job, datetime.now(timezone.utc), began): + now = datetime.now(timezone.utc) + if start_overdue(job, now, began): + print(f'::error::the cache warm leg did not start within {START_SECONDS // 60} minutes; nothing deleted') + return 1 + if gave_up(job, now): print('::error::the cache warm leg never reached its save; nothing deleted') return 1 time.sleep(POLL_SECONDS) @@ -190,7 +211,7 @@ def main(): print(result.stderr, end='', file=sys.stderr) return result.returncode while not save_finished(job): - if gave_up(job, datetime.now(timezone.utc), began): + if gave_up(job, datetime.now(timezone.utc)): print(f'::error::{SAVE_LOST}') return 1 time.sleep(POLL_SECONDS) From ab260718f59578e4f73071de326dc050de995970 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 28 Nov 2026 00:45:19 -0800 Subject: [PATCH 2/2] CI: the cold cache clear fails when the warm leg starts after the start limit start_overdue only looked at a leg that had not started, so a leg that started 5 seconds past START_SECONDS and reached its save was let through and the old entries were deleted. The script now exits 1 before the save check when the leg's start time is more than START_SECONDS after the script started. --- scripts/ci-clear-rolling-cache-tests.py | 23 +++++++++++++++++++++++ scripts/ci-clear-rolling-cache.py | 9 +++++++++ 2 files changed, 32 insertions(+) diff --git a/scripts/ci-clear-rolling-cache-tests.py b/scripts/ci-clear-rolling-cache-tests.py index 8ccc8a5adc..a284579d16 100644 --- a/scripts/ci-clear-rolling-cache-tests.py +++ b/scripts/ci-clear-rolling-cache-tests.py @@ -92,6 +92,13 @@ class Deadline(unittest.TestCase): self.assertTrue(clear.start_overdue(queued, after_limit, self.began)) self.assertFalse(clear.gave_up(queued, after_limit)) + def test_a_leg_that_starts_after_the_start_limit_fails(self): + at = lambda seconds: warm_job("in_progress", started_at=(self.began + timedelta(seconds=seconds)).isoformat()) + self.assertTrue(clear.started_late(at(clear.START_SECONDS + 5), self.began)) + self.assertFalse(clear.started_late(at(clear.START_SECONDS - 10), self.began)) + self.assertFalse(clear.started_late(warm_job("queued"), self.began)) + self.assertFalse(clear.started_late(None, self.began)) + class Workflow(unittest.TestCase): ci = (Path(__file__).resolve().parent.parent / ".github" / "workflows" / "ci.yml").read_text() @@ -215,6 +222,22 @@ class AfterDeletion(unittest.TestCase): canceled = warm_job("completed", "cancelled", [("Gate", "completed", "success"), ("Post Setup", "completed", "cancelled")]) self.assertEqual(self.run_main(canceled), 1) + def test_a_leg_that_starts_late_and_saves_deletes_nothing(self): + # Clock.now() is 04:00, so the leg starts 120 minutes 5 seconds after the script. + late = warm_job("in_progress", steps=self.SAVING, started_at="2026-11-28T06:00:05Z") + deleted = [] + + def run(args, **kwargs): + deleted.append(args[-1]) + return subprocess.CompletedProcess(args, 0, "", "") + + env = {"GITHUB_REPOSITORY": "storytold/vectorcraft", "GITHUB_RUN_ID": "1", "GITHUB_RUN_ATTEMPT": "1"} + with mock.patch.object(clear, "gh", lambda url: {"jobs": ME + [late]}), mock.patch.object(clear.subprocess, "run", run), \ + mock.patch.object(clear.time, "sleep"), mock.patch.object(clear, "datetime", Clock), mock.patch.dict(os.environ, env), \ + contextlib.redirect_stdout(io.StringIO()): + self.assertEqual(clear.main(), 1) + self.assertEqual(deleted, []) + def test_a_save_that_fails_after_the_deletion_exits_1(self): failed = warm_job("completed", "failure", [("Gate", "completed", "success"), ("Post Setup", "completed", "failure")]) self.assertEqual(self.run_main(failed), 1) diff --git a/scripts/ci-clear-rolling-cache.py b/scripts/ci-clear-rolling-cache.py index 3c27809187..3b913d68f7 100644 --- a/scripts/ci-clear-rolling-cache.py +++ b/scripts/ci-clear-rolling-cache.py @@ -103,6 +103,12 @@ def start_overdue(job, now, began): return started_at(job) is None and (now - began).total_seconds() > START_SECONDS +def started_late(job, began): + """True when the warm leg started more than START_SECONDS after `began` (this script's start).""" + started = started_at(job) + return started is not None and (started - began).total_seconds() > START_SECONDS + + def gave_up(job, now): """True once GIVE_UP_SECONDS have passed since the warm leg started.""" started = started_at(job) @@ -167,6 +173,9 @@ def main(): while True: jobs = gh(jobs_url) or {} job = warm_job(jobs.get('jobs', [])) + if started_late(job, began): + print(f'::error::the cache warm leg did not start within {START_SECONDS // 60} minutes; nothing deleted') + return 1 if job and saving(job): break if job is None: