Skip to content

Make the lock tests and one review test pass on macOS - #205

Merged
bbondy merged 2 commits into
masterfrom
fix-macos-test-failures
Oct 31, 2026
Merged

bbondy merged 2 commits into
masterfrom
fix-macos-test-failures

Conversation

@netzenbot

Copy link
Copy Markdown
Collaborator

User impact: on macOS, a job run under with-lock.sh by a process that already holds a lock, as run.sh does for the PRD sync and the in-progress sweep, is now turned away when a second copy is running; before, both ran. Four tests that failed on every macOS checkout now pass.

The problem

On a Mac, a job that is supposed to run one at a time can run twice at once. If the process that starts it already holds a lock, the job is told it has its own lock without ever contending for it. run.sh holds its run slot while it starts the PRD sync and the in-progress sweep, so those are in that position. Three tests in tests/test_scripts.py catch this on a Mac, and a fourth fails there because a wc count comes back padded with spaces; Linux CI sees neither.

Reproduce

On a Mac (/bin/bash is 3.2, no flock(1) installed, so scripts/lib/lock.sh uses its Python backend), from the root of a checkout:

  1. Save this as /tmp/demo-holder.sh. It takes a lock the way run.sh does, then starts two with-lock.sh jobs of the same name two seconds apart:
    #!/bin/bash
    W=$1
    source "$W/scripts/lib/lock.sh"
    bot_acquire_lock "$(mktemp -d)/slot.lock" || exit 1
    rm -f "$W"/.ignore/.demo-sync.lock*
    "$W/scripts/with-lock.sh" demo-sync --timeout 20 -- bash -c 'echo "job A: running"; sleep 3; echo "job A: done"' 2>&1 | sed 's/^/[A] /' &
    sleep 2
    "$W/scripts/with-lock.sh" demo-sync --timeout 20 -- bash -c 'echo "job B: running"' 2>&1 | sed 's/^/[B] /'
    wait
  2. Run /bin/bash /tmp/demo-holder.sh "$PWD".

Observed on the parent commit: job B runs while job A is still running.

[B] demo-sync starting (model=unset, slot 1/1)
[B] job B: running
[A] demo-sync starting (model=unset, slot 1/1)
[A] job A: running
[A] job A: done

Expected, and observed with this change: job B is turned away.

[B] Another demo-sync job is already running. Exiting.
[A] demo-sync starting (model=unset, slot 1/1)
[A] job A: running
[A] job A: done

python -m pytest tests/test_scripts.py -k "TestRunLocking or test_the_session_holds_no_github_credential" fails 4 tests on the parent commit (second=0 where second=1 is expected in three, GH_CONFIG_DIR= 0-entries where GH_CONFIG_DIR=0-entries is expected in one) and passes 12 here.

The fix

bot_acquire_lock opens its lock file with exec 200>>file. When fd 200 is already open, the bash 3.2 that macOS ships leaves it pointing at the old file and reports success, so a process that inherited a held lock re-locked its parent's open file description and was granted it. The function now closes fd 200 first, so the open always makes a new description that has to contend with the holder.

The review-request test's stand-in claude counted a config directory's entries with wc -l, which pads with spaces on macOS ( 0). It now strips them with tr -d " ". The assertion is unchanged.

Which redirections bash 3.2 ignores

exec N>>B run after exec N>>A, then asking which file fd N points at:

fd /bin/bash 3.2.57 Homebrew bash 5.3
5 B B
9 B B
10 A B
200 A B

The exit status is 0 in every case. exec 200>&- followed by exec 200>>B does reach B in 3.2, which is why closing first works.

Test plan

  • python -m pytest tests/test_scripts.py -k "TestRunLocking or test_the_session_holds_no_github_credential" — 4 failed on origin/master, 12 passed here
  • same command with only scripts/lib/lock.sh reverted — the 3 lock tests fail; with only the test stub reverted — the credential test fails
  • PATH=/opt/homebrew/bin:$PATH python -m pytest tests/test_scripts.py tests/test_concurrency.py -k "TestRunLocking or test_the_session_holds_no_github_credential or Lock or lock" (bash 5.3 first on PATH) — 49 passed with this change and 49 passed with lock.sh reverted
  • python -m pytest tests/ — 1505 passed, 4 skipped
  • ruff check . and ruff format --check . — passed
  • make check-reviewdog — no findings (opengrep and pip-audit ran unsandboxed because landrun is not installed here)
  • CI passes cleanly

On the bash 3.2 that macOS ships, `exec 200>>file` leaves fd 200 pointing
at its old file when the fd is already open, and still reports success.
A process that inherited a held lock, such as with-lock.sh started by
run.sh while it holds its run slot, then flocked the holder's own open
file description, was granted it, and returned 0 without contending for
the lock it asked for.

bot_acquire_lock now closes the fd before opening the lock file, so the
open always makes a new open file description. This fixes
TestRunLocking::test_acquires_and_blocks_a_second_holder,
test_one_slot_behaves_exactly_like_the_single_lock_it_replaced and
test_no_count_means_one_slot on macOS.
The stand-in claude in TestReviewRequestQueue counts the entries of the
session's gh config directory with `wc -l`. BSD wc pads the count with
spaces, so on macOS the recorded line read `GH_CONFIG_DIR=       0-entries`
and test_the_session_holds_no_github_credential failed. The stub now
strips the spaces; the assertion is unchanged.
@bbondy
bbondy merged commit 3f90ee4 into master Oct 31, 2026
3 checks passed
@bbondy
bbondy deleted the fix-macos-test-failures branch October 31, 2026 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants