Skip to content

fix(git): worktrees add ends with a FileNotFoundError when another process deletes a file while the reclaim step measures a worktree #2412

Closed

Description

@dan-petty

Problem Description

devops repos worktrees add stops with an unhandled FileNotFoundError traceback when a file in another worktree is deleted while the reclaim step measures that worktree. directory_size (src/devops_cli/git/worktree_inventory.py:324) walks the directory with os.walk and calls os.lstat on each name. A file that os.walk listed and that another process then deletes makes os.lstat raise, and the error ends the whole command. The reclaim step measures the worktrees of merged work (_size_of, called at lines 438 and 492), and these are the worktrees that another agent's worktrees add or worktrees prune --confirm removes at the same time.

Reproduction Steps

  1. Have a worktree of merged work under .data/agent/worktrees/, with a .venv.
  2. Start uv run devops repos worktrees add <issue> --type feat --slug <slug> in two sessions at about the same time, or start one while uv run devops repos worktrees prune --confirm removes that worktree.
  3. One run walks the worktree while the other deletes its files.

Seen on 2026-10-28 while several agents started worktrees at once.

Expected vs Actual Behavior

Expected: a file that is gone when it is measured counts as 0 bytes (or the directory's size is reported as unknown), and the command goes on to create the worktree.

Actual: the command ends with a traceback, and the worktree is not created. A retry with --no-reclaim works.

Environment Diagnostics

devops-cli at origin/main bb46660, Python 3.14, Linux devcontainer (rootless Podman on WSL2).

Logs, Traces, or Terminal Output

│   324 def directory_size(path: Path) -> int:
│   325 │   """The bytes of every file under `path`, links counted as links and not followed."""
│   326 │   return sum(
│ ❱ 327 │   │   os.lstat(os.path.join(top, name)).st_size
│   328 │   │   for top, _dirs, names in os.walk(path)
│   329 │   │   for name in names
│   330 │   )
FileNotFoundError: [Errno 2] No such file or directory:
'<repo>/.data/agent/worktrees/w-<n>/.venv/lib/python3.14/site-packages/authlib/integrations/base_client/async_app.py'

Proposed design

Problem statement

The directory_size function in src/devops_cli/git/worktree_inventory.py walks a directory with os.walk and calls os.lstat on each listed name. When another process deletes a file between the os.walk listing and the os.lstat call, the function raises an unhandled FileNotFoundError. This crashes the devops repos worktrees add command during its reclaim step, which measures worktrees of merged work. The crash prevents worktree creation and has no retry path other than --no-reclaim.

Acceptance criteria

  • Criterion 1: directory_size returns a byte count without raising when a file is deleted during the walk.
    Verification: Run uv run devops ci test tests/test_repos_worktrees.py with a test that creates a file, starts a thread that deletes it, and calls directory_size. The call returns an int and does not raise FileNotFoundError.
  • Criterion 2: A file that is gone when os.lstat runs contributes 0 bytes to the total.
    Verification: The test asserts the returned size equals the sum of the files that still exist, excluding the deleted file.
  • Criterion 3: devops repos worktrees add completes and creates the worktree when a concurrent process deletes files in a measured worktree.
    Verification: A test or integration scenario runs worktrees add while another process removes files in a merged worktree. The command exits 0 and the new worktree exists.
  • Criterion 4: The existing tests for directory_size and _size_of still pass.
    Verification: Run uv run devops ci test tests/test_repos_worktrees.py and confirm all tests pass.

Key questions

  • Which exception should directory_size catch around os.lstat?: Catch FileNotFoundError. It is the exception raised when a file listed by os.walk is deleted before os.lstat runs. Catching the broader OSError is not needed for this race. (proposed) [sources: src/devops_cli/git/worktree_inventory.py:324]
  • What value should a missing file contribute to the size total?: Zero bytes. The file is gone, so it adds nothing to the directory size. The command continues with the partial total. (proposed)
  • Is the change confined to directory_size in worktree_inventory.py?: Yes. The function at line 324 is the only place that calls os.lstat in the walk. _size_of at line 333 already handles the case where the directory itself is gone by returning None. No other call site needs a change. [sources: src/devops_cli/git/worktree_inventory.py:324, src/devops_cli/git/worktree_inventory.py:333]

Open questions

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/bug
    on Oct 28, 2026
  2. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones on the board.

    • Type: type/bug. An unhandled FileNotFoundError exception crashes the command; the fix is to catch it in directory_size.
    • Priority: P2-Medium. It blocks worktree creation but is intermittent and has a workaround with --no-reclaim.
    • Value: Medium. It prevents worktree creation in a specific race condition that agents hit when starting work in parallel.
    • Effort: Low. The fix is to wrap os.lstat in a try/except FileNotFoundError in directory_size and skip the missing file.
    • Related: fix(repos): worktrees add warns that a merged worktree was not reclaimed when a parallel run already removed it #2408, which asks for a different change. Both are race conditions in worktrees add during parallel runs in the same file, but #2408 is a false warning after removal while this is a crash during size measurement.
  3. dan-petty commented on Oct 28, 2026

    @dan-petty
    OwnerAuthor

    Refine cannot set this item to Ready at 73d1807 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

  4. added a commit that references this issue on Oct 28, 2026
    4226904
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    source/agenttype/bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions