Skip to content

Concurrent spawns attaching to the same checkout path fail with "Cannot checkout branch" though no branch is requested #3609

Closed

Description

@IlyaM

Summary

Two bb thread spawn --environment <path> calls that attach to the same existing unmanaged checkout at the same moment intermittently fail. One thread ends in error with Cannot checkout branch while another thread is using this workspace, even though neither spawn requests a branch. About one thread in four failed when pairs were spawned simultaneously. Expected: both threads attach to the existing environment, as they do when spawned a few seconds apart or when attached by environment id.

Versions and environment

  • bb 0.43.0 (npm bb-app), Linux 7.0 (devcontainer), Node v24.15.0
  • Source read at main cf51227e1135309a3c9c0baf5630be1ca7ba2714; the cited files are unchanged from that commit
  • Reproduced with claude-code (Claude Code 2.1.270, claude-haiku-4-5-20251001); first observed with the pi provider
  • Project checkout provider, unmanaged path: a detached git worktree inside the project's local source checkout, already registered as a ready environment
  • Spawned by an unattended script that starts two review threads in parallel against one checkout

Steps to reproduce

Run from the project's source checkout, with PROJECT set to that project's id.

  1. Create a scratch checkout and seed its environment:

    WT="$PWD/.scratch/bb-race/wt"
    git worktree add --detach "$WT" HEAD
    bb thread spawn --project "$PROJECT" --environment "$WT" \
      --provider claude-code --model claude-haiku-4-5-20251001 --reasoning-level low \
      --permission-mode accept-edits --visibility hidden --title seed \
      --prompt "Reply with the single word OK. Do not use tools." --json

    Wait for the seed thread to go idle. Its environment is now ready at $WT.

  2. Spawn pairs of threads against the path at the same moment:

    spawn() {
      bb thread spawn --project "$PROJECT" --environment "$1" \
        --provider claude-code --model claude-haiku-4-5-20251001 --reasoning-level low \
        --permission-mode accept-edits --visibility hidden --title "race $2" \
        --prompt "Reply with the single word OK. Do not use tools." --json
    }
    for i in 1 2 3 4 5; do spawn "$WT" "$i-a" & spawn "$WT" "$i-b" & wait; sleep 8; done
  3. After about 15 seconds, check the new threads' status with bb thread list --include-hidden --json. Some are error with no environmentId.

The same thing happens without --title, so it doesn't depend on skipping title generation.

It did not reproduce when step 2 passes the seed environment's id (--environment env_…) instead of the path. That gave 0 failures in 30 threads.

Expected vs actual

Expected: every thread attaches to the existing environment at $WT, as the successful spawns do:

Preparing workspace
Preparing Project checkout…
Using workspace: <path>
Provisioned thread

Actual, from bb thread log <failed thread> --json (system/* events):

system/thread-provisioning  Preparing workspace (started)
system/thread-provisioning  Preparing Project checkout… (started), environmentId set to a fresh project-checkout row
system/thread-provisioning  Workspace setup failed (failed), 11 ms later
system/error                {"code":"thread_provisioning_failed","message":"Provisioning thread failed","detail":"Cannot checkout branch while another thread is using this workspace"}

Evidence

Measured by spawning pairs simultaneously, 8 s apart, then reading every thread's status:

Attach by Title Threads error
path --title given 10 2
path generated 16 4
environment id --title given 30 0

All 6 failures carry the detail above.

Timeline from the first real occurrence (ms offsets from the first spawn):

  • 0: thread A spawned. At 1632 it starts "Preparing Project checkout…". At 2036 it is bound to the existing environment. At 2521 provisioning completes.
  • 184: thread B spawned. At 1877 it starts "Preparing Project checkout…". At 1888 provisioning fails.

B's provider step starts and fails while A's provider step holds the path.

Observed in source at cf51227e:

Inferred cause, not verified by instrumentation: every path spawn provisions a temporary project-checkout environment row, and all of those rows compete for one exclusive path claim. A spawn that loses the claim fails immediately instead of waiting. It then reports the branch-checkout message even though no checkout was requested.

The thread and environment ids are from a local install, so they aren't listed here. No public-safe investigation link is available.

Suggested fix

A suggestion only; no code was changed.

  • When a branch-less attach's claim is refused because another provisioning is binding the same path, wait for that claim to be released and retry, instead of failing.
  • Alternatively, when the path already has a ready environment and no branch is requested, bind to it without taking the exclusive claim.
  • Either way, give a refused claim its own message, such as "Workspace is being prepared by another thread". The branch-checkout text sends users looking for a checkout that never happened.
  • A regression test could provision two branch-less project-checkout attaches to one path concurrently and expect both to reach the existing environment.

What was ruled out

  • Not Archived idle threads block branch checkout after the workspace is released #2068. That issue is the same message caused by archived threads blocking a branch checkout. Here no branch is requested and no archived thread is involved.
  • Not provider-specific: seen with pi and claude-code.
  • Not title generation: fails both with and without --title.
  • Not a dirty or detached checkout: the failure happens 11 ms into provisioning, before any host inspection. The same checkout attaches fine when spawns are seconds apart or by environment id.
  • Not reproduced on 0.43.1 specifically; the cited code is unchanged on main at cf51227e.

Suggested priority and effort

Medium. It hits any script or orchestrator that starts parallel threads in one shared checkout. The thread is lost to error with a misleading message, but no work or data is lost. Workaround: attach by environment id. Effort is probably Low to Medium.

AGENT GENERATED

Activity

  1. added theissue type on Sep 13, 2026
  2. added
    workspaces
    confirmed-repro
    on Sep 13, 2026
  3. bb-slop-cop commented on Sep 13, 2026

    @bb-slop-cop
    Contributor

    🚨 SLOP COP 🚨 · new-issue-autopilot

    Classification: Bug · Priority: Medium · Effort: Medium · workspaces.

    Report: REPRODUCED, high root-cause confidence. Label: confirmed-repro.

    Branchless attachment immediately fails when another preparation holds the exclusive path claim through thread binding. A real-SQLite server regression reproduced this in two clean trusted-main checkouts; live CLI failure rates were not measured.

    Fix: #3610 — bounded, cancellable claim retries preserve serialization. The regression now passes; 99 relevant tests, both typechecks, and plugin bundle preparation pass. 171 changed lines across 3 files. PR is ready and unmerged.

    AGENT GENERATED

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

    confirmed-reproworkspaces

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions