Skip to content

Commit bd1d09b

Browse files
jpnurmiclaude
andcommitted
Close TOCTOU race in build-sentry-cocoa.sh stale lock reap
Switch from a noclobber PID file to a directory lock with a pid file inside. The atomic mkdir gates acquisition; stale reap renames the dir to a unique name before rm -rf so two concurrent reapers can't both proceed to own the lock. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 2d05ef4 commit bd1d09b

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

scripts/build-sentry-cocoa.sh

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,23 @@ pushd "$(dirname "$0")" >/dev/null
55
cd ../modules/sentry-cocoa
66

77
mkdir -p Carthage
8-
PID_FILE="$PWD/Carthage/.build.pid"
9-
trap 'if [[ "$(cat "$PID_FILE" 2>/dev/null)" == "$$" ]]; then rm -f "$PID_FILE"; fi' EXIT
8+
LOCK_DIR="$PWD/Carthage/.build.lock"
9+
trap 'if [[ "$(cat "$LOCK_DIR/pid" 2>/dev/null)" == "$$" ]]; then rm -rf "$LOCK_DIR"; fi' EXIT
1010

1111
# Serialize concurrent invocations; parallel xcodebuilds race on DerivedData.
12-
while ! (set -C; echo $$ > "$PID_FILE") 2>/dev/null; do
13-
build_pid=$(cat "$PID_FILE" 2>/dev/null || true)
12+
while ! mkdir "$LOCK_DIR" 2>/dev/null; do
13+
build_pid=$(cat "$LOCK_DIR/pid" 2>/dev/null || true)
1414
if [[ -n "$build_pid" ]] && ! kill -0 "$build_pid" 2>/dev/null; then
1515
echo "Previous build did not complete (pid $build_pid); cleaning up and retrying" >&2
16-
rm -f "$PID_FILE"
16+
# Atomically claim the stale dir via rename
17+
if mv "$LOCK_DIR" "$LOCK_DIR.stale.$$" 2>/dev/null; then
18+
rm -rf "$LOCK_DIR.stale.$$"
19+
fi
1720
continue
1821
fi
1922
sleep 2
2023
done
24+
echo $$ > "$LOCK_DIR/pid"
2125

2226
current_sha=$(git rev-parse HEAD)
2327
if [[ -f Carthage/.built-from-sha ]] && [[ "$(cat Carthage/.built-from-sha)" == "$current_sha" ]]; then

0 commit comments

Comments
 (0)