Repository navigation
fix: serialize worktree lifecycle operations with a flock on .cwm/lock - #17
Merged
Merged
Conversation
create_worktree, remove_worktree, prune_stale and register_agent_symlink mutated .git/worktrees and the per-branch metadata under .cwm/worktrees/ without any locking. Concurrent agents driving 'git worktree add'/'prune' through the PR #15 git-hook could corrupt both. Add cwm_lock(cwm_dir), a blocking fcntl.flock(LOCK_EX) context manager on .cwm/lock, and wrap the critical region of each lifecycle method. Both the CLI commands and the agent git-hook converge on the same WSM methods, so locking at the method boundary serializes every entry path. Build and test operations are intentionally left lockless so parallel builds across worktrees stay concurrent.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Worktree lifecycle operations were unsynchronized:
create_worktree,remove_worktree,prune_staleandregister_agent_symlinkmutated.git/worktreesand the per-branch metadata under.cwm/worktrees/with no locking. Concurrent agents drivinggit worktree add/prunethrough the PR #15 git-hook could corrupt both.This adds
cwm_lock(cwm_dir)— a blocking POSIXfcntl.flock(LOCK_EX)context manager on.cwm/lock— and wraps the critical region of each lifecycle method. The CLI commands and the agent git-hook both converge on the same WSM methods, so locking at the method boundary serializes every entry path. Build and test operations stay lockless by design, so parallel builds across worktrees remain concurrent.Changes
src/cwm/util/lock.py:cwm_lockcontext manager (blockingLOCK_EX,EINTRretry, lock file never deleted, body exceptions propagate unchanged).src/cwm/core/wsm.py: wrap the critical region ofcreate_worktree(incl. the existence/collision check, closing a TOCTOU window),remove_worktree,prune_stale, andregister_agent_symlink.tests/unit/test_lock.py: 5 unit tests (real fcntl; a second open file description proves mutual exclusion deterministically).tests/unit/test_wsm.py:TestLifecycleLocking— 4 tests asserting the lock is held across each lifecycle method's git operation.README.md: "Concurrency / locking" section.Testing
uv run python -m pytest tests/→ 179 passed (full suite, same environment as CI). The 9 new tests (test_lock.py×5,TestLifecycleLocking×4) all pass; no regressions.Release: patch → v6.1.2