From e6e450ee37fa3fb87c6f588e37fa12807fb46550 Mon Sep 17 00:00:00 2001 From: mrveiss Date: Mon, 9 Nov 2026 00:56:15 +0200 Subject: [PATCH] fix(hooks): slot wait keys on loop iteration, not the SECONDS clock (#21225) --- repo_tests/push_marker_20260_test.py | 22 ++++++++++++++++++++++ tools/git-hooks/pre-push | 6 ++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/repo_tests/push_marker_20260_test.py b/repo_tests/push_marker_20260_test.py index d166aee05e..3c11962cb0 100644 --- a/repo_tests/push_marker_20260_test.py +++ b/repo_tests/push_marker_20260_test.py @@ -142,3 +142,25 @@ def test_contrast_no_wait_means_no_extra_network_call(tmp_path: Path) -> None: ) _, calls = _hook(tmp_path, body) assert calls == "" + + +def _first_attempt_across_a_second_boundary(tmp_path: Path, hook: str) -> str: + """A slot taken on the first attempt, with the SECONDS clock moved on while taking it (#21225).""" + body = ( + f"{hook}\nlog() {{ :; }}\nunset CI AUTOBOT_MACHINE_SLOT_HELD\n" + "machine_slot_take() { SECONDS=$((SECONDS + 5)); return 0; }\n" + f"export AUTOBOT_MACHINE_SLOT_DIR={tmp_path} AUTOBOT_MACHINE_SLOT_LOADAVG={fake_load(tmp_path / 'loadavg')}\n" + "branch_name=issue-1-a; remote_sha=abc; machine_slot_wait\n" + ) + done, calls = _hook(tmp_path, body) + assert done.returncode == 0, done.stderr + return calls + + +def test_a_first_attempt_take_across_a_second_boundary_sets_no_marker(tmp_path: Path) -> None: + assert _first_attempt_across_a_second_boundary(tmp_path, hook_functions()) == "" + + +def test_contrast_the_clock_comparison_counted_that_take_as_a_wait(tmp_path: Path) -> None: + clock = hook_functions().replace('[ "$waited" = 1 ] || return 0', '[ "$SECONDS" != "$start" ] || return 0') + assert "ref=refs/push-gate/issue-1-a/" in _first_attempt_across_a_second_boundary(tmp_path, clock) diff --git a/tools/git-hooks/pre-push b/tools/git-hooks/pre-push index aa26a27845..64d145122e 100755 --- a/tools/git-hooks/pre-push +++ b/tools/git-hooks/pre-push @@ -317,14 +317,15 @@ machine_slot_wait() { [ -z "${CI:-}" ] && [ -z "${AUTOBOT_MACHINE_SLOT_HELD:-}" ] || return 0 command -v flock >/dev/null 2>&1 || { warn "flock is missing: this push did not queue for a machine slot"; return 0; } local dir="${AUTOBOT_MACHINE_SLOT_DIR:-${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}}/autobot-machine-slots-$(id -u)}" - local start=$SECONDS next=$SECONDS every="${AUTOBOT_MACHINE_SLOT_REPORT:-60}" why + local start=$SECONDS next=$SECONDS every="${AUTOBOT_MACHINE_SLOT_REPORT:-60}" why waited=0 mkdir -p "$dir" while :; do # A slot is taken only while the 1-minute load is under the ceiling. if machine_slot_load_high; then why="$MACHINE_SLOT_LOAD_MSG" elif machine_slot_take "$dir"; then - [ "$SECONDS" != "$start" ] || return 0 + # #21225: a wait is an iteration of this loop; SECONDS is an integer clock and also ticks across a first-attempt take. + [ "$waited" = 1 ] || return 0 log "waited $((SECONDS - start))s for a machine slot" # #20260: the queue has no time limit, so the marker set before it may be about to expire. [ -z "${branch_name:-}" ] || push_marker_set "$branch_name" "${remote_sha:-$ZERO}" @@ -336,6 +337,7 @@ machine_slot_wait() { log "queued $((SECONDS - start))s: $why" next=$((SECONDS + every)) fi + waited=1 sleep "${AUTOBOT_MACHINE_SLOT_POLL:-5}" done }