Skip to content

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

Go

fix(dev-env): keep the allocation lock exclusive under uutils mkdir (#10606)

pseudoyu215b634e9c5d
diff --git a/scripts/dev-env.sh b/scripts/dev-env.sh
index a7f3d5308d..15a492d2de 100755
--- a/scripts/dev-env.sh
+++ b/scripts/dev-env.sh
@@ -93,10 +93,17 @@ expires_at_after_hours() {
# flock is not on a stock macOS, so the lock is an atomic mkdir. The holder's
# pid is recorded so a lock left behind by a killed process is recoverable
# instead of wedging every later command.
+# mkdir's exit status alone is not the claim: uutils mkdir (Ubuntu 26.04)
+# also reports success to a caller that lost the race to create the directory.
+# Creating the pid file exclusively decides the holder.
+try_lock() {
+ mkdir "$LOCK_DIR" 2>/dev/null && (set -o noclobber; printf '%s\n' "$$" > "$LOCK_DIR/pid") 2>/dev/null
+}
+
acquire_lock() {
local waited=0
mkdir -p "$DEV_HOME"
- while ! mkdir "$LOCK_DIR" 2>/dev/null; do
+ while ! try_lock; do
local holder=""
holder="$(cat "$LOCK_DIR/pid" 2>/dev/null || true)"
if [ -n "$holder" ] && ! kill -0 "$holder" 2>/dev/null; then
@@ -107,10 +114,13 @@ acquire_lock() {
[ "$waited" -lt 100 ] || die "Timed out waiting for the allocation lock at $LOCK_DIR. If nothing else is running, remove it."
sleep 0.1
done
- printf '%s\n' "$$" > "$LOCK_DIR/pid"
}
-release_lock() { rm -rf "$LOCK_DIR"; }
+release_lock() {
+ rm -f "$LOCK_DIR/pid"
+ # Once pid is gone the next holder may already have taken this directory.
+ rmdir "$LOCK_DIR" 2>/dev/null || true
+}
# ---------------------------------------------------------------- registry ---
diff --git a/scripts/dev-env.test.sh b/scripts/dev-env.test.sh
index e2e07f5279..0eb86b0447 100644
--- a/scripts/dev-env.test.sh
+++ b/scripts/dev-env.test.sh
@@ -592,4 +592,43 @@ printf 'n\n' | dev_env destroy orphan-902 > "$out" 2>&1 || fail "declining destr
require_contains "$out" "Cancelled."
[ -d "$MULTICA_DEV_HOME/envs/orphan-902" ] || fail "declined destroy removed the environment anyway"
+# ---------------------------------------------------------------------------
+# uutils mkdir (Ubuntu 26.04) reports success to a caller that lost the race
+# to create a directory. A mkdir that succeeds on the held allocation lock must
+# not hand it to a second holder, or two `up`s allocate the same slot.
+# ---------------------------------------------------------------------------
+(
+ # shellcheck disable=SC1090
+ source "$root_dir/scripts/dev-env.sh"
+ command mkdir -p "$LOCK_DIR"
+ printf '%s\n' "$PPID" > "$LOCK_DIR/pid"
+ mkdir() { command mkdir "$@" 2>/dev/null || [ -d "$LOCK_DIR" ]; }
+ sleep() { :; }
+ if (acquire_lock) > "$out" 2>&1; then
+ fail "a mkdir that succeeded on the held lock directory let a second holder in"
+ fi
+ require_contains "$out" "Timed out waiting for the allocation lock"
+ [ "$(cat "$LOCK_DIR/pid")" = "$PPID" ] || fail "the lock holder's pid was overwritten"
+ command rm -rf "$LOCK_DIR"
+)
+
+# Releasing must drop only this holder's claim. Under uutils mkdir the next
+# caller can claim the directory as soon as the pid file is unlinked; deleting
+# the directory after that would hand the lock to a third caller.
+(
+ # shellcheck disable=SC1090
+ source "$root_dir/scripts/dev-env.sh"
+ acquire_lock
+ rm() {
+ command rm -f "$LOCK_DIR/pid"
+ # The next caller claims the lock between the unlink and the directory removal.
+ printf '%s\n' "$PPID" > "$LOCK_DIR/pid"
+ case "$1" in -r*) command rmdir "$LOCK_DIR" ;; esac
+ }
+ release_lock 2> "$out" || fail "release_lock failed when the next holder had already claimed the lock"
+ unset -f rm
+ [ "$(cat "$LOCK_DIR/pid" 2>/dev/null)" = "$PPID" ] || fail "release_lock removed the next holder's lock"
+ command rm -rf "$LOCK_DIR"
+)
+
echo "✓ dev-env.sh registry behaviour verified"