Skip to content

Commit db7d7a6

Browse files
fix(server): keep the reconcile sweep from failing a sandbox mid-start (#5265)
* fix(podman): keep a polled stale exit from failing an in-progress start The gateway's reconcile sweep applies polled snapshots without waiting for an in-progress start. During a stop/start restart, get_sandbox and list_sandboxes built the stopped workload from its list entry, so the previous run's exit was reported as ContainerExited and moved the sandbox from Starting to Error. While a start is in progress, inspect a stopped workload before reporting it. If it is running again, or still shows the exit recorded before the start, report it as ContainerStarting. A new exit, and any exit once the start has returned, is still reported. The start is tracked by a guard that clears the in-progress mark on every return path. Closes #5258 Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * docs(podman): describe how restarts fence the previous exit Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * fix(podman): keep a restart from containing its own workload A restart starts the workload before its exited supervisor. Watch reconciliation, start events, and polled snapshots could inspect the workload in that gap, see a running workload beside an exited supervisor, and stop it. Record the supervisor exit that the start observed. While that start is in progress, an exited supervisor with the recorded FinishedAt reports the workload as starting instead of containing it. A missing supervisor, a new supervisor exit, and any exit after the start returns are still contained. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * fix(server): skip the reconcile poll of a sandbox whose start is in progress StartSandbox holds the sandbox lifecycle gate for the whole driver call, and the workload can still show its previous exit until the driver starts it. The watch path already waits on that gate for Starting sandboxes; the reconcile sweep did not, so a poll could move a restarting sandbox from Starting to Error. Skip such a sandbox and let the next tick or event report the settled state. This replaces the Podman-specific fence of polled snapshots. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * fix(server): preserve cleanup during start reconciliation Signed-off-by: Shiju <shiju@nvidia.com> * fix(server): defer the reconcile sweep for a start claimed by another replica The sweep skipped a Starting sandbox only when this replica's lifecycle gate was held, so a replica that did not own the start could still apply the previous run's exit. Also skip sandboxes whose driver operation is durably pending. Deletion keeps its own pending-operation handling. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * test(server): check a sweep still applies an exit after the start settles Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> * refactor(server): narrow the reconcile start guard to the snapshot path Check for an in-progress start only after the driver poll: the resource-version fence already rejects a poll that overlaps a start beginning earlier. Limit the durable-claim check to Starting sandboxes, drop the prune-path guard (pending operations already block pruning), restore the original watch-delay test, and cover the gap where only the local gate defers the sweep. Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> --------- Signed-off-by: Matthew Grossman <mgrossman@nvidia.com> Signed-off-by: Shiju <shiju@nvidia.com> Co-authored-by: Shiju <shiju@nvidia.com>
1 parent 7c8e018 commit db7d7a6

1 file changed

Lines changed: 187 additions & 0 deletions

File tree

  • crates/openshell-server/src/compute

‎crates/openshell-server/src/compute/mod.rs‎

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6660,6 +6660,26 @@ impl ComputeRuntime {
66606660
self.sandbox_watch_bus.remove(sandbox_id);
66616661
}
66626662

6663+
/// Whether a sweep observes a `Starting` sandbox with an active start.
6664+
///
6665+
/// `StartSandbox` holds the gate for the whole driver call, during which
6666+
/// the workload can still show its previous exit. The sweep skips the
6667+
/// sandbox instead of waiting, and the next tick or watch event
6668+
/// reports the settled state. The gate is probed, never retained, so
6669+
/// Deleted watches can still dispatch cleanup.
6670+
///
6671+
/// The gate is local to this replica. A start owned by another replica is
6672+
/// visible through its durable driver-operation claim.
6673+
fn sweep_start_in_progress(&self, record: &ObjectRecord) -> Result<bool, String> {
6674+
let sandbox = decode_sandbox_record(record)?;
6675+
Ok(sandbox.phase() == SandboxPhase::Starting as i32
6676+
&& (provisioning_deadline::driver_operation_pending(&sandbox)
6677+
|| self
6678+
.lifecycle_gates
6679+
.try_lock_for(sandbox.object_id())
6680+
.is_none()))
6681+
}
6682+
66636683
async fn reconcile_snapshot_sandbox(
66646684
&self,
66656685
snapshot: DriverSandbox,
@@ -6700,6 +6720,7 @@ impl ComputeRuntime {
67006720
};
67016721
if existing.resource_version != expected_resource_version
67026722
|| existing.updated_at_ms > sweep_started_at_ms
6723+
|| self.sweep_start_in_progress(&existing)?
67036724
{
67046725
return Ok(());
67056726
}
@@ -14383,6 +14404,129 @@ mod tests {
1438314404
);
1438414405
}
1438514406

14407+
// Regression for #5258. The reconcile sweep polls the driver without
14408+
// waiting for a start, and a restarting workload still shows its previous
14409+
// exit until the driver starts it. The sweep skips a sandbox whose start
14410+
// holds the lifecycle gate; the settled state is reported afterwards.
14411+
// Another replica cannot see that gate, so it relies on the start's
14412+
// durable driver-operation claim.
14413+
#[tokio::test]
14414+
async fn reconcile_sweep_skips_a_sandbox_while_its_start_is_in_progress() {
14415+
for other_replica in [false, true] {
14416+
let driver = ControlledDriver::new();
14417+
driver.block_start();
14418+
let sandbox = sandbox_record(
14419+
"sb-start-reconcile",
14420+
"sandbox-start-reconcile",
14421+
SandboxPhase::Stopped,
14422+
);
14423+
let mut polled = ready_driver_sandbox(sandbox.object_id(), sandbox.object_name());
14424+
polled.status = Some(make_driver_status(make_driver_condition(
14425+
"ContainerExited",
14426+
"Container exited with code 0",
14427+
)));
14428+
driver.set_get_outcome(ControlledGetOutcome::Sandbox(Box::new(polled.clone())));
14429+
let runtime = test_runtime(driver.clone()).await;
14430+
runtime.store.put_message(&sandbox).await.unwrap();
14431+
14432+
let start_runtime = runtime.clone();
14433+
let sandbox_name = sandbox.object_name().to_string();
14434+
let start =
14435+
tokio::spawn(
14436+
async move { start_runtime.start_sandbox("default", &sandbox_name).await },
14437+
);
14438+
tokio::time::timeout(Duration::from_secs(1), driver.start_started.notified())
14439+
.await
14440+
.expect("start did not reach the driver");
14441+
14442+
let mut observer = runtime.clone();
14443+
if other_replica {
14444+
// Replicas share the store but not lifecycle gates.
14445+
observer.lifecycle_gates = Arc::new(LifecycleGateRegistry::default());
14446+
}
14447+
tokio::time::timeout(
14448+
Duration::from_secs(1),
14449+
observer.reconcile_snapshot_sandbox(polled.clone(), i64::MAX),
14450+
)
14451+
.await
14452+
.expect("reconciliation must not wait for the start")
14453+
.unwrap();
14454+
let phase_during_start = runtime
14455+
.store
14456+
.get_message::<Sandbox>(sandbox.object_id())
14457+
.await
14458+
.unwrap()
14459+
.unwrap()
14460+
.phase();
14461+
assert_eq!(
14462+
phase_during_start,
14463+
SandboxPhase::Starting as i32,
14464+
"other_replica={other_replica}"
14465+
);
14466+
14467+
driver.release_start();
14468+
start.await.unwrap().unwrap();
14469+
14470+
// Once the start settles, a genuine exit of the restarted
14471+
// workload still surfaces through the sweep.
14472+
observer
14473+
.reconcile_snapshot_sandbox(polled, i64::MAX)
14474+
.await
14475+
.unwrap();
14476+
let settled = runtime
14477+
.store
14478+
.get_message::<Sandbox>(sandbox.object_id())
14479+
.await
14480+
.unwrap()
14481+
.unwrap();
14482+
assert!(
14483+
!provisioning_deadline::driver_operation_pending(&settled),
14484+
"other_replica={other_replica}"
14485+
);
14486+
assert_eq!(
14487+
settled.phase(),
14488+
SandboxPhase::Error as i32,
14489+
"other_replica={other_replica}"
14490+
);
14491+
}
14492+
}
14493+
14494+
// A start writes `Starting` before it records its driver-operation claim.
14495+
// In between, only the local lifecycle gate defers this replica's sweep.
14496+
#[tokio::test]
14497+
async fn reconcile_sweep_skips_a_starting_sandbox_whose_gate_is_held_before_its_claim() {
14498+
let driver = ControlledDriver::new();
14499+
let sandbox = sandbox_record(
14500+
"sb-start-gate",
14501+
"sandbox-start-gate",
14502+
SandboxPhase::Starting,
14503+
);
14504+
let mut polled = ready_driver_sandbox(sandbox.object_id(), sandbox.object_name());
14505+
polled.status = Some(make_driver_status(make_driver_condition(
14506+
"ContainerExited",
14507+
"Container exited with code 0",
14508+
)));
14509+
driver.set_get_outcome(ControlledGetOutcome::Sandbox(Box::new(polled.clone())));
14510+
let runtime = test_runtime(driver).await;
14511+
runtime.store.put_message(&sandbox).await.unwrap();
14512+
14513+
let gate = runtime.lifecycle_gates.lock_for(sandbox.object_id()).await;
14514+
runtime
14515+
.reconcile_snapshot_sandbox(polled, i64::MAX)
14516+
.await
14517+
.unwrap();
14518+
drop(gate);
14519+
14520+
let stored = runtime
14521+
.store
14522+
.get_message::<Sandbox>(sandbox.object_id())
14523+
.await
14524+
.unwrap()
14525+
.unwrap();
14526+
assert!(!provisioning_deadline::driver_operation_pending(&stored));
14527+
assert_eq!(stored.phase(), SandboxPhase::Starting as i32);
14528+
}
14529+
1438614530
#[tokio::test]
1438714531
async fn live_container_exit_during_start_still_transitions_to_error() {
1438814532
for reason in [
@@ -16452,6 +16596,49 @@ mod tests {
1645216596
stop_watch_loop(shutdown_tx, watch_handle).await;
1645316597
}
1645416598

16599+
// The sweep's start probe must not hold the lifecycle gate across the
16600+
// driver poll, or a Deleted watch for a `Starting` sandbox could not
16601+
// dispatch driver cleanup.
16602+
#[tokio::test]
16603+
async fn deleted_watch_during_sweep_poll_of_starting_sandbox_dispatches_cleanup() {
16604+
let driver = ControlledDriver::new();
16605+
driver.block_get();
16606+
driver.set_get_outcome(ControlledGetOutcome::Missing);
16607+
let runtime = test_runtime(driver.clone()).await;
16608+
let sandbox = sandbox_record("sb-a", "sandbox-a", SandboxPhase::Starting);
16609+
runtime.store.put_message(&sandbox).await.unwrap();
16610+
16611+
let (shutdown_tx, watch_handle) = start_watch_loop(&runtime, &driver).await;
16612+
let mut deleted_rx = runtime.sandbox_watch_bus.subscribe("sb-a");
16613+
let reconcile_runtime = runtime.clone();
16614+
let snapshot = ready_driver_sandbox("sb-a", "sandbox-a");
16615+
let reconcile = tokio::spawn(async move {
16616+
reconcile_runtime
16617+
.reconcile_snapshot_sandbox(snapshot, i64::MAX)
16618+
.await
16619+
});
16620+
tokio::time::timeout(Duration::from_secs(1), driver.get_started.notified())
16621+
.await
16622+
.expect("reconciliation did not reach the driver lookup");
16623+
16624+
driver.send_event(deleted_watch_event("sb-a"));
16625+
tokio::time::timeout(Duration::from_secs(1), deleted_rx.recv())
16626+
.await
16627+
.expect("watch event was blocked by reconciliation lookup")
16628+
.expect("sandbox watch bus closed before notification");
16629+
tokio::time::timeout(Duration::from_secs(1), driver.delete_started.notified())
16630+
.await
16631+
.expect("the sweep must not suppress deletion-watch driver cleanup");
16632+
driver.release_get();
16633+
tokio::time::timeout(Duration::from_secs(1), reconcile)
16634+
.await
16635+
.unwrap()
16636+
.unwrap()
16637+
.unwrap();
16638+
assert_eq!(driver.delete_calls(), 1);
16639+
stop_watch_loop(shutdown_tx, watch_handle).await;
16640+
}
16641+
1645516642
#[tokio::test]
1645616643
async fn non_deleting_container_exit_still_transitions_to_error() {
1645716644
let runtime = test_runtime(Arc::new(TestDriver::default())).await;

0 commit comments

Comments
 (0)