Skip to content

Commit 96e2790

Browse files
authored
fix: prevent concurrent task metadata corruption (#64)
Concurrent `fm_set` and `fm_del` calls shared `TASK.md.tmp` and performed unlocked read-modify-write operations, causing lost updates and missing-temp rename errors. Serialize each complete mutation with a Bash 3.2-compatible per-file lock, write through a unique adjacent temporary file, and preserve the original on failures. Cleanup traps release locks and temporary files on failure and termination while preserving caller traps and file permissions. Lock acquisition is bounded; SIGKILL or a machine crash can leave a stale lock requiring manual cleanup after confirming no writer owns it. Require both success and a verbose creation receipt from `mkdir` to handle Ubuntu 26.04's [uutils race bug](uutils/coreutils#13970), where losing racers can incorrectly exit successfully. The concurrent regression reproduced this on Ubuntu 26.04, and a deterministic false-success regression protects the workaround. Validation: - `make check`: lint and all 218 tests passed locally on macOS after pulling the latest main. - Seven metadata regressions cover 40 concurrent setter/deleter pairs, false-success lock acquisition, failure preservation, termination cleanup, contention, permissions, and caller traps. - All seven metadata tests also passed under Bash 3.2 and in Ubuntu 26.04 with eight parallel Bats jobs. - The initial regressions failed against the original implementation; `git diff --check` passed.
1 parent 01f8b5f commit 96e2790

2 files changed

Lines changed: 177 additions & 10 deletions

File tree

‎lib/tasks.sh‎

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,61 @@ fm_get() { # file key
3737
}
3838

3939
fm_set() {
40-
VAL=$3 awk -v k="$2" '
41-
BEGIN { v = ENVIRON["VAL"] }
42-
NR == 1 { print; next }
43-
!end && $0 == "---" { if (!found) print k ": " v; end = 1; print; next }
44-
!end && index($0, k ":") == 1 { print k ": " v; found = 1; next }
45-
{ print }' "$1" >"$1.tmp"
46-
mv "$1.tmp" "$1"
40+
fm_mutate "$1" set "$2" "$3"
4741
}
4842

4943
fm_del() { # file key
50-
awk -v k="$2" 'NR > 1 && !end && $0 == "---" { end = 1 }
51-
NR == 1 || end || index($0, k ":") != 1' "$1" >"$1.tmp"
52-
mv "$1.tmp" "$1"
44+
fm_mutate "$1" del "$2"
5345
}
5446

47+
# Serialize the entire read/modify/rename, not just the rename. A subshell keeps
48+
# cleanup traps local to this mutation (including when called under `if`).
49+
# TASK.md.lock is independent of the lifecycle allocator's run.lock; callers
50+
# may hold run.lock while updating metadata, never the reverse.
51+
fm_mutate() (
52+
local file=$1 op=$2 key=$3 value=${4:-} lock receipt tmp='' attempts=0
53+
# Resolve directory aliases such as a worktree's .collie symlink so every
54+
# writer uses the same lock. Keep the temp beside the file for atomic rename.
55+
case $file in */*) ;; *) file="./$file" ;; esac
56+
file="$(cd "${file%/*}" && pwd -P)/${file##*/}" || return 1
57+
lock="$file.lock"
58+
# uutils mkdir on Ubuntu 26.04 can exit 0 after losing a creation race
59+
# (uutils/coreutils#13970). Its verbose receipt is emitted only on creation;
60+
# require both success and a receipt without parsing localized output.
61+
while ! receipt=$(mkdir -v "$lock" 2>/dev/null) || [ -z "$receipt" ]; do
62+
attempts=$((attempts + 1))
63+
if [ "$attempts" -ge 400 ]; then
64+
printf 'collie: timed out locking metadata %s\n' "$file" >&2
65+
return 1
66+
fi
67+
sleep 0.025
68+
done
69+
# Bash 3.2 unwinds locals before EXIT on a signal-triggered explicit exit.
70+
# Capture shell-quoted paths now, rather than looking up locals in the trap.
71+
# shellcheck disable=SC2064
72+
trap "rmdir $(printf '%q' "$lock")" EXIT
73+
trap 'exit 129' HUP
74+
trap 'exit 130' INT
75+
trap 'exit 143' TERM
76+
tmp=$(mktemp "$file.tmp.XXXXXX") || return 1
77+
# shellcheck disable=SC2064
78+
trap "rm -f $(printf '%q' "$tmp"); rmdir $(printf '%q' "$lock")" EXIT
79+
# Preserve the original permissions; never publish a partial transform.
80+
cp -p "$file" "$tmp" || return 1
81+
if [ "$op" = set ]; then
82+
VAL=$value awk -v k="$key" '
83+
BEGIN { v = ENVIRON["VAL"] }
84+
NR == 1 { print; next }
85+
!end && $0 == "---" { if (!found) print k ": " v; end = 1; print; next }
86+
!end && index($0, k ":") == 1 { print k ": " v; found = 1; next }
87+
{ print }' "$file" >"$tmp" || return 1
88+
else
89+
awk -v k="$key" 'NR > 1 && !end && $0 == "---" { end = 1 }
90+
NR == 1 || end || index($0, k ":") != 1' "$file" >"$tmp" || return 1
91+
fi
92+
mv -f "$tmp" "$file" || return 1
93+
)
94+
5595
# NOW: the current time in a strftime format (stamp_utc: in UTC). bash 4.2+
5696
# formats it without forking date; bash 3.2 still runs date.
5797
if [ $((BASH_VERSINFO[0] * 100 + BASH_VERSINFO[1])) -ge 402 ]; then

‎test/metadata.bats‎

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env bats
2+
3+
# These tests source only task helpers: no tmux server or user state is used.
4+
setup() {
5+
source "$BATS_TEST_DIRNAME/../lib/tasks.sh"
6+
f="$BATS_TEST_TMPDIR/TASK.md"
7+
printf '%s\n' --- 'title: original' 'priority: P2' --- '' 'Body: keep me' >"$f"
8+
cp "$f" "$BATS_TEST_TMPDIR/original"
9+
}
10+
11+
assert_clean() {
12+
[ ! -e "$f.lock" ]
13+
local leftovers
14+
leftovers=$(find "$BATS_TEST_TMPDIR" -name 'TASK.md.tmp*')
15+
[ -z "$leftovers" ]
16+
}
17+
18+
@test "metadata: concurrent setters and deleters preserve every independent update" {
19+
local i pid failures=0 pids=()
20+
for i in {1..40}; do fm_set "$f" "old$i" remove; done
21+
# Delay after the real read/transform to expose overlapping snapshots and
22+
# shared-temp renames reliably, rather than relying on scheduler luck.
23+
awk() { command awk "$@"; sleep 0.02; }
24+
for i in {1..40}; do
25+
(while [ ! -e "$BATS_TEST_TMPDIR/go" ]; do sleep 0.01; done
26+
fm_set "$f" "new$i" "value$i") &
27+
pids+=("$!")
28+
(while [ ! -e "$BATS_TEST_TMPDIR/go" ]; do sleep 0.01; done
29+
fm_del "$f" "old$i") &
30+
pids+=("$!")
31+
done
32+
touch "$BATS_TEST_TMPDIR/go"
33+
for pid in "${pids[@]}"; do wait "$pid" || failures=$((failures + 1)); done
34+
[ "$failures" -eq 0 ]
35+
for i in {1..40}; do
36+
fm_get "$f" "new$i"; [ "$FM" = "value$i" ]
37+
fm_get "$f" "old$i"; [ -z "$FM" ]
38+
done
39+
[ "$(command awk '$0 == "---" { n++ } END { print n }' "$f")" -eq 2 ]
40+
grep -qx 'title: original' "$f"
41+
grep -qx 'priority: P2' "$f"
42+
grep -qx 'Body: keep me' "$f"
43+
assert_clean
44+
}
45+
46+
@test "metadata: termination cleans partial output and releases the lock" {
47+
awk() { printf 'partial\n'; /bin/sh -c 'kill -TERM "$PPID"'; }
48+
run fm_set "$f" title changed
49+
[ "$status" -eq 143 ]
50+
cmp "$f" "$BATS_TEST_TMPDIR/original"
51+
assert_clean
52+
unset -f awk
53+
fm_del "$f" priority
54+
fm_get "$f" priority; [ -z "$FM" ]
55+
}
56+
57+
@test "metadata: lock timeout preserves content and never removes another writer's lock" {
58+
mkdir "$f.lock"
59+
# Exercise the bounded retry path without a ten-second wall-clock delay.
60+
sleep() { :; }
61+
run fm_set "$f" title changed
62+
[ "$status" -ne 0 ]
63+
[[ "$output" == *'timed out locking metadata'* ]]
64+
[ -d "$f.lock" ]
65+
cmp "$f" "$BATS_TEST_TMPDIR/original"
66+
# A different task remains writable while this task is locked.
67+
cp "$f" "$BATS_TEST_TMPDIR/other.md"
68+
fm_set "$BATS_TEST_TMPDIR/other.md" title independent
69+
fm_get "$BATS_TEST_TMPDIR/other.md" title; [ "$FM" = independent ]
70+
}
71+
72+
@test "metadata: relative paths preserve permissions, body and caller traps" {
73+
chmod 640 "$f"
74+
cd "$BATS_TEST_TMPDIR"
75+
local before after
76+
before=$(trap -p)
77+
fm_set TASK.md title 'a & b / c'
78+
fm_del TASK.md priority
79+
after=$(trap -p)
80+
[ "$before" = "$after" ]
81+
fm_get "$f" title; [ "$FM" = 'a & b / c' ]
82+
[ "$(LC_ALL=C ls -l "$f" | cut -c1-10)" = '-rw-r-----' ]
83+
grep -qx 'Body: keep me' "$f"
84+
assert_clean
85+
}
86+
87+
@test "metadata: a false-success mkdir cannot enter or remove another writer's lock" {
88+
command mkdir "$f.lock"
89+
# Ubuntu 26.04 uutils mkdir can return 0 after losing a creation race,
90+
# but emits its verbose creation receipt only for the actual winner.
91+
mkdir() { command mkdir "$@" || return 0; }
92+
sleep() { :; }
93+
run fm_set "$f" title changed
94+
[ "$status" -ne 0 ]
95+
[[ "$output" == *'timed out locking metadata'* ]]
96+
cmp "$f" "$BATS_TEST_TMPDIR/original"
97+
[ -d "$f.lock" ]
98+
}
99+
100+
@test "metadata: failed transform preserves original and releases lock for both operations" {
101+
awk() { printf 'partial output\n'; return 17; }
102+
run fm_set "$f" title changed
103+
[ "$status" -ne 0 ]
104+
cmp "$f" "$BATS_TEST_TMPDIR/original"
105+
assert_clean
106+
run fm_del "$f" priority
107+
[ "$status" -ne 0 ]
108+
cmp "$f" "$BATS_TEST_TMPDIR/original"
109+
assert_clean
110+
unset -f awk
111+
fm_set "$f" title recovered
112+
fm_get "$f" title; [ "$FM" = recovered ]
113+
}
114+
115+
@test "metadata: failed rename and temporary creation preserve original and release lock" {
116+
mv() { return 18; }
117+
run fm_set "$f" title changed
118+
[ "$status" -ne 0 ]
119+
cmp "$f" "$BATS_TEST_TMPDIR/original"
120+
assert_clean
121+
unset -f mv
122+
mktemp() { return 19; }
123+
run fm_del "$f" priority
124+
[ "$status" -ne 0 ]
125+
cmp "$f" "$BATS_TEST_TMPDIR/original"
126+
assert_clean
127+
}

0 commit comments

Comments
 (0)