Skip to content

fix(kubernetes): compare-and-swap lifecycle writes on the bootstrap claim - #6109

Open
matthewgrossman wants to merge 5 commits into
fix/5781-ha-restart-supervisor-race/matthewgrossmanfrom
fix/ha-lifecycle-claim-cas/matthewgrossman
Open

matthewgrossman wants to merge 5 commits into
fix/5781-ha-restart-supervisor-race/matthewgrossmanfrom
fix/ha-lifecycle-claim-cas/matthewgrossman

Conversation

@matthewgrossman

@matthewgrossman matthewgrossman commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Create and restart writes to the Sandbox CR now compare against the request's bootstrap claim instead of retrying blindly on resourceVersion conflicts. A create or restart whose claim was superseded mid-flight, for example by a suspension the lease holder started or another replica's restart, now fails instead of overwriting the other transition. A superseded restart's rollback leaves the Sandbox to that transition and deletes only the supervisor Pod it created.

Stacked on #6103 and should merge after it. GitHub retargets this PR to main when #6103 merges.

Related Issue

Closes #6108

Changes

  • New module src/bootstrap_claim.rs holds the claim and the writes that respect it: the phase and operation enums, the bootstrap annotation accessors, compare_and_patch_dynamic_object, and delete_supervisor_pod_and_wait (by UID). driver.rs shrinks by about 300 lines.
  • compare_and_patch_dynamic_object is the single retry loop. It passes the current Sandbox to the patch builder, which may refuse the write. Writes without a claim (stop, suspension, suspension completion) call it directly. A Sandbox without a resourceVersion is now an error instead of an unconditional write.
  • SandboxRuntimeBootstrapClaim (Sandbox UID, operation, generation) is the only write path for create and restart:
    • begin: a restart claims the Sandbox only if it is stopped and no other transition is in progress.
    • patch and release: the Running, fence-binding, and Released writes require the claim to still be held (phase Preparing or Released, per SandboxRuntimeBootstrapPhase::held_by_claimant) with the same UID, operation, and generation. Otherwise they return Precondition, and the request takes its rollback path.
    • Create builds its claim from the generation it already has instead of reading it back from the created CR.
  • Restart rollback (roll_back_restart): the restart returns the supervisor Pod it created along with its error (RestartFailure). The rollback runs suspend_if_claimed, a claimed suspension write, then delete_created_supervisor_pod.
    • Claim still held (including Released, so a Released write that timed out but landed still counts): suspend, delete this request's supervisor Pod by UID, then stop as before.
    • Claim superseded, or the Sandbox is gone: don't stop the sandbox or write to the CR. Delete only the supervisor Pod this request created, by UID, and withdraw its shared instance as before. Return the original error.
    • Previously the rollback always ran stop_sandbox_inner, which deletes the supervisor Pod by name and writes the suspension unconditionally. That tore down another replica's restart that had taken over the claim.
    • Remaining window, noted in the roll_back_restart doc comment and the README: once the rollback has suspended the Sandbox, the stop that follows is not claim-aware. If another start finishes the same stop and claims the Sandbox before this stop's final poll, this stop can still remove that start's supervisor Pod. Any two concurrent stops have the same window. This is the remaining known gap; feat: fence sandbox lifecycle actions with a gateway-issued epoch #6110 proposes fencing StopSandbox/SuspendSandbox by runtime generation to close it.
  • Create rollback is unchanged: on any failure it deletes its own Sandbox CR. A create claim can only be superseded by a stop or suspension, not by another start (start refuses an in-progress create), so no other request's runtime is torn down.
  • SandboxRuntimeBootstrapOperation enum replaces the bare "create"/"restart"/"stop" strings.
  • delete_supervisor_pod_and_wait returns an error for a nameless Pod instead of using an empty name.
  • crates/openshell-driver-kubernetes/README.md: the HA paragraph states the rule (create/restart CR writes compare-and-swap against the claim, and supervisor Pods are deleted by UID), describes how each request type rolls back, links feat: fence sandbox lifecycle actions with a gateway-issued epoch #6110 for stop and suspend along with the concurrent-stop window that remains, and no longer lists the overwrite as a limitation.

Across replicas, the gateway already serializes StartSandbox: every start, including a retry of a sandbox already Starting (crates/openshell-server/src/compute/mod.rs:2960-2975), goes through claim_provisioning_operation (provisioning_operation.rs:194-251), which refuses while a driver operation is pending. Only pre-upgrade rows without a provisioning attempt ID skip that claim (provisioning_operation.rs:195-198), because every gateway Starting write mints one (apply_lifecycle_phase → new_preparation_record). Stop and suspend have no such claim. Concurrent stops can reach the driver through stop/suspend retries (QuiesceAdmission::Retry for Stopping/Suspending, mod.rs:524-532), through startup recovery on every replica (recover_persisted_lifecycle_transitions, mod.rs:5010, which re-sends the stop without the lease), and through the provisioning-timeout reclaim (provisioning_deadline.rs:692), which sends STOP while a START may still be pending, by design.

