Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(scripts): keep the agent-environment lock exclusive under uutils mkdir (MUL-9293) (#10630)

Jiayuan Zhang0815568229cc
diff --git a/scripts/agent-environment.sh b/scripts/agent-environment.sh
index ab0a6c0be0..a6b5e51cac 100755
--- a/scripts/agent-environment.sh
+++ b/scripts/agent-environment.sh
@@ -57,10 +57,19 @@ other_component() {
# An atomic mkdir, as in dev-env.sh: flock is not on a stock macOS. A lock
# whose holder is gone is taken over, so a killed stop cannot wedge the next.
+# mkdir's exit status alone does not decide the holder: uutils mkdir (Ubuntu
+# 26.04) can tell two simultaneous callers that each created the directory.
+# The holder is whoever then creates `held` in it, which bash's noclobber does
+# with O_EXCL. The directory stays the lock, so a stop and a `start` still
+# running an earlier version of this script keep excluding each other.
+try_lock() {
+ mkdir "$LOCK_DIR" 2>/dev/null && (set -C; : > "$LOCK_DIR/held") 2>/dev/null
+}
+
acquire_lock() {
local holder marker waited=0
mkdir -p "$STATE_DIR"
- while ! mkdir "$LOCK_DIR" 2>/dev/null; do
+ while ! try_lock; do
# Removing a PID-specific marker is the reaper's atomic claim. Only
# its winner may rmdir the old lock; another stale observer cannot
# remove a new holder's marker (or its directory).
@@ -71,6 +80,7 @@ acquire_lock() {
*) holder="${marker##*/owner-}" ;;
esac
if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null && rm "$marker" 2>/dev/null; then
+ rm -f "$LOCK_DIR/held"
rmdir "$LOCK_DIR" 2>/dev/null || true
fi
done
@@ -82,7 +92,9 @@ acquire_lock() {
}
release_lock() {
- rm "$LOCK_DIR/owner-$$" && rmdir "$LOCK_DIR"
+ rm "$LOCK_DIR/owner-$$" "$LOCK_DIR/held"
+ # Once held is gone the next holder may already have taken this directory.
+ rmdir "$LOCK_DIR" 2>/dev/null || true
}
claim_file() { printf '%s/%s.claim' "$STATE_DIR" "$1"; }
diff --git a/scripts/agent-environment.test.sh b/scripts/agent-environment.test.sh
index 65edb8b670..b29ab6ef15 100644
--- a/scripts/agent-environment.test.sh
+++ b/scripts/agent-environment.test.sh
@@ -129,6 +129,29 @@ wait "$second" || fail "stop desktop failed"
[ "$(count "down web")" = 1 ] && [ "$(count "down desktop")" = 1 ] || fail "concurrent stops did not stop both components"
[ "$(count destroy)" = 1 ] || fail "concurrent stops destroyed $(count destroy) times, want once"
+# ...even where mkdir is not exclusive. uutils mkdir (Ubuntu 26.04) can tell
+# two simultaneous callers that each created the same directory; this one
+# tells every caller so.
+new_repo concurrent-mkdir
+touch "$repo/registered"
+live_claim web
+live_claim desktop
+mkdir -p "$repo/bin"
+cat > "$repo/bin/mkdir" <<'EOF'
+#!/usr/bin/env bash
+args=()
+for arg in "$@"; do [ "$arg" = -p ] || args+=("$arg"); done
+exec "$REAL_MKDIR" -p "${args[@]}"
+EOF
+chmod +x "$repo/bin/mkdir"
+PATH="$repo/bin:$PATH" REAL_MKDIR="$(command -v mkdir)" bash "$repo/scripts/agent-environment.sh" stop web > /dev/null &
+first=$!
+PATH="$repo/bin:$PATH" REAL_MKDIR="$(command -v mkdir)" bash "$repo/scripts/agent-environment.sh" stop desktop > /dev/null &
+second=$!
+wait "$first" || fail "stop web failed with a non-exclusive mkdir"
+wait "$second" || fail "stop desktop failed with a non-exclusive mkdir"
+[ "$(count destroy)" = 1 ] || fail "with a non-exclusive mkdir, concurrent stops destroyed $(count destroy) times, want once"
+
# 4. The daemon signals start after running stop. start's own cleanup then
# finds nothing left to do.
new_repo signalled
@@ -273,4 +296,31 @@ touch "$repo/.multica/agent-environments/lock.d/owner-$dead"
bash "$repo/scripts/agent-environment.sh" stop web > /dev/null
[ "$(count destroy)" = 1 ] || fail "a dead PID-specific lock prevented cleanup"
+# 10. A `start` from before the checkout was updated keeps running the script
+# it loaded, and its cleanup locks the way that version did: lock.d with only
+# its owner marker. A stop from the updated script waits for it.
+new_repo mixed-version
+touch "$repo/registered"
+lock_dir="$repo/.multica/agent-environments/lock.d"
+mkdir -p "$lock_dir"
+sleep 300 &
+old_holder=$!
+pids+=("$old_holder")
+touch "$lock_dir/owner-$old_holder"
+mv "$repo/scripts/dev-env.sh" "$repo/scripts/dev-env.original"
+cat > "$repo/scripts/dev-env.sh" <<'EOF'
+#!/usr/bin/env bash
+set -eu
+[ ! -e "$RACE_LOCK/owner-$RACE_HOLDER" ] || touch "$RACE_LOCK/../entered-while-held"
+exec bash "$(dirname "$0")/dev-env.original" "$@"
+EOF
+RACE_LOCK="$lock_dir" RACE_HOLDER="$old_holder" bash "$repo/scripts/agent-environment.sh" stop desktop > /dev/null &
+stopping=$!
+sleep 1
+[ ! -e "$repo/.multica/agent-environments/entered-while-held" ] || fail "a stop entered beside an earlier version's lock holder"
+rm "$lock_dir/owner-$old_holder" && rmdir "$lock_dir"
+stop_job "$old_holder"
+wait "$stopping" || fail "stop behind an earlier version's lock failed"
+[ "$(count "down desktop")" = 1 ] && [ "$(count destroy)" = 1 ] || fail "stop did not clean up once the earlier version released its lock"
+
echo "✓ agent-environment.sh lifecycle verified"