@@ -36,6 +36,9 @@ use std::time::Duration;
3636use tracing:: { debug, info, warn} ;
3737use url:: Url ;
3838
39+ const STOP_COMPLETION_POLL_INTERVAL : Duration = Duration :: from_millis ( 50 ) ;
40+ const STOP_COMPLETION_TIMEOUT_HEADROOM : Duration = Duration :: from_secs ( 5 ) ;
41+
3942impl From < PodmanApiError > for ComputeDriverError {
4043 fn from ( value : PodmanApiError ) -> Self {
4144 match value {
@@ -914,22 +917,65 @@ impl PodmanComputeDriver {
914917 Ok ( entries. into_iter ( ) . next ( ) )
915918 }
916919
920+ async fn wait_for_container_stopped (
921+ & self ,
922+ sandbox_id : & str ,
923+ container_id : & str ,
924+ ) -> Result < ( ) , ComputeDriverError > {
925+ let timeout = Duration :: from_secs ( u64:: from ( self . config . stop_timeout_secs ) )
926+ + STOP_COMPLETION_TIMEOUT_HEADROOM ;
927+ let deadline = tokio:: time:: Instant :: now ( ) + timeout;
928+
929+ loop {
930+ let inspect = self
931+ . client
932+ . inspect_container ( container_id)
933+ . await
934+ . map_err ( ComputeDriverError :: from) ?;
935+ if matches ! ( inspect. state. status. as_str( ) , "exited" | "stopped" ) {
936+ return Ok ( ( ) ) ;
937+ }
938+
939+ let now = tokio:: time:: Instant :: now ( ) ;
940+ if now >= deadline {
941+ return Err ( ComputeDriverError :: Message ( format ! (
942+ "container {container_id} for sandbox {sandbox_id} did not finish stopping within {timeout:?} (last state: {})" ,
943+ inspect. state. status,
944+ ) ) ) ;
945+ }
946+ tokio:: time:: sleep ( STOP_COMPLETION_POLL_INTERVAL . min ( deadline - now) ) . await ;
947+ }
948+ }
949+
917950 /// Stop a sandbox container without deleting it.
918951 pub async fn stop_sandbox ( & self , sandbox_id : & str ) -> Result < ( ) , ComputeDriverError > {
919952 let container = self
920953 . find_container ( sandbox_id)
921954 . await ?
922955 . ok_or ( ComputeDriverError :: NotFound ) ?;
956+ let container_id = container. id ;
957+ if container. state == "stopping" {
958+ return self
959+ . wait_for_container_stopped ( sandbox_id, & container_id)
960+ . await ;
961+ }
923962 if container. state != "running" {
924963 return Ok ( ( ) ) ;
925964 }
926- let container_id = container. id ;
927965 info ! ( sandbox_id = %sandbox_id, container = %container_id, "Stopping sandbox container" ) ;
928966
929967 self . client
930968 . stop_container ( & container_id, self . config . stop_timeout_secs )
931969 . await
932- . map_err ( ComputeDriverError :: from)
970+ . map_err ( ComputeDriverError :: from) ?;
971+
972+ // Podman can return from the stop request before inspect reports the
973+ // container as exited. If start runs during that interval, the exit
974+ // event from the previous run can arrive after the gateway has moved
975+ // the same sandbox to Starting, causing it to regress to Error. Wait
976+ // for the terminal container state before allowing a restart.
977+ self . wait_for_container_stopped ( sandbox_id, & container_id)
978+ . await
933979 }
934980
935981 /// Start a previously stopped sandbox container.
@@ -1437,6 +1483,10 @@ mod tests {
14371483 vec ! [
14381484 StubResponse :: new( StatusCode :: OK , r#"[{"Id":"ctr-1","State":"running"}]"# ) ,
14391485 StubResponse :: new( StatusCode :: NO_CONTENT , "" ) ,
1486+ StubResponse :: new(
1487+ StatusCode :: OK ,
1488+ r#"{"Id":"ctr-1","Name":"sandbox","State":{"Status":"exited","Running":false,"FinishedAt":"2026-08-12T16:39:13Z"},"Config":{}}"# ,
1489+ ) ,
14401490 ] ,
14411491 ) ;
14421492 test_driver ( stop_socket. clone ( ) )
@@ -1453,6 +1503,12 @@ mod tests {
14531503 api_path( "/libpod/containers/ctr-1/stop?timeout=10" )
14541504 )
14551505 ) ;
1506+ assert_eq ! (
1507+ stop_requests
1508+ . lock( )
1509+ . expect( "request log lock should not be poisoned" ) [ 2 ] ,
1510+ format!( "GET {}" , api_path( "/libpod/containers/ctr-1/json" ) )
1511+ ) ;
14561512
14571513 let ( start_socket, start_requests, start_handle) = spawn_podman_stub (
14581514 "lifecycle-start" ,
@@ -1487,6 +1543,74 @@ mod tests {
14871543 let _ = fs:: remove_file ( start_socket) ;
14881544 }
14891545
1546+ #[ tokio:: test]
1547+ async fn stop_waits_for_the_container_to_leave_stopping_state ( ) {
1548+ let ( socket, requests, handle) = spawn_podman_stub (
1549+ "lifecycle-stop-wait" ,
1550+ vec ! [
1551+ StubResponse :: new( StatusCode :: OK , r#"[{"Id":"ctr-1","State":"running"}]"# ) ,
1552+ StubResponse :: new( StatusCode :: NO_CONTENT , "" ) ,
1553+ StubResponse :: new(
1554+ StatusCode :: OK ,
1555+ r#"{"Id":"ctr-1","Name":"sandbox","State":{"Status":"stopping","Running":true},"Config":{}}"# ,
1556+ ) ,
1557+ StubResponse :: new(
1558+ StatusCode :: OK ,
1559+ r#"{"Id":"ctr-1","Name":"sandbox","State":{"Status":"exited","Running":false,"FinishedAt":"2026-08-12T16:39:13Z"},"Config":{}}"# ,
1560+ ) ,
1561+ ] ,
1562+ ) ;
1563+
1564+ test_driver ( socket. clone ( ) )
1565+ . stop_sandbox ( "sandbox-1" )
1566+ . await
1567+ . expect ( "stop should wait for the terminal container state" ) ;
1568+ handle. await . expect ( "stop stub should finish" ) ;
1569+
1570+ let requests = requests
1571+ . lock ( )
1572+ . expect ( "request log lock should not be poisoned" ) ;
1573+ assert_eq ! ( requests. len( ) , 4 ) ;
1574+ assert_eq ! (
1575+ requests[ 2 ] ,
1576+ format!( "GET {}" , api_path( "/libpod/containers/ctr-1/json" ) )
1577+ ) ;
1578+ assert_eq ! ( requests[ 3 ] , requests[ 2 ] ) ;
1579+
1580+ let _ = fs:: remove_file ( socket) ;
1581+ }
1582+
1583+ #[ tokio:: test]
1584+ async fn stop_retry_waits_for_an_existing_stopping_container ( ) {
1585+ let ( socket, requests, handle) = spawn_podman_stub (
1586+ "lifecycle-stop-retry" ,
1587+ vec ! [
1588+ StubResponse :: new( StatusCode :: OK , r#"[{"Id":"ctr-1","State":"stopping"}]"# ) ,
1589+ StubResponse :: new(
1590+ StatusCode :: OK ,
1591+ r#"{"Id":"ctr-1","Name":"sandbox","State":{"Status":"exited","Running":false,"FinishedAt":"2026-08-12T16:39:13Z"},"Config":{}}"# ,
1592+ ) ,
1593+ ] ,
1594+ ) ;
1595+
1596+ test_driver ( socket. clone ( ) )
1597+ . stop_sandbox ( "sandbox-1" )
1598+ . await
1599+ . expect ( "stop retry should wait for the terminal container state" ) ;
1600+ handle. await . expect ( "stop retry stub should finish" ) ;
1601+
1602+ let requests = requests
1603+ . lock ( )
1604+ . expect ( "request log lock should not be poisoned" ) ;
1605+ assert_eq ! ( requests. len( ) , 2 ) ;
1606+ assert_eq ! (
1607+ requests[ 1 ] ,
1608+ format!( "GET {}" , api_path( "/libpod/containers/ctr-1/json" ) )
1609+ ) ;
1610+
1611+ let _ = fs:: remove_file ( socket) ;
1612+ }
1613+
14901614 #[ test]
14911615 fn validate_gpu_request_accepts_gpu_count_request_shape ( ) {
14921616 let gpu = GpuResourceRequirements { count : Some ( 2 ) } ;
0 commit comments