State file race condition when audit.sh runs concurrently #5
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#5
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
Problem
The
save_statefunction uses a non-atomic read-modify-write sequence to update per-repo state files. The finalecho ... >> "$state_file"append is not protected against concurrent access, and thegrep > tmp && mv tmppattern only covers one of the two write steps.Location
audit.sh, lines 179–186 (save_statefunction):Also
branch_needs_audit(lines 189–213) reads state without a lock.Risk
If two instances of
audit.shrun simultaneously (e.g., triggered by a cron job that overlaps with a manual run, or via--repofilters for different repos), they can race on the same.statefile. Both instances may read an identical saved SHA, both decide auditing is needed, both run redundant audits, and both write overlapping or duplicate entries to the state file. Over time, duplicate entries can accumulate, causingbranch_needs_auditto always trigger (always seeing current SHA != saved SHA from the stale duplicate line).Suggested fix direction
Use a lockfile per-repo (e.g.,
flock "$state_file.lock" -c "...") around the read-modify-write cycle in bothsave_stateandbranch_needs_audit. Alternatively, restructure to use a single atomic write per branch entry using a temp file andmv.Severity
minor
Found by
Automated audit by Claude Code