Make gstack's shell locks hold when mkdir reports a race it lost (GST-2247) (#4262)

* Make gstack's shell locks hold when mkdir reports a race it lost (GST-2247)

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Fix lock tests and fixtures for gstack_lock_take; trim setup-gbrain lock prose

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Ship the lock helper with every copied runtime and fixture that runs a lock

The Claude Code wrapper's copied runtime now includes bin/gstack-lock.sh, and
bin/gstack-render-claude.sh sources it beside itself, so gstack-config set
works in a copied runtime and the render lock is taken by every caller.
Test fixtures that copy single bin scripts now copy the helper too, and the
mkdir lock guard is registered as a repo-wide docs reader.

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Release a lock whose owner file could not be fully written

gstack_lock_take created the owner file with a noclobber redirect and, when
that redirect failed, treated an existing file as a competing holder. If the
file was created but the write then failed (disk full, quota, ulimit -f), the
helper found its own empty file, returned 1 and left a lock nobody held;
freeze's state lock has no stale recovery, so it stayed busy until removed by
hand. The helper now tells "opened but write failed" from "already held",
removes its own file and directory, and returns 3. The inline copy in
gstack-ff-update is updated byte for byte, and a new test under ulimit -f 0
fails on the old helper.

Review-Finding: s11/outside-voice#1 reader=outside-voice
Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

* Describe the lease lock as gstack_lock_take's O_EXCL owner file

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>

---------

Co-authored-by: capy-ai[bot] <230910855+capy-ai[bot]@users.noreply.github.com>
This commit is contained in:
Garry Tanandcapy-ai[bot] authored and GitHub committed 2026-11-17 09:05:25 +00:00
1 parent 76fad3edd9
commit bcece3fbcd
50 files changed
+621 -154

No files matched your search

+24 -4
View File
@@ -9,7 +9,8 @@
# the new revision (git show FETCH_HEAD:bin/gstack-ff-update) and run it in a
# private temp directory, so it sources nothing. The lock and the
# flatten-and-strip sanitizer are inlined; test/gstack-ff-update.test.ts pins
# them to their twins in bin/gstack-session-update.
# them to their twins in bin/gstack-session-update, and gstack_lock_take to
# bin/gstack-lock.sh.
#
# Inside an install-scoped lock (<git-common-dir>/gstack-ff.lock):
# 1. The working tree is captured as-is through a private index (tracked
@@ -115,18 +116,38 @@ LOCK_DIR="$COMMON/gstack-ff.lock"
LOCK_TTL_MINUTES=30
MYPID=$$
OWN_INDEX_LOCK="" TMP="" HB_PID=""
# Twin of bin/gstack-lock.sh (test-pinned): mkdir's exit status is not a lock
# on Ubuntu 26.04's uutils mkdir, so the pid file is created with O_EXCL.
gstack_lock_take() {
local _gl_dir="$1" _gl_file="${2:-pid}" _gl_content="${3:-$$}"
if [ -e "$_gl_dir" ] || [ -L "$_gl_dir" ]; then return 1; fi
mkdir "$_gl_dir" 2>/dev/null || :
if [ ! -d "$_gl_dir" ] || [ -L "$_gl_dir" ]; then
if [ -e "$_gl_dir" ] || [ -L "$_gl_dir" ]; then return 1; fi
return 2
fi
local _gl_opened
_gl_opened=$(set -o noclobber; { echo o; printf '%s\n' "$_gl_content" >&3; } 2>/dev/null 3> "$_gl_dir/$_gl_file") && return 0
if [ "$_gl_opened" = o ]; then
rm -f "$_gl_dir/$_gl_file"; rmdir "$_gl_dir" 2>/dev/null || :
return 3
fi
if [ -e "$_gl_dir/$_gl_file" ] || [ -L "$_gl_dir/$_gl_file" ] || [ ! -d "$_gl_dir" ]; then return 1; fi
rmdir "$_gl_dir" 2>/dev/null || :
return 3
}
lock_is_expired() {
_hb="$LOCK_DIR/pid"
[ -f "$_hb" ] || _hb="$LOCK_DIR"
[ -n "$(find "$_hb" -maxdepth 0 -mmin +$LOCK_TTL_MINUTES 2>/dev/null)" ]
}
lock_try() {
mkdir "$LOCK_DIR" 2>/dev/null && return 0
gstack_lock_take "$LOCK_DIR" pid "$MYPID" && return 0
_lp=$(cat "$LOCK_DIR/pid" 2>/dev/null)
if lock_is_expired || { [ "${_lp:-0}" -gt 0 ] 2>/dev/null && ! kill -0 "$_lp" 2>/dev/null; }; then
mv "$LOCK_DIR" "$LOCK_DIR.reap.$$" 2>/dev/null || return 1
rm -rf "$LOCK_DIR.reap.$$" 2>/dev/null
mkdir "$LOCK_DIR" 2>/dev/null && return 0
gstack_lock_take "$LOCK_DIR" pid "$MYPID" && return 0
fi
return 1
}
@@ -145,7 +166,6 @@ until lock_try; do
[ "$_wait" -gt 0 ] || refuse 75 ff-busy "another gstack update is moving $DIR (lock $LOCK_DIR held by pid $(cat "$LOCK_DIR/pid" 2>/dev/null || echo unknown)); nothing was changed, retry in a minute"
sleep 1; _wait=$((_wait - 1))
done
echo "$MYPID" > "$LOCK_DIR/pid" 2>/dev/null
trap cleanup_ff_update EXIT
trap 'exit 129' HUP
trap 'exit 130' INT