Skip to content

bug: HA lifecycle writes can overwrite a suspension started by another gateway replica #6108

Backlog

Description

@matthewgrossman

User Story

I maintain the OpenShell Kubernetes driver and run the HA gateway (k3s-ha) conformance lanes. While fixing an HA restart race in #6103, I found that a lifecycle request on one gateway replica can silently overwrite a suspension that the lease holder started, so a sandbox the holder decided to suspend keeps running.

Problem Statement

In an HA gateway, only the lease holder reconciles sandbox runtimes, but any replica serves lifecycle RPCs. Lifecycle writes to the Sandbox CR go through patch_dynamic_object_with_resource_version_retry, which rereads the CR on a resourceVersion conflict and passes the patch builder only the new resourceVersion. Every write therefore retries until it lands, whatever transition the CR is in by then. A restart never checks that its Preparing claim still stands.

Interleaving:

  1. A restart on replica A claims a stopped sandbox (Preparing, operation restart).
  2. The lease holder suspends the sandbox while that bootstrap is fresh, for example because the resource admission re-check fails. The CR becomes Suspended, phase Suspending, operation stop.
  3. Replica A's Running patch conflicts, rereads, and sets the sandbox back to Running.
  4. Replica A's Released phase patch leaves phase Released with operation stop. That state never completes, because completion requires operation create or restart. The workload keeps running until the 300 s bootstrap grace period expires and reconciliation reaps it.

On main before #6103, the restart's Running patch rewrote the whole claim, so it fully undid the suspension instead of leaving a half-applied state. In both cases a suspension decided by the holder, such as an admission or fence failure, can be overridden by another replica.

Impact / Why This Matters

A security-relevant suspension (admission or network-fence drift) can be reversed by an unrelated restart on another replica, leaving the workload running for up to the grace period. There is no operator workaround besides running a single gateway replica.

Acceptance Criteria

  • A create or restart write to the Sandbox CR fails instead of overwriting a transition another replica started, and the request takes its existing rollback path.
  • A restart claims the Sandbox only when it is stopped and no other transition is in progress.
  • A unit test covers the interleaving above: a claimed write that conflicts, rereads a suspension, and fails without writing again.
  • The HA paragraph in crates/openshell-driver-kubernetes/README.md no longer lists this as a limitation.

Reproduction Steps

There is no deterministic end-to-end reproduction; the suspension has to land between a restart's claim and its Running or Released patch.

  1. Run an HA gateway with the Kubernetes driver (for example the k3s-ha conformance environment) with at least two replicas.
  2. Restart a stopped sandbox through a replica that does not hold the reconcile lease.
  3. While that restart is Preparing, make the lease holder suspend the sandbox, for example by invalidating its admitted resources.
  4. Observe the Sandbox CR end up Running with phase Released and operation stop until the grace period reaps it.

Proposed Direction

Branch fix/ha-lifecycle-claim-cas/matthewgrossman (commit 54af099) has a working draft:

  • compare_and_patch_dynamic_object passes the current Sandbox to the patch builder, which may refuse the write.
  • A SandboxRuntimeBootstrapClaim (Sandbox UID, operation, generation) is claimed by restart only on a stopped, unclaimed Sandbox. Create's claim is its initial annotations.
  • The Running, fence-binding and Released writes of create and restart require the claim to still hold.
  • A SandboxRuntimeBootstrapOperation enum replaces bare "create"/"restart"/"stop" strings.

Open questions to resolve there:

  • Stale claims: with the claim check, can a retried start with a new generation be refused after a replica crashed mid-restart, until the 300 s grace period reaps the abandoned claim? Today it proceeds. On a first reading, the start path stops an in-progress restart before claiming, which would clear the abandoned claim, but this needs confirming. Decide whether a request may take over a stale claim, or whether waiting is acceptable.
  • Helper shape: the draft layers three write helpers (compare_and_patch_dynamic_object, patch_dynamic_object_with_resource_version_retry, patch_claimed_sandbox). Consider a flatter shape.

Environment

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions