Lock state-file access to fix concurrent audit.sh races #13
No reviewers
Labels
No labels
shellcheck
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
bc1bb/claude-code-audit!13
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "fix/state-file-race"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
Fixes #5 —
save_state,branch_needs_audit, andget_new_branchesall touched the same per-repo state files (.state,.branches) with no coordination. Two overlappingaudit.shruns (cron + manual, or two--repoinvocations) could:.state→ duplicate lines that later makebranch_needs_auditalways trigger.statefile mid-updateFix
Wrap the critical sections in subshells holding
flockon a per-repo${repo_name}.lockfile:flock 9) insave_state— the only mutatorflock -s 9) inbranch_needs_auditandget_new_branches— readers can run concurrently with each other but not with a writerBoth readers also moved their file-existence check inside the lock, so they can't observe a torn
mvmid-flight from a concurrentsave_state.Notes
$STATE_DIRnext to.state/.branches, named${repo_name}.lock. They're created on first use and persist (zero bytes, harmless).Test plan
audit.sh --repo Xruns against the same repo serialize cleanly — no duplicate entries in.statesaved_shapath)Found by
Automated audit by Claude Code (issue #5)