Repository navigation
Description
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
maincf51227e1135309a3c9c0baf5630be1ca7ba2714; 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 thepiprovider - Project checkout provider, unmanaged path: a detached
git worktreeinside the project's local source checkout, already registered as areadyenvironment - 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.
-
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
readyat$WT. -
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
-
After about 15 seconds, check the new threads' status with
bb thread list --include-hidden --json. Some areerrorwith noenvironmentId.
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:
- The project-checkout provider's
createcallsexperimental_claimPath(path)before anything else. If the claim is refused, it returnsLIVE_THREAD_MESSAGE, whether or not a branch was requested: plugins/environment-project-checkout/server.ts#L186-L192, message at #L20-L21. claimEnvironmentPathrefuses when any other non-destroyed environment row holdsclaimPathfor the same host and path: packages/db/src/data/environments.ts#L549-L557.- The claim is held until
markProviderEnvironmentAttachedclears it: apps/server/src/services/environments/environment-engine.ts#L514-L532. - Attaching by id goes through
assertEnvironmentPathAvailableand anownerThreadIdcheck, but takes no claim: apps/server/src/services/threads/thread-create.ts#L356, apps/server/src/services/threads/thread-request-eligibility.ts#L168.
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
readyenvironment 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
piandclaude-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
mainatcf51227e.
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
bb-slop-cop commentedon Sep 13, 2026
🚨 SLOP COP 🚨 ·
new-issue-autopilotClassification: 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.
Retry contended path claims during branchless attachment (#3610)