Files
gstack/bin/gstack-lock.sh
T
Garry Tanandcapy-ai[bot] bcece3fbcd 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>
2026-11-17 09:05:25 +00:00

49 lines
2.5 KiB
Bash

# shellcheck shell=bash
# gstack-lock.sh — the one way gstack shell code takes a lock directory.
#
# Source it, then call:
#
# gstack_lock_take DIR [FILE] [CONTENT]
# Take the lock DIR by creating DIR/FILE (default `pid`) holding CONTENT
# (default $$). Returns 0 when this shell took the lock, 1 when it is held
# (DIR already existed, or another process created FILE first), 2 when DIR
# could not be created (missing or unwritable parent, read-only filesystem)
# and 3 when DIR was made but FILE could not be created or fully written
# in it (disk full, quota, a file-size limit; FILE and DIR are removed
# again, so a failed write never leaves a lock nobody holds). Neither 2
# nor 3 is evidence of a competing holder.
#
# Why not `mkdir DIR && ...`: mkdir's exit status is not a lock. Rust
# coreutils (uutils) 0.8.0, the mkdir on Ubuntu 26.04 and on GitHub's
# ubuntu-latest from 2026-11-09, reports success when another process created
# the directory first, so two racing callers could both think they held it.
# Here mkdir only makes the directory and its status is ignored. The arbiter is
# the shell's noclobber redirect, which bash, zsh and dash open with
# O_CREAT|O_EXCL themselves (no external binary), so the kernel lets exactly one
# caller create FILE. flock(1) is not used: stock macOS does not ship it.
#
# A DIR that exists before the call is never taken, even with no FILE in it, so
# a lock left by a holder killed between mkdir and FILE stays held until the
# caller's own stale-lock recovery removes it, exactly as before. Callers keep
# their own waiting, staleness, takeover and release logic; the owner still
# releases with `rm -rf DIR`. Safe under set -e and set -u, and noclobber never
# leaks into the caller's shell.
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
}