Conversation
When two `rwx sandbox exec` commands run concurrently and both trigger auto-create, the storage lock was released before calling StartSandbox, allowing the second process to also find no session and create a duplicate. The second write would overwrite the first session key, orphaning the first sandbox (running remotely but invisible locally). Fix by passing the already-held lock from ExecSandbox/ResetSandbox into StartSandbox, so the "check if session exists → create → persist" sequence is atomic. StartSandbox releases the lock after persisting the initial session.
tonywok
approved these changes
Mar 11, 2026
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.
Slack thread
I attempted to get an integration test wrapped around this race condition, but I could not reliably get it to fail without the code change. The unit tests have been red/green verified though, which is the best we can do with this particular race.
Summary
rwx sandbox execcommands run concurrently (e.g. Claude Code running rubocop and rspec in parallel), both find no existing session and each creates a new sandbox. The secondSetSessioncall overwrites the first because they share the same storage key (cwd:branch:configFile), orphaning the first sandbox.ExecSandbox/ResetSandboxintoStartSandbox, making the "check → create → persist" sequence atomic. The second process blocks until the first has persisted, then finds the existing session and reuses it.ResetSandboxwhere the lock was released beforeStartSandbox.Test plan
TestService_ExecSandbox_ConcurrentAutoCreate— two goroutines callExecSandboxconcurrently; asserts only oneInitiateRuncall occurs and both return the same RunIDTestService_StartSandbox_StorageLock— verifiesStartSandboxaccepts a pre-held lock, persists the session, and releases the lock (including on error)expected: 1, actual: 2without the fix