@@ -1418,11 +1418,15 @@ impl ComputeRuntime {
14181418 || driver_snapshot_confirms_stopped ( & snapshot) ;
14191419 let suspension_progressing =
14201420 expected_stopped && driver_snapshot_confirms_stopping ( & snapshot) ;
1421- if suspension_progressing {
1421+ let runtime_restart_during_stop =
1422+ expected_stopped && driver_snapshot_reports_runtime_restart ( & snapshot) ;
1423+ if suspension_progressing || runtime_restart_during_stop {
14221424 // The Kubernetes controller has accepted the stop and
1423- // is waiting for its pod to terminate. Preserve the
1424- // durable transition so a later watch event can complete
1425- // it instead of claiming the sandbox is running again.
1425+ // is waiting for its pod to terminate. A container
1426+ // runtime restart can likewise be the expected SIGTERM
1427+ // exit from an in-flight stop. Preserve the durable
1428+ // transition so completion or recovery, rather than the
1429+ // watcher, determines its terminal state.
14261430 debug ! ( sandbox_id, "Sandbox stop is still progressing" ) ;
14271431 } else if backend_phase == SandboxPhase :: Error
14281432 || observed_stopped == expected_stopped
@@ -4020,8 +4024,11 @@ fn apply_driver_snapshot(sandbox: &mut Sandbox, incoming: &DriverSandbox, sessio
40204024
40214025 // Infrastructure errors and successful main-process completions are
40224026 // sticky until an explicit lifecycle operation changes desired state. A
4023- // late backend snapshot must not revive either result.
4024- if matches ! ( old_phase, SandboxPhase :: Error | SandboxPhase :: Completed ) {
4027+ // late signal-exit snapshot must also not overwrite the terminal reason
4028+ // recorded by a completed explicit stop.
4029+ if matches ! ( old_phase, SandboxPhase :: Error | SandboxPhase :: Completed )
4030+ || ( old_phase == SandboxPhase :: Stopped && driver_snapshot_reports_runtime_restart ( incoming) )
4031+ {
40254032 if let Some ( metadata) = sandbox. metadata . as_mut ( ) {
40264033 metadata. name . clone_from ( sandbox_name) ;
40274034 }
@@ -4058,6 +4065,13 @@ fn apply_driver_snapshot(sandbox: &mut Sandbox, incoming: &DriverSandbox, sessio
40584065 ) ;
40594066
40604067 phase = match old_phase {
4068+ // SIGTERM-driven runtime exits are reported as a runtime restart by
4069+ // Docker and Podman. While an explicit stop owns this durable
4070+ // transition, preserve Stopping so the stop result chooses whether
4071+ // the sandbox actually reached Stopped or needs recovery.
4072+ SandboxPhase :: Stopping if driver_snapshot_reports_runtime_restart ( incoming) => {
4073+ SandboxPhase :: Stopping
4074+ }
40614075 SandboxPhase :: Stopping
40624076 if phase == SandboxPhase :: Stopped || driver_snapshot_confirms_stopped ( incoming) =>
40634077 {
@@ -4139,6 +4153,17 @@ fn driver_snapshot_confirms_stopped(incoming: &DriverSandbox) -> bool {
41394153 } )
41404154}
41414155
4156+ fn driver_snapshot_reports_runtime_restart ( incoming : & DriverSandbox ) -> bool {
4157+ incoming. status . as_ref ( ) . is_some_and ( |status| {
4158+ status. conditions . iter ( ) . any ( |condition| {
4159+ condition. status . eq_ignore_ascii_case ( "false" )
4160+ && condition
4161+ . reason
4162+ . eq_ignore_ascii_case ( "ContainerRuntimeRestart" )
4163+ } )
4164+ } )
4165+ }
4166+
41424167fn driver_snapshot_confirms_stopping ( incoming : & DriverSandbox ) -> bool {
41434168 incoming. status . as_ref ( ) . is_some_and ( |status| {
41444169 status. conditions . iter ( ) . any ( |condition| {
@@ -6770,6 +6795,103 @@ mod tests {
67706795 assert_eq ! ( driver. stop_calls( ) , 1 ) ;
67716796 }
67726797
6798+ #[ tokio:: test]
6799+ async fn explicit_stop_completes_after_term_runtime_restart ( ) {
6800+ let driver = ControlledDriver :: new ( ) ;
6801+ driver. block_stop ( ) ;
6802+ let runtime = test_runtime ( driver. clone ( ) ) . await ;
6803+ let sandbox = sandbox_record ( "sb-term-stop" , "sandbox-term-stop" , SandboxPhase :: Ready ) ;
6804+ runtime. store . put_message ( & sandbox) . await . unwrap ( ) ;
6805+
6806+ let stop_runtime = runtime. clone ( ) ;
6807+ let stop = tokio:: spawn ( async move {
6808+ stop_runtime
6809+ . stop_sandbox ( "default" , "sandbox-term-stop" )
6810+ . await
6811+ } ) ;
6812+ tokio:: time:: timeout ( Duration :: from_secs ( 1 ) , driver. stop_started . notified ( ) )
6813+ . await
6814+ . expect ( "stop did not reach the driver" ) ;
6815+
6816+ let mut runtime_restart = ready_driver_sandbox ( sandbox. object_id ( ) , sandbox. object_name ( ) ) ;
6817+ runtime_restart. status = Some ( make_driver_status ( make_driver_condition (
6818+ "ContainerRuntimeRestart" ,
6819+ "container exited with status 143" ,
6820+ ) ) ) ;
6821+ runtime. apply_sandbox_update ( runtime_restart) . await . unwrap ( ) ;
6822+
6823+ let stopping = runtime
6824+ . store
6825+ . get_message :: < Sandbox > ( sandbox. object_id ( ) )
6826+ . await
6827+ . unwrap ( )
6828+ . unwrap ( ) ;
6829+ assert_eq ! ( stopping. phase( ) , SandboxPhase :: Stopping as i32 ) ;
6830+ assert_eq ! (
6831+ stopping. status. unwrap( ) . conditions[ 0 ] . reason,
6832+ "ContainerRuntimeRestart"
6833+ ) ;
6834+
6835+ driver. release_stop ( ) ;
6836+ let stopped = tokio:: time:: timeout ( Duration :: from_secs ( 1 ) , stop)
6837+ . await
6838+ . expect ( "stop did not finish" )
6839+ . unwrap ( )
6840+ . unwrap ( ) ;
6841+ assert_eq ! ( stopped. phase( ) , SandboxPhase :: Stopped as i32 ) ;
6842+ assert_eq ! ( stopped. status. unwrap( ) . conditions[ 0 ] . reason, "Stopped" ) ;
6843+ }
6844+
6845+ #[ tokio:: test]
6846+ async fn failed_stop_does_not_report_term_runtime_restart_as_stopped ( ) {
6847+ let driver = ControlledDriver :: new ( ) ;
6848+ driver. block_stop ( ) ;
6849+ driver. set_stop_outcome ( ControlledLifecycleOutcome :: Error ( "stop timed out" ) ) ;
6850+ let runtime = test_runtime ( driver. clone ( ) ) . await ;
6851+ let sandbox = sandbox_record ( "sb-term-fail" , "sandbox-term-fail" , SandboxPhase :: Ready ) ;
6852+ runtime. store . put_message ( & sandbox) . await . unwrap ( ) ;
6853+
6854+ let stop_runtime = runtime. clone ( ) ;
6855+ let stop = tokio:: spawn ( async move {
6856+ stop_runtime
6857+ . stop_sandbox ( "default" , "sandbox-term-fail" )
6858+ . await
6859+ } ) ;
6860+ tokio:: time:: timeout ( Duration :: from_secs ( 1 ) , driver. stop_started . notified ( ) )
6861+ . await
6862+ . expect ( "stop did not reach the driver" ) ;
6863+
6864+ let mut runtime_restart = ready_driver_sandbox ( sandbox. object_id ( ) , sandbox. object_name ( ) ) ;
6865+ runtime_restart. status = Some ( make_driver_status ( make_driver_condition (
6866+ "ContainerRuntimeRestart" ,
6867+ "container exited with status 143" ,
6868+ ) ) ) ;
6869+ driver. set_get_outcome ( ControlledGetOutcome :: Sandbox ( Box :: new (
6870+ runtime_restart. clone ( ) ,
6871+ ) ) ) ;
6872+ runtime. apply_sandbox_update ( runtime_restart) . await . unwrap ( ) ;
6873+
6874+ driver. release_stop ( ) ;
6875+ let err = tokio:: time:: timeout ( Duration :: from_secs ( 1 ) , stop)
6876+ . await
6877+ . expect ( "stop did not finish" )
6878+ . unwrap ( )
6879+ . unwrap_err ( ) ;
6880+ assert ! ( err. message( ) . contains( "stop timed out" ) ) ;
6881+
6882+ let stored = runtime
6883+ . store
6884+ . get_message :: < Sandbox > ( sandbox. object_id ( ) )
6885+ . await
6886+ . unwrap ( )
6887+ . unwrap ( ) ;
6888+ assert_eq ! ( stored. phase( ) , SandboxPhase :: Stopping as i32 ) ;
6889+ assert_eq ! (
6890+ stored. status. unwrap( ) . conditions[ 0 ] . reason,
6891+ "ContainerRuntimeRestart"
6892+ ) ;
6893+ }
6894+
67736895 #[ tokio:: test]
67746896 async fn request_cancellation_does_not_cancel_start_worker ( ) {
67756897 let driver = ControlledDriver :: new ( ) ;
@@ -8360,6 +8482,73 @@ mod tests {
83608482 assert_eq ! ( status. exit_code, None ) ;
83618483 }
83628484
8485+ #[ tokio:: test]
8486+ async fn unexpected_term_runtime_restart_transitions_to_error ( ) {
8487+ let runtime = test_runtime ( Arc :: new ( TestDriver :: default ( ) ) ) . await ;
8488+ let sandbox = sandbox_record ( "sb-term-exit" , "sandbox-term-exit" , SandboxPhase :: Ready ) ;
8489+ runtime. store . put_message ( & sandbox) . await . unwrap ( ) ;
8490+ let mut runtime_restart = ready_driver_sandbox ( sandbox. object_id ( ) , sandbox. object_name ( ) ) ;
8491+ runtime_restart. status = Some ( make_driver_status ( make_driver_condition (
8492+ "ContainerRuntimeRestart" ,
8493+ "container exited with status 143" ,
8494+ ) ) ) ;
8495+
8496+ runtime. apply_sandbox_update ( runtime_restart) . await . unwrap ( ) ;
8497+
8498+ let stored = runtime
8499+ . store
8500+ . get_message :: < Sandbox > ( sandbox. object_id ( ) )
8501+ . await
8502+ . unwrap ( )
8503+ . unwrap ( ) ;
8504+ assert_eq ! ( stored. phase( ) , SandboxPhase :: Error as i32 ) ;
8505+ assert_eq ! (
8506+ stored. status. unwrap( ) . conditions[ 0 ] . reason,
8507+ "ContainerRuntimeRestart"
8508+ ) ;
8509+ }
8510+
8511+ #[ tokio:: test]
8512+ async fn late_term_runtime_restart_preserves_intentional_stop_status ( ) {
8513+ let runtime = test_runtime ( Arc :: new ( TestDriver :: default ( ) ) ) . await ;
8514+ let mut sandbox = sandbox_record (
8515+ "sb-term-stopped" ,
8516+ "sandbox-term-stopped" ,
8517+ SandboxPhase :: Stopped ,
8518+ ) ;
8519+ sandbox. status = Some ( SandboxStatus {
8520+ sandbox_name : sandbox. object_name ( ) . to_string ( ) ,
8521+ phase : SandboxPhase :: Stopped as i32 ,
8522+ conditions : vec ! [ SandboxCondition {
8523+ r#type: "Ready" . to_string( ) ,
8524+ status: "False" . to_string( ) ,
8525+ reason: "Stopped" . to_string( ) ,
8526+ message: "Sandbox compute is stopped" . to_string( ) ,
8527+ last_transition_time: String :: new( ) ,
8528+ } ] ,
8529+ ..Default :: default ( )
8530+ } ) ;
8531+ runtime. store . put_message ( & sandbox) . await . unwrap ( ) ;
8532+ let mut runtime_restart = ready_driver_sandbox ( sandbox. object_id ( ) , sandbox. object_name ( ) ) ;
8533+ runtime_restart. status = Some ( make_driver_status ( make_driver_condition (
8534+ "ContainerRuntimeRestart" ,
8535+ "container exited with status 143" ,
8536+ ) ) ) ;
8537+
8538+ runtime. apply_sandbox_update ( runtime_restart) . await . unwrap ( ) ;
8539+
8540+ let stored = runtime
8541+ . store
8542+ . get_message :: < Sandbox > ( sandbox. object_id ( ) )
8543+ . await
8544+ . unwrap ( )
8545+ . unwrap ( ) ;
8546+ assert_eq ! ( stored. phase( ) , SandboxPhase :: Stopped as i32 ) ;
8547+ let ready = & stored. status . unwrap ( ) . conditions [ 0 ] ;
8548+ assert_eq ! ( ready. reason, "Stopped" ) ;
8549+ assert_eq ! ( ready. message, "Sandbox compute is stopped" ) ;
8550+ }
8551+
83638552 #[ tokio:: test]
83648553 async fn late_driver_exit_preserves_completed_main_result ( ) {
83658554 let runtime = test_runtime ( Arc :: new ( TestDriver :: default ( ) ) ) . await ;
0 commit comments