Open questions from #6108

Stale claims. A retried start is not refused. In start_sandbox_runtime_generation, a Sandbox with a bootstrap in progress whose operation is restart (or whose phase is Suspending) goes through the in-progress branch, which calls stop_sandbox_inner before claiming. That stop writes without a claim, suspends the Sandbox, waits for the stop to complete, and applies sandbox_runtime_suspension_completion_patch, which clears the bootstrap annotations. The refreshed Sandbox is then stopped and unclaimed, so claim.begin succeeds. A request may therefore already take over an abandoned restart claim without waiting for the 300 s grace period; no change was needed. If the abandoned claim was still live on another replica, that replica's next claimed write fails and rolls back. An abandoned create claim is still refused with "initial sandbox bootstrap has not completed" until reconciliation reaps it, which is the existing behavior and unchanged here.

Helper shape. Collapsed after code review: claimed writes go through claim.patch, which always checks the claim, and unclaimed writes call compare_and_patch_dynamic_object directly. The resourceVersion-only wrapper and patch_claimed_sandbox are gone.

Testing

  • cargo fmt --check clean
  • cargo test -p openshell-driver-kubernetes --lib: 348 passed
  • cargo clippy -p openshell-driver-kubernetes --all-targets: no warnings
  • Unit tests added/updated:
    • claimed_writes_do_not_overwrite_another_transition: a claimed write conflicts, rereads a suspension, and fails without writing again; a held claim retries the conflict and succeeds.
    • restart_claims_only_an_unclaimed_stopped_sandbox: the claim is refused on another restart, our own claim, a stop, and a running Sandbox, and succeeds on a stopped one.
    • superseded_restart_rollback_spares_the_other_restart: a restart whose claim another replica's restart took over rolls back without writing to the Sandbox and without deleting the other restart's supervisor Pod. It fails against the old stop_sandbox_inner rollback.
    • reconciliation_preserves_a_supervisor_another_replica_is_restarting now asserts at least two Sandbox reads instead of exactly two, so it checks the reread without pinning the read count.
  • E2E tests added/updated (if applicable): no deterministic E2E reproduction of the interleaving exists.

Checklist

  • Follows Conventional Commits
  • Commits are signed off (DCO)
  • Architecture docs updated (if applicable)

@matthewgrossman matthewgrossman added area:drivers Compute and secret drivers, host OS/architecture support, backend isolation, and lifecycle. topic:kubernetes Kubernetes operators, CRDs, deployment controllers, and native Kubernetes API integration. topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts. labels Dec 1, 2026
@github-actions github-actions Bot added the state:accepted A maintainer decided OpenShell should pursue this issue label Dec 1, 2026
…laim

Create and restart patches to the Sandbox CR reread it on a conflict and
reapplied the patch whatever its state, so a restart on one replica could
overwrite a suspension the lease holder started.

- The patch builder now receives the current Sandbox and may refuse.
- A restart claims the Sandbox only if it is stopped and no other
  transition is in progress.
- The Running, fence-binding and Released writes of create and restart
  require their claim (operation, generation, Preparing) to still hold;
  otherwise the request fails and rolls back.
- Type bootstrap operations with an enum.
- Return an error instead of using an empty name when a Pod or Sandbox
  has no name.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
A restart whose claimed write failed rolled back with a full stop, which
deleted the supervisor Pod by name and wrote the suspension blindly. If
another replica's restart had taken over the claim, the rollback tore
that restart down.

The rollback now writes the suspension as a claimed write. If the claim
no longer holds, it leaves the Sandbox to the transition that replaced
it and deletes only the supervisor Pod this request created, by UID.
A claim in the Released phase still counts as held, so a rollback after
a Released write that timed out but landed still stops the sandbox.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>
The claim types, annotation accessors, compare-and-swap patch, and
by-UID supervisor deletion move from driver.rs to bootstrap_claim.rs.
The claim is now the only write path for create and restart: begin,
patch, and release. Writes without a claim call
compare_and_patch_dynamic_object directly, so the resourceVersion retry
wrapper and patch_claimed_sandbox are gone. A missing resourceVersion is
now an error instead of an unconditional write.

A failed restart now returns the supervisor Pod it created with its
error instead of setting a captured variable, and the rollback is split
into suspend_if_claimed and delete_created_supervisor_pod. Create builds
its claim from the generation it already has. The README states the
remaining concurrent-stop window.

Signed-off-by: Matthew Grossman <mgrossman@nvidia.com>

This branch has not been deployed

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

Labels

area:drivers Compute and secret drivers, host OS/architecture support, backend isolation, and lifecycle. state:accepted A maintainer decided OpenShell should pursue this issue topic:kubernetes Kubernetes operators, CRDs, deployment controllers, and native Kubernetes API integration. topic:reliability High availability, recovery, retries, graceful degradation, and correctness across restarts.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant