From 4fa375649b70682953fd7bf72d4ee652e9f4667e Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:11:03 -0300 Subject: [PATCH 1/6] test(ctl): keep the traces of concurrent starts and dump them on failure Each concurrent start runs under sh -x with its output kept; when the test fails it prints the three traces with exit codes and timings, the pidfiles, the lock directory and its pid file, the children file, the copies the fake recorded and the test's processes. The fake now also records its own pid. ci: let both Python legs finish (fail-fast: false), so a failure on one leg no longer hides the result of the other. --- .github/workflows/ci.yml | 1 + board/tests/test_ctl.py | 58 +++++++++++++++++++++++++++++++++++----- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 848eb61..54094a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,6 +12,7 @@ jobs: test: runs-on: ubuntu-latest strategy: + fail-fast: false matrix: python: ["3.10", "3.13"] steps: diff --git a/board/tests/test_ctl.py b/board/tests/test_ctl.py index 083408c..b3ac8a3 100644 --- a/board/tests/test_ctl.py +++ b/board/tests/test_ctl.py @@ -14,6 +14,7 @@ REPO = Path(__file__).resolve().parent.parent.parent FAKE = """#!/bin/sh +echo "$$ ppid=$PPID $0 $*" >> "$MINIRUNNER_RUN/copies" sh -c 'trap "" TERM; sleep 300' & echo $! >> "$MINIRUNNER_RUN/children" wait @@ -60,6 +61,16 @@ def procs_with(marker, tag): return out +def proc_line(pid): + """pid, ppid, process group, state and command line of a process, for a failure dump.""" + try: + f = Path(f"/proc/{pid}/stat").read_text().rsplit(")", 1)[1].split() + cmd = Path(f"/proc/{pid}/cmdline").read_bytes().replace(b"\0", b" ").decode(errors="replace") + except OSError: + return f"{pid}: gone" + return f"{pid}: ppid={f[1]} pgid={f[2]} state={f[0]} start={f[19]} cmd={cmd.strip()!r}" + + def copies_running(marker, tag): """the copies among procs_with(): a match whose parent is itself a match is that copy's own fork that has not exec'd yet (it still shows the parent's command line), not a second copy.""" @@ -233,14 +244,47 @@ def test_start_twice_runs_one_copy(self): self.assertEqual(len(copies_running(self.marker, str(self.tmp))), self.copies) def test_concurrent_starts_run_one_copy(self): - ps = [subprocess.Popen(["sh", str(REPO / self.script), "start"], env=self.env, - stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) for _ in range(3)] - for p in ps: - p.wait(60) - self.assertTrue(self.wait_for(lambda: len(self.children()) >= self.copies)) + # Each call runs under sh -x with its output kept, so a failure shows what every call did: whether two + # held the lock at once, one took the lock and judged the running copy dead, or one never took it. + env = {**self.env, "PS4": "+ "} + t0 = time.monotonic() + calls = [] + for i in range(3): + trace = (self.tmp / f"trace-{i}").open("w") + self.addCleanup(trace.close) + p = subprocess.Popen(["sh", "-x", str(REPO / self.script), "start"], env=env, stdout=trace, + stderr=subprocess.STDOUT) + calls.append({"p": p, "spawned": time.monotonic() - t0, "ended": None}) + while any(c["ended"] is None for c in calls) and time.monotonic() - t0 < 60: + for c in calls: + if c["ended"] is None and c["p"].poll() is not None: + c["ended"] = time.monotonic() - t0 + time.sleep(0.005) + self.assertTrue(self.wait_for(lambda: len(self.children()) >= self.copies), self.dump(calls)) time.sleep(0.3) - self.assertEqual(len(self.children()), self.copies) - self.assertEqual(len(copies_running(self.marker, str(self.tmp))), self.copies) + if len(self.children()) != self.copies or len(copies_running(self.marker, str(self.tmp))) != self.copies: + self.fail(f"{len(self.children())} copies started, {self.copies} expected\n" + self.dump(calls)) + + def dump(self, calls): + """what the concurrent calls did and left behind, printed only when the test fails.""" + out = ["--- concurrent start evidence ---"] + for i, c in enumerate(calls): + out.append(f"== call {i}: pid={c['p'].pid} rc={c['p'].poll()} spawned={c['spawned']:.3f}s " + f"ended={c['ended'] if c['ended'] is None else round(c['ended'], 3)}s") + out.append((self.tmp / f"trace-{i}").read_text(errors="replace").rstrip()) + for f in sorted(self.run_dir.glob("*.pid")): + pid = f.read_text().strip() + out.append(f"== pidfile {f.name}: {pid!r} -> {proc_line(pid) if pid.isdigit() else '-'}") + out.append(f"== lock {self.lock.name}: " + ( + f"present, entries={sorted(x.name for x in self.lock.iterdir())}" if self.lock.is_dir() else "absent")) + if (self.lock / "pid").exists(): + out.append(f"== lock pid file: {(self.lock / 'pid').read_text()!r}") + for name in ("children", "copies"): + f = self.run_dir / name + out.append(f"== {name}:\n" + (f.read_text().rstrip() if f.exists() else "(none)")) + out.append("== processes of this test:") + out += [proc_line(pid) for pid in sorted(procs_with(str(self.tmp), str(self.tmp)) + self.children())] + return "\n".join(out) def test_stop_kills_the_process_group(self): self.ctl("start") From b0835d1068a286d664a8731104e23b09df83bd6d Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:16:53 -0300 Subject: [PATCH 2/6] test(ctl): add the copies' output and the status verdict to the failure dump --- board/tests/test_ctl.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/board/tests/test_ctl.py b/board/tests/test_ctl.py index b3ac8a3..4220e34 100644 --- a/board/tests/test_ctl.py +++ b/board/tests/test_ctl.py @@ -260,7 +260,8 @@ def test_concurrent_starts_run_one_copy(self): if c["ended"] is None and c["p"].poll() is not None: c["ended"] = time.monotonic() - t0 time.sleep(0.005) - self.assertTrue(self.wait_for(lambda: len(self.children()) >= self.copies), self.dump(calls)) + if not self.wait_for(lambda: len(self.children()) >= self.copies): + self.fail(f"{len(self.children())} copies started, {self.copies} expected\n" + self.dump(calls)) time.sleep(0.3) if len(self.children()) != self.copies or len(copies_running(self.marker, str(self.tmp))) != self.copies: self.fail(f"{len(self.children())} copies started, {self.copies} expected\n" + self.dump(calls)) @@ -282,6 +283,11 @@ def dump(self, calls): for name in ("children", "copies"): f = self.run_dir / name out.append(f"== {name}:\n" + (f.read_text().rstrip() if f.exists() else "(none)")) + for f in sorted(self.run_dir.glob("*.out")) + sorted(self.run_dir.glob("*.log")): + out.append(f"== output of the copy, {f.name}:\n" + f.read_text(errors="replace").rstrip()) + status = subprocess.run(["sh", "-x", str(REPO / self.script), "status"], env={**self.env, "PS4": "+ "}, + capture_output=True, text=True, timeout=60) + out.append(f"== status now: rc={status.returncode}\n" + (status.stdout + status.stderr).rstrip()) out.append("== processes of this test:") out += [proc_line(pid) for pid in sorted(procs_with(str(self.tmp), str(self.tmp)) + self.children())] return "\n".join(out) From 482bfb885afbf2cb4af34a68dd29c70c559f2195 Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:22:35 -0300 Subject: [PATCH 3/6] ci: repeat the concurrent start tests 20 times per leg for now --- .github/workflows/ci.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 54094a4..ff8353c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,6 +22,16 @@ jobs: python-version: ${{ matrix.python }} - name: Board tests run: python -m unittest discover -s board/tests -t board -v + - name: Concurrent starts, repeated (temporary, to catch the flake with its evidence) + if: ${{ !cancelled() }} + working-directory: board + run: | + fails=0 + for i in $(seq 20); do + python -m unittest -k concurrent tests.test_ctl || fails=$((fails + 1)) + done + echo "rounds with a failure: $fails of 20" + [ "$fails" -eq 0 ] hygiene: runs-on: ubuntu-latest From fe7945860ca17212661c428fdefc8a27d28ec7d9 Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:44:31 -0300 Subject: [PATCH 4/6] fix(scripts): decide the ctl lock by a noclobber pid file, not by mkdir's exit status uutils mkdir, the default mkdir on Ubuntu 26.04 (ubuntu-latest), reports success to a call whose directory a concurrent call created a moment before: in a 3-way race on a fresh path, more than one call succeeded in 830 of 2000 rounds (GNU mkdir: 0). Two concurrent starts then both held the lock and both started a copy. A call now holds $LOCK (and $LOCK.reclaim) only when it also creates the pid file inside under noclobber, which the shell opens with O_EXCL. A call that loses leaves the directory alone, and release removes the file, then the directory, never recursively. --- board/board-ctl.sh | 49 +++++++++++++++++++++++++-------------- board/tests/test_ctl.py | 8 +++++++ scripts/gh-runners-ctl.sh | 49 +++++++++++++++++++++++++-------------- scripts/runner-ctl.sh | 49 +++++++++++++++++++++++++-------------- 4 files changed, 104 insertions(+), 51 deletions(-) diff --git a/board/board-ctl.sh b/board/board-ctl.sh index bba86f7..c69d8a4 100755 --- a/board/board-ctl.sh +++ b/board/board-ctl.sh @@ -41,22 +41,28 @@ alive() { } # --- lock: the same text in scripts/gh-runners-ctl.sh, scripts/runner-ctl.sh and board/board-ctl.sh --- -# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). mkdir is atomic, so -# whoever creates $LOCK holds it. The holder then writes " " to $LOCK/pid, being field -# 22 of /proc//stat (when that process started, in clock ticks since boot). The lock is stale only -# when that process is gone: the pid is dead, or now belongs to a process with another start time (pids -# are reused, after a reboot above all, and the lock directory survives one). A restart holds it for as -# long as it takes. +# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). The lock is the +# directory $LOCK with the file $LOCK/pid in it, " " of the holder, being field 22 of +# /proc//stat (when that process started, in clock ticks since boot). A call holds the lock when it +# creates $LOCK and then creates $LOCK/pid itself under noclobber (set -C: the shell opens the file with +# O_EXCL, so of several calls exactly one creates it). The exit status of mkdir alone does not decide: +# uutils mkdir, the default mkdir on Ubuntu 26.04, also reports success to a call whose directory another +# call created a moment before. A call that loses leaves the directory alone: it is the winner's. +# The lock is stale only when its holder is gone: the pid is dead, or now belongs to a process with another +# start time (pids are reused, after a reboot above all, and the lock directory survives one). A restart +# holds it for as long as it takes. # With no readable pid (the holder died between mkdir and writing it, or an older version of these # scripts, which wrote none, holds it), the lock is stale once find -mmin +LOCK_ORPHAN_MIN matches it # (GNU find counts whole minutes: from 11 minutes old; the old one-minute rule fired at 2). Those # older versions held it for a whole restart: up to 30 s per runner to stop it, plus a check-repo.sh call # per runner, which has no timeout of its own. Ten minutes covers a restart of several runners over a # slow link; eleven is the longest such a leftover can block the scripts. -# A stale lock is removed only under a second lock, $LOCK.reclaim, after checking again inside it: two -# callers that both saw it stale would otherwise both remove it, the second one removing the lock the -# first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was left by -# a call killed while reclaiming, and is removed. +# A stale lock is removed only under a second lock, $LOCK.reclaim, taken the same way, after checking again +# inside it: two callers that both saw it stale would otherwise both remove it, the second one removing the +# lock the first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was +# left by a call killed while reclaiming (empty when an older version left it), and is removed. +# Both are released file first, then directory, never recursively: a call that created its pid file in a +# directory being released keeps it, and the rmdir fails. # Residual risks: two callers removing such a leftover $LOCK.reclaim in the same instant can both # reclaim. After a reboot, a process that gets the old holder's pid and started in the same clock tick # since boot passes for the holder; calls then fail with "another call holds" until $LOCK is removed. @@ -83,32 +89,41 @@ lock_stale() { [ -n "$lock_start" ] && [ "$(proc_start "$lock_pid")" != "$lock_start" ] } +# take directory $1: create it, then $1/pid under noclobber; true only for the call that created $1/pid +lock_make() { + mkdir "$1" 2>/dev/null && (set -C; echo "$LOCK_ME" >"$1/pid") 2>/dev/null +} + +# release directory $1: its pid file, then the directory itself, and only if it is still empty +lock_free() { + rm -f "$1/pid" && rmdir "$1" 2>/dev/null +} + take_lock() { + LOCK_ME="$$ $(proc_start $$)" i=0 - until mkdir "$LOCK" 2>/dev/null; do + until lock_make "$LOCK"; do if lock_stale; then - if mkdir "$LOCK.reclaim" 2>/dev/null; then + if lock_make "$LOCK.reclaim"; then # nobody else removes $LOCK while $LOCK.reclaim is ours, so this second look is final if lock_stale; then rm -rf "$LOCK"; fi - rmdir "$LOCK.reclaim" + lock_free "$LOCK.reclaim" [ -d "$LOCK" ] || continue elif [ -n "$(find "$LOCK.reclaim" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then - rmdir "$LOCK.reclaim" 2>/dev/null && continue + lock_free "$LOCK.reclaim" && continue fi fi i=$((i + 1)) if [ "$i" -ge 30 ]; then echo "$CTL: another call holds $LOCK" >&2; exit 1; fi sleep 1 done - LOCK_ME="$$ $(proc_start $$)" - echo "$LOCK_ME" >"$LOCK/pid" trap drop_lock EXIT trap 'exit 1' INT TERM HUP } # release $LOCK, only while it is still ours drop_lock() { - [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && rm -f "$LOCK/pid" && rmdir "$LOCK" 2>/dev/null + [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && lock_free "$LOCK" } # --- end lock --- diff --git a/board/tests/test_ctl.py b/board/tests/test_ctl.py index 4220e34..2760b88 100644 --- a/board/tests/test_ctl.py +++ b/board/tests/test_ctl.py @@ -223,6 +223,14 @@ def test_concurrent_starts_over_a_stale_lock_run_one_copy(self): self.make_lock(f"{dead.pid} 1") self.test_concurrent_starts_run_one_copy() + def test_concurrent_starts_with_a_mkdir_that_cannot_lose_run_one_copy(self): + # uutils mkdir (Ubuntu 26.04) reports success to a call whose directory a concurrent call created a + # moment before; this one always does: the pid file, created with noclobber, has to decide alone + mkdir = self.tmp / "bin" / "mkdir" + mkdir.write_text(f'#!/bin/sh\n{shutil.which("mkdir")} "$@" 2>/dev/null\nexit 0\n') + mkdir.chmod(0o755) + self.test_concurrent_starts_run_one_copy() + def test_start_during_an_exec_runs_one_copy(self): # the second start comes while the first copy's command line reads empty: still running, not stale nohup = self.tmp / "bin" / "nohup" diff --git a/scripts/gh-runners-ctl.sh b/scripts/gh-runners-ctl.sh index bd4d126..3cf8299 100755 --- a/scripts/gh-runners-ctl.sh +++ b/scripts/gh-runners-ctl.sh @@ -45,22 +45,28 @@ alive() { } # --- lock: the same text in scripts/gh-runners-ctl.sh, scripts/runner-ctl.sh and board/board-ctl.sh --- -# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). mkdir is atomic, so -# whoever creates $LOCK holds it. The holder then writes " " to $LOCK/pid, being field -# 22 of /proc//stat (when that process started, in clock ticks since boot). The lock is stale only -# when that process is gone: the pid is dead, or now belongs to a process with another start time (pids -# are reused, after a reboot above all, and the lock directory survives one). A restart holds it for as -# long as it takes. +# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). The lock is the +# directory $LOCK with the file $LOCK/pid in it, " " of the holder, being field 22 of +# /proc//stat (when that process started, in clock ticks since boot). A call holds the lock when it +# creates $LOCK and then creates $LOCK/pid itself under noclobber (set -C: the shell opens the file with +# O_EXCL, so of several calls exactly one creates it). The exit status of mkdir alone does not decide: +# uutils mkdir, the default mkdir on Ubuntu 26.04, also reports success to a call whose directory another +# call created a moment before. A call that loses leaves the directory alone: it is the winner's. +# The lock is stale only when its holder is gone: the pid is dead, or now belongs to a process with another +# start time (pids are reused, after a reboot above all, and the lock directory survives one). A restart +# holds it for as long as it takes. # With no readable pid (the holder died between mkdir and writing it, or an older version of these # scripts, which wrote none, holds it), the lock is stale once find -mmin +LOCK_ORPHAN_MIN matches it # (GNU find counts whole minutes: from 11 minutes old; the old one-minute rule fired at 2). Those # older versions held it for a whole restart: up to 30 s per runner to stop it, plus a check-repo.sh call # per runner, which has no timeout of its own. Ten minutes covers a restart of several runners over a # slow link; eleven is the longest such a leftover can block the scripts. -# A stale lock is removed only under a second lock, $LOCK.reclaim, after checking again inside it: two -# callers that both saw it stale would otherwise both remove it, the second one removing the lock the -# first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was left by -# a call killed while reclaiming, and is removed. +# A stale lock is removed only under a second lock, $LOCK.reclaim, taken the same way, after checking again +# inside it: two callers that both saw it stale would otherwise both remove it, the second one removing the +# lock the first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was +# left by a call killed while reclaiming (empty when an older version left it), and is removed. +# Both are released file first, then directory, never recursively: a call that created its pid file in a +# directory being released keeps it, and the rmdir fails. # Residual risks: two callers removing such a leftover $LOCK.reclaim in the same instant can both # reclaim. After a reboot, a process that gets the old holder's pid and started in the same clock tick # since boot passes for the holder; calls then fail with "another call holds" until $LOCK is removed. @@ -87,32 +93,41 @@ lock_stale() { [ -n "$lock_start" ] && [ "$(proc_start "$lock_pid")" != "$lock_start" ] } +# take directory $1: create it, then $1/pid under noclobber; true only for the call that created $1/pid +lock_make() { + mkdir "$1" 2>/dev/null && (set -C; echo "$LOCK_ME" >"$1/pid") 2>/dev/null +} + +# release directory $1: its pid file, then the directory itself, and only if it is still empty +lock_free() { + rm -f "$1/pid" && rmdir "$1" 2>/dev/null +} + take_lock() { + LOCK_ME="$$ $(proc_start $$)" i=0 - until mkdir "$LOCK" 2>/dev/null; do + until lock_make "$LOCK"; do if lock_stale; then - if mkdir "$LOCK.reclaim" 2>/dev/null; then + if lock_make "$LOCK.reclaim"; then # nobody else removes $LOCK while $LOCK.reclaim is ours, so this second look is final if lock_stale; then rm -rf "$LOCK"; fi - rmdir "$LOCK.reclaim" + lock_free "$LOCK.reclaim" [ -d "$LOCK" ] || continue elif [ -n "$(find "$LOCK.reclaim" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then - rmdir "$LOCK.reclaim" 2>/dev/null && continue + lock_free "$LOCK.reclaim" && continue fi fi i=$((i + 1)) if [ "$i" -ge 30 ]; then echo "$CTL: another call holds $LOCK" >&2; exit 1; fi sleep 1 done - LOCK_ME="$$ $(proc_start $$)" - echo "$LOCK_ME" >"$LOCK/pid" trap drop_lock EXIT trap 'exit 1' INT TERM HUP } # release $LOCK, only while it is still ours drop_lock() { - [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && rm -f "$LOCK/pid" && rmdir "$LOCK" 2>/dev/null + [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && lock_free "$LOCK" } # --- end lock --- diff --git a/scripts/runner-ctl.sh b/scripts/runner-ctl.sh index 06edafe..daafba0 100755 --- a/scripts/runner-ctl.sh +++ b/scripts/runner-ctl.sh @@ -69,22 +69,28 @@ alive() { } # --- lock: the same text in scripts/gh-runners-ctl.sh, scripts/runner-ctl.sh and board/board-ctl.sh --- -# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). mkdir is atomic, so -# whoever creates $LOCK holds it. The holder then writes " " to $LOCK/pid, being field -# 22 of /proc//stat (when that process started, in clock ticks since boot). The lock is stale only -# when that process is gone: the pid is dead, or now belongs to a process with another start time (pids -# are reused, after a reboot above all, and the lock directory survives one). A restart holds it for as -# long as it takes. +# Serialises start/stop/restart (Termux:Boot and an SSH session at the same moment). The lock is the +# directory $LOCK with the file $LOCK/pid in it, " " of the holder, being field 22 of +# /proc//stat (when that process started, in clock ticks since boot). A call holds the lock when it +# creates $LOCK and then creates $LOCK/pid itself under noclobber (set -C: the shell opens the file with +# O_EXCL, so of several calls exactly one creates it). The exit status of mkdir alone does not decide: +# uutils mkdir, the default mkdir on Ubuntu 26.04, also reports success to a call whose directory another +# call created a moment before. A call that loses leaves the directory alone: it is the winner's. +# The lock is stale only when its holder is gone: the pid is dead, or now belongs to a process with another +# start time (pids are reused, after a reboot above all, and the lock directory survives one). A restart +# holds it for as long as it takes. # With no readable pid (the holder died between mkdir and writing it, or an older version of these # scripts, which wrote none, holds it), the lock is stale once find -mmin +LOCK_ORPHAN_MIN matches it # (GNU find counts whole minutes: from 11 minutes old; the old one-minute rule fired at 2). Those # older versions held it for a whole restart: up to 30 s per runner to stop it, plus a check-repo.sh call # per runner, which has no timeout of its own. Ten minutes covers a restart of several runners over a # slow link; eleven is the longest such a leftover can block the scripts. -# A stale lock is removed only under a second lock, $LOCK.reclaim, after checking again inside it: two -# callers that both saw it stale would otherwise both remove it, the second one removing the lock the -# first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was left by -# a call killed while reclaiming, and is removed. +# A stale lock is removed only under a second lock, $LOCK.reclaim, taken the same way, after checking again +# inside it: two callers that both saw it stale would otherwise both remove it, the second one removing the +# lock the first had just taken. $LOCK.reclaim is held for milliseconds; one that find -mmin +1 matches was +# left by a call killed while reclaiming (empty when an older version left it), and is removed. +# Both are released file first, then directory, never recursively: a call that created its pid file in a +# directory being released keeps it, and the rmdir fails. # Residual risks: two callers removing such a leftover $LOCK.reclaim in the same instant can both # reclaim. After a reboot, a process that gets the old holder's pid and started in the same clock tick # since boot passes for the holder; calls then fail with "another call holds" until $LOCK is removed. @@ -111,32 +117,41 @@ lock_stale() { [ -n "$lock_start" ] && [ "$(proc_start "$lock_pid")" != "$lock_start" ] } +# take directory $1: create it, then $1/pid under noclobber; true only for the call that created $1/pid +lock_make() { + mkdir "$1" 2>/dev/null && (set -C; echo "$LOCK_ME" >"$1/pid") 2>/dev/null +} + +# release directory $1: its pid file, then the directory itself, and only if it is still empty +lock_free() { + rm -f "$1/pid" && rmdir "$1" 2>/dev/null +} + take_lock() { + LOCK_ME="$$ $(proc_start $$)" i=0 - until mkdir "$LOCK" 2>/dev/null; do + until lock_make "$LOCK"; do if lock_stale; then - if mkdir "$LOCK.reclaim" 2>/dev/null; then + if lock_make "$LOCK.reclaim"; then # nobody else removes $LOCK while $LOCK.reclaim is ours, so this second look is final if lock_stale; then rm -rf "$LOCK"; fi - rmdir "$LOCK.reclaim" + lock_free "$LOCK.reclaim" [ -d "$LOCK" ] || continue elif [ -n "$(find "$LOCK.reclaim" -maxdepth 0 -mmin +1 2>/dev/null)" ]; then - rmdir "$LOCK.reclaim" 2>/dev/null && continue + lock_free "$LOCK.reclaim" && continue fi fi i=$((i + 1)) if [ "$i" -ge 30 ]; then echo "$CTL: another call holds $LOCK" >&2; exit 1; fi sleep 1 done - LOCK_ME="$$ $(proc_start $$)" - echo "$LOCK_ME" >"$LOCK/pid" trap drop_lock EXIT trap 'exit 1' INT TERM HUP } # release $LOCK, only while it is still ours drop_lock() { - [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && rm -f "$LOCK/pid" && rmdir "$LOCK" 2>/dev/null + [ "$(cat "$LOCK/pid" 2>/dev/null)" = "$LOCK_ME" ] && lock_free "$LOCK" } # --- end lock --- From 30dba2ba6b4f025163286d8549d3f6c17b2293c0 Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:44:39 -0300 Subject: [PATCH 5/6] ci: print which mkdir the runner has in the repeated step --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ff8353c..78fa9dc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,6 +26,7 @@ jobs: if: ${{ !cancelled() }} working-directory: board run: | + mkdir --version | head -n 1 fails=0 for i in $(seq 20); do python -m unittest -k concurrent tests.test_ctl || fails=$((fails + 1)) From b7bf73ba3314a3f64d7f13c4bb722e30f49fa888 Mon Sep 17 00:00:00 2001 From: agent-kit-startup <273612197+agent-kit-startup@users.noreply.github.com> Date: Mon, 30 Nov 2026 17:58:19 -0300 Subject: [PATCH 6/6] ci: drop the temporary repeated concurrent start step --- .github/workflows/ci.yml | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 78fa9dc..54094a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,17 +22,6 @@ jobs: python-version: ${{ matrix.python }} - name: Board tests run: python -m unittest discover -s board/tests -t board -v - - name: Concurrent starts, repeated (temporary, to catch the flake with its evidence) - if: ${{ !cancelled() }} - working-directory: board - run: | - mkdir --version | head -n 1 - fails=0 - for i in $(seq 20); do - python -m unittest -k concurrent tests.test_ctl || fails=$((fails + 1)) - done - echo "rounds with a failure: $fails of 20" - [ "$fails" -eq 0 ] hygiene: runs-on: ubuntu-latest