Skip to content

fix: prevent spurious sandbox restarts under concurrent agent usage - #31

Merged
malaporte merged 1 commit into
mainfrom
sandbox-restarts
Apr 10, 2026
Merged

malaporte merged 1 commit into
mainfrom
sandbox-restarts

Conversation

@malaporte

Copy link
Copy Markdown
Owner

Summary

When multiple agents use the same named sandbox concurrently, the sandbox could restart unexpectedly. Two root causes were identified and fixed.

Changes

  • GPG socket fingerprint: Removed stat.ino and stat.mtimeMs from the fingerprint computed in resolveGpgSocketInfo. The socket path + dir is sufficient to identify the config; inode/mtime churn whenever the GPG agent restarts was causing hash mismatches that triggered unnecessary sandbox restarts.
  • Lock scope in ensureSandbox: The validate → compare → stop → start cycle is now fully enclosed by the file lock. Previously only startSandbox was locked, leaving a window where two concurrent agents could both observe a hash mismatch and both call stopSandbox, killing a sandbox that was actively in use. The new flow uses double-checked locking: a fast unlocked path handles the common case (hash matches, return immediately), and the slow locked path re-validates after acquiring the lock so agents that waited pick up a freshly started sandbox without triggering another restart.

Testing

  • bun run typecheck — clean
  • bun run test — 139/139 tests pass
  • bun run build — all platform binaries compile successfully

Reviewer Notes

The double-checked locking pattern relies on validateState + computeConfigHash being safe to call concurrently (they are — both are read-only with respect to the state file). The writeState call inside startSandbox uses an atomic temp-file rename, so there is no torn-read risk on the state file itself.

- Remove inode/mtime from GPG socket fingerprint in resolveGpgSocketInfo;
  socket path alone is sufficient and inode/mtime churn when the GPG agent
  restarts was causing hash mismatches that triggered unnecessary restarts
- Move the validate+compare+stop+start cycle inside the file lock in
  ensureSandbox so only one agent at a time can make the restart decision;
  add double-checked locking so agents that waited for the lock re-validate
  before restarting, picking up a freshly started sandbox when possible
@malaporte
malaporte marked this pull request as ready for review April 10, 2026 14:23
@malaporte
malaporte merged commit 6ea87f4 into main Apr 10, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.20.3 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant