Skip to content

Commit 6f812a0

Browse files
authored
fix: prevent duplicate run IDs on false-success mkdir (#81)
Concurrent agent launches can allocate the same run ID when mkdir reports success after losing the race to create run.lock. Require both a successful exit and a nonempty creation receipt before entering the allocator, matching the existing metadata lock protection. The retry limit and cleanup protocol remain unchanged. The regression holds another writer's lock and models false-success mkdir. It verifies that allocation never enters the critical section, removes the held lock, or changes existing events. It fails on the original implementation and passes with the guard. This is the production allocator fix targeting current main. Its diff contains only lib/cmd/agents.sh and test/run_lock.bats. No benchmark workflow, readiness changes, worker-default changes or benchmark documentation are included. Benchmark PR #73 remains a separate, unmerged reference. Validation: - Local regression and full lint pass after updating from current origin/main. - [Fresh Linux/macOS CI](https://github.com/TSLsun/tmux-collie/actions/runs/41453065875) is green on current-main-updated head `0af5348`. - [Controlled experiment](https://github.com/TSLsun/tmux-collie/actions/runs/41376826676) tested the identical allocator/test patch: all 120 real concurrent-ID checks passed. Its separate Codex/Gemini readiness failures remain recorded; neither was a duplicate-ID failure. - The native Linux probe observed 17 status-0/no-creation-receipt mkdir exits in 50 rounds of 32 contenders; kernel controls had one winner per round. [Upstream mechanism](uutils/coreutils#13970). User authorization promotes this PR from an experimental candidate to a production fix for review. Merging remains a separate human decision; no automatic merge is enabled.
1 parent b8574d6 commit 6f812a0

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

‎lib/cmd/agents.sh‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ run_agent() {
1010
# Allocate and record a run in one per-task critical section. A subshell
1111
# keeps cleanup traps local to this launch, including callers like handoff.
1212
start_run() ( # task agent repo path argv...
13-
local id=$1 agent=$2 repo=$3 path=$4 lock run wid server pane i=0
13+
local id=$1 agent=$2 repo=$3 path=$4 lock receipt run wid server pane i=0
1414
shift 4
1515
lock="$TASKS_DIR/$id/run.lock"
16-
until mkdir "$lock" 2>/dev/null; do
16+
# As with metadata locks, require a creation receipt: some mkdir versions
17+
# can report success after losing a race to create an existing directory.
18+
while ! receipt=$(mkdir -v "$lock" 2>/dev/null) || [ -z "$receipt" ]; do
1719
i=$((i + 1))
1820
[ "$i" -lt 200 ] || die "run: timed out waiting for $lock"
1921
sleep 0.05

‎test/run_lock.bats‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bats
2+
3+
# Exercise the allocator's lock boundary without starting a tmux server.
4+
@test "run allocation: false-success mkdir cannot enter or remove another writer's lock" {
5+
source "$BATS_TEST_DIRNAME/../lib/cmd/agents.sh"
6+
TASKS_DIR="$BATS_TEST_TMPDIR/tasks"
7+
mkdir -p "$TASKS_DIR/x/run.lock"
8+
printf 'existing events\n' >"$TASKS_DIR/x/events.log"
9+
cp "$TASKS_DIR/x/events.log" "$BATS_TEST_TMPDIR/original"
10+
# Model uutils mkdir returning success after losing a creation race.
11+
# Real mkdir still emits a verbose receipt only when it creates the lock.
12+
mkdir() { command mkdir "$@" || return 0; }
13+
sleep() { :; }
14+
die() { printf 'collie: %s\n' "$*" >&2; exit 1; }
15+
ensure_session() { touch "$BATS_TEST_TMPDIR/entered"; return 1; }
16+
17+
run start_run x stub A "$BATS_TEST_TMPDIR" stub
18+
[ "$status" -ne 0 ]
19+
[[ "$output" == *'timed out waiting for'* ]]
20+
[ ! -e "$BATS_TEST_TMPDIR/entered" ]
21+
[ -d "$TASKS_DIR/x/run.lock" ]
22+
cmp "$TASKS_DIR/x/events.log" "$BATS_TEST_TMPDIR/original"
23+
}

0 commit comments

Comments
 (0)