Skip to content

Commit 20d2e86

Browse files
authored
fix(sandbox): reject stale exit during restart (#2857)
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 82f62fa commit 20d2e86

1 file changed

Lines changed: 87 additions & 11 deletions

File tree

  • crates/openshell-server/src/compute

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

Lines changed: 87 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,8 @@ impl ComputeRuntime {
13941394
let name = sandbox.object_name().to_string();
13951395
if matches!(phase, SandboxPhase::Stopping | SandboxPhase::Starting) {
13961396
let status = sandbox.status.get_or_insert_with(Default::default);
1397-
status.main_process_instance_id.clear();
1397+
// Retain the previous instance id as a tombstone until
1398+
// the restarted supervisor registers its new id.
13981399
status.exit_code = None;
13991400
}
14001401
upsert_ready_condition(
@@ -2910,16 +2911,29 @@ impl ComputeRuntime {
29102911
return Ok(());
29112912
}
29122913
if let Some(status) = existing.status.as_ref() {
2913-
if !status.main_process_instance_id.is_empty()
2914-
&& status.main_process_instance_id != instance_id
2915-
{
2916-
tracing::warn!(
2917-
sandbox_id,
2918-
instance_id,
2919-
active_instance_id = %status.main_process_instance_id,
2920-
"ignoring stale main-process exit report"
2921-
);
2922-
return Ok(());
2914+
if !status.main_process_instance_id.is_empty() {
2915+
// While Starting, the stored id belongs to the stopped
2916+
// instance. A different id is the new process and its early
2917+
// exit must still be recorded.
2918+
if phase == SandboxPhase::Starting && status.main_process_instance_id == instance_id
2919+
{
2920+
tracing::warn!(
2921+
sandbox_id,
2922+
instance_id,
2923+
"ignoring main-process exit report from stopped instance while sandbox is starting"
2924+
);
2925+
return Ok(());
2926+
}
2927+
if phase != SandboxPhase::Starting && status.main_process_instance_id != instance_id
2928+
{
2929+
tracing::warn!(
2930+
sandbox_id,
2931+
instance_id,
2932+
active_instance_id = %status.main_process_instance_id,
2933+
"ignoring stale main-process exit report"
2934+
);
2935+
return Ok(());
2936+
}
29232937
}
29242938
if let Some(current_exit_code) = status.exit_code {
29252939
if current_exit_code != exit_code {
@@ -5123,6 +5137,68 @@ mod tests {
51235137
}
51245138
}
51255139

5140+
#[tokio::test]
5141+
async fn starting_uses_previous_main_process_instance_as_exit_tombstone() {
5142+
let runtime = test_runtime(Arc::new(TestDriver::default())).await;
5143+
let mut sandbox = sandbox_record("sb-1", "sandbox-a", SandboxPhase::Stopped);
5144+
sandbox.status = Some(SandboxStatus {
5145+
phase: SandboxPhase::Stopped as i32,
5146+
main_process_instance_id: "instance-old".into(),
5147+
exit_code: Some(143),
5148+
..Default::default()
5149+
});
5150+
runtime.store.put_message(&sandbox).await.unwrap();
5151+
let stored = runtime
5152+
.store
5153+
.get_message::<Sandbox>("sb-1")
5154+
.await
5155+
.unwrap()
5156+
.unwrap();
5157+
5158+
let starting = runtime
5159+
.write_lifecycle_phase(
5160+
&stored,
5161+
SandboxPhase::Starting,
5162+
"Starting",
5163+
"Sandbox start requested",
5164+
)
5165+
.await
5166+
.unwrap();
5167+
let status = starting.status.as_ref().unwrap();
5168+
assert_eq!(status.main_process_instance_id, "instance-old");
5169+
assert_eq!(status.exit_code, None);
5170+
5171+
runtime
5172+
.main_process_exited("sb-1", "instance-old", 143)
5173+
.await
5174+
.unwrap();
5175+
let stored = runtime
5176+
.store
5177+
.get_message::<Sandbox>("sb-1")
5178+
.await
5179+
.unwrap()
5180+
.unwrap();
5181+
assert_eq!(stored.phase(), SandboxPhase::Starting as i32);
5182+
let status = stored.status.as_ref().unwrap();
5183+
assert_eq!(status.main_process_instance_id, "instance-old");
5184+
assert_eq!(status.exit_code, None);
5185+
5186+
runtime
5187+
.main_process_exited("sb-1", "instance-new", 1)
5188+
.await
5189+
.unwrap();
5190+
let stored = runtime
5191+
.store
5192+
.get_message::<Sandbox>("sb-1")
5193+
.await
5194+
.unwrap()
5195+
.unwrap();
5196+
assert_eq!(stored.phase(), SandboxPhase::Error as i32);
5197+
let status = stored.status.unwrap();
5198+
assert_eq!(status.main_process_instance_id, "instance-new");
5199+
assert_eq!(status.exit_code, Some(1));
5200+
}
5201+
51265202
fn ssh_session_record(id: &str, sandbox_id: &str) -> SshSession {
51275203
SshSession {
51285204
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {

0 commit comments

Comments
 (0)