Repository navigation
Description
User Story
I operate OpenShell gateways for agent deployments. During sandbox creation, the compute driver's watch event for the new sandbox races with the create-side writes on the same store record. I need the watch-driven update to survive that race so the gateway's view of the sandbox stays in sync with the driver.
Problem Statement
A watch-driven sandbox update performs a single compare-and-swap in update_sandbox_record (crates/openshell-server/src/compute/mod.rs). The version it pins is captured when the apply starts. Any concurrent write to the same sandbox record between that read and the CAS, such as the supervisor session state written while the sandbox comes up, makes the CAS fail with a Conflict. The watch loop then logs a warning and drops the event:
WARN openshell_server::compute: Failed to apply compute driver event
error=concurrent modification detected during sandbox reconciliation (current resource_version: 11)
This exact warning shows up in #5954's gateway logs at sandbox creation time. The comment on the update path claims the next watch event will retry naturally. That is not true in general: the Kubernetes driver re-emits full snapshots every 30 seconds, but the Docker driver's watch stream is event-driven (initial snapshot plus lifecycle events, no periodic re-emit), so on those drivers the dropped snapshot is not reapplied until the sandbox changes state again.
Watch-driven removals already retry this race (d08935e, #5666). Additions and updates do not.
Impact / Why This Matters
A dropped update loses the driver's phase, condition, and endpoint observations for that snapshot. On drivers without periodic re-emit, the gateway's record stays stale until the sandbox's next lifecycle event or the reconciliation sweep, and the only trace is a warning in the logs. For controllers that watch gateway state to converge, the stale window is unpredictable.
Acceptance Criteria
- A watch-driven sandbox update that loses one CAS race is retried and converges instead of being dropped
- Each retry re-reads the record and re-runs the snapshot revalidation, so a stale snapshot cannot overwrite a newer gateway-owned transition (for example a concurrent stop)
- The retry is bounded and yields between attempts, so a persistently conflicting sandbox cannot delay watch events for other sandboxes
- Persistence failures other than a CAS conflict still surface immediately, without retry
- After the retry bound, the update defers to reconciliation with a warning naming the sandbox
Follow-up (out of scope here): a gateway metric counting watch-apply conflicts and retries.
Reproduction Steps
- Run a gateway with a compute driver whose watch stream does not re-emit unchanged sandboxes.
- Create a sandbox while another write lands on the sandbox record between the watch apply's version read and its CAS. The supervisor session state is a common one during creation. Sandbox persisted by CreateSandbox stays invisible to workspace-scoped GetSandbox/GetService/GetProvider after a watch-event persist conflict #5954's logs show exactly this on a single-replica SQLite gateway with the Kubernetes driver.
- Observe the "Failed to apply compute driver event" warning with "concurrent modification detected during sandbox reconciliation", and the driver snapshot not applied to the store.
Environment
- OpenShell: v0.1.10 (log evidence from Sandbox persisted by CreateSandbox stays invisible to workspace-scoped GetSandbox/GetService/GetProvider after a watch-event persist conflict #5954); code path unchanged on main
- Compute driver: Kubernetes (evidence), Docker/Podman affected the longest (no snapshot re-emit)
- Deployment: single gateway replica, SQLite store
Logs
Taken from #5954, at sandbox creation:
WARN openshell_server::compute: Failed to apply compute driver event
error=concurrent modification detected during sandbox reconciliation
(current resource_version: 11)
Activity