@@ -25,13 +25,16 @@ use openshell_core::proto::compute::v1::{
2525 GpuResourceRequirements , MemoryResourceCapabilities , ResourceCapabilities ,
2626} ;
2727use std:: collections:: HashMap ;
28+ use std:: future:: Future ;
2829use std:: path:: { Path , PathBuf } ;
2930use std:: sync:: Arc ;
3031use std:: time:: Duration ;
3132use tracing:: { Instrument as _, debug, info, warn} ;
3233
3334const STOP_COMPLETION_POLL_INTERVAL : Duration = Duration :: from_millis ( 50 ) ;
3435const STOP_COMPLETION_TIMEOUT_HEADROOM : Duration = Duration :: from_secs ( 5 ) ;
36+ const MAX_PING_RETRIES : u32 = 5 ;
37+ const PING_RETRY_DELAY : Duration = Duration :: from_secs ( 2 ) ;
3538const POLICY_DNS_RESOLV_CONF : & [ u8 ] = b"nameserver 127.0.0.53\n " ;
3639
3740#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
@@ -413,12 +416,33 @@ fn resolve_socket_path(
413416 } )
414417}
415418
419+ async fn ping_with_retry < F , Fut > ( mut ping : F ) -> Result < ( ) , PodmanApiError >
420+ where
421+ F : FnMut ( ) -> Fut ,
422+ Fut : Future < Output = Result < ( ) , PodmanApiError > > ,
423+ {
424+ let mut retries = 0 ;
425+ loop {
426+ match ping ( ) . await {
427+ Ok ( ( ) ) => return Ok ( ( ) ) ,
428+ Err ( error) if retries < MAX_PING_RETRIES => {
429+ retries += 1 ;
430+ warn ! (
431+ attempt = retries,
432+ max_retries = MAX_PING_RETRIES ,
433+ error = %error,
434+ "Podman socket not ready, retrying"
435+ ) ;
436+ tokio:: time:: sleep ( PING_RETRY_DELAY ) . await ;
437+ }
438+ Err ( error) => return Err ( error) ,
439+ }
440+ }
441+ }
442+
416443impl PodmanComputeDriver {
417444 /// Create a new driver, verifying the Podman socket is reachable.
418445 pub async fn new ( mut config : PodmanComputeConfig ) -> Result < Self , PodmanApiError > {
419- const MAX_PING_RETRIES : u32 = 5 ;
420- const PING_RETRY_DELAY : Duration = Duration :: from_secs ( 2 ) ;
421-
422446 let socket_path = resolve_socket_path ( config. socket_path . clone ( ) , detect_socket) ?;
423447 config. socket_path = Some ( socket_path. clone ( ) ) ;
424448
@@ -449,23 +473,7 @@ impl PodmanComputeDriver {
449473 // unavailability (e.g. podman.socket restarting after a package
450474 // upgrade). The systemd unit uses Wants=podman.socket (not Requires),
451475 // so the gateway may start while the socket is briefly re-activating.
452- let mut attempts = 0 ;
453- loop {
454- match client. ping ( ) . await {
455- Ok ( ( ) ) => break ,
456- Err ( e) if attempts < MAX_PING_RETRIES => {
457- attempts += 1 ;
458- warn ! (
459- attempt = attempts,
460- max_retries = MAX_PING_RETRIES ,
461- error = %e,
462- "Podman socket not ready, retrying"
463- ) ;
464- tokio:: time:: sleep ( PING_RETRY_DELAY ) . await ;
465- }
466- Err ( e) => return Err ( e) ,
467- }
468- }
476+ ping_with_retry ( || client. ping ( ) ) . await ?;
469477
470478 // Verify cgroups v2, detect rootless mode, and log system info.
471479 let rootless = match client. system_info ( ) . await {
@@ -2048,7 +2056,7 @@ mod tests {
20482056 use openshell_core:: proto:: compute:: v1:: {
20492057 DriverSandboxSpec , DriverSandboxTemplate , ResourceRequirements ,
20502058 } ;
2051- use std:: collections:: HashMap ;
2059+ use std:: collections:: { HashMap , VecDeque } ;
20522060 use std:: fs;
20532061 use std:: path:: { Path , PathBuf } ;
20542062
@@ -2109,6 +2117,63 @@ mod tests {
21092117 assert ! ( err. to_string( ) . contains( "no responsive Podman API socket" ) ) ;
21102118 }
21112119
2120+ #[ tokio:: test( start_paused = true ) ]
2121+ async fn ping_retries_transient_failures ( ) {
2122+ let mut outcomes = VecDeque :: from ( [
2123+ Err ( PodmanApiError :: Connection ( "first" . to_string ( ) ) ) ,
2124+ Err ( PodmanApiError :: Connection ( "second" . to_string ( ) ) ) ,
2125+ Ok ( ( ) ) ,
2126+ ] ) ;
2127+ let started = tokio:: time:: Instant :: now ( ) ;
2128+
2129+ ping_with_retry ( || std:: future:: ready ( outcomes. pop_front ( ) . expect ( "ping outcome" ) ) )
2130+ . await
2131+ . expect ( "a later successful ping should stop retries" ) ;
2132+
2133+ assert ! ( outcomes. is_empty( ) ) ;
2134+ assert_eq ! ( started. elapsed( ) , PING_RETRY_DELAY * 2 ) ;
2135+ }
2136+
2137+ #[ tokio:: test( start_paused = true ) ]
2138+ async fn ping_failure_is_bounded_by_retry_policy ( ) {
2139+ let mut attempts = 0 ;
2140+ let started = tokio:: time:: Instant :: now ( ) ;
2141+
2142+ let error = ping_with_retry ( || {
2143+ attempts += 1 ;
2144+ std:: future:: ready ( Err ( PodmanApiError :: Connection ( format ! (
2145+ "attempt {attempts}"
2146+ ) ) ) )
2147+ } )
2148+ . await
2149+ . expect_err ( "persistent connection failures should be returned" ) ;
2150+
2151+ assert_eq ! ( attempts, MAX_PING_RETRIES + 1 ) ;
2152+ assert_eq ! ( started. elapsed( ) , PING_RETRY_DELAY * MAX_PING_RETRIES ) ;
2153+ assert_eq ! (
2154+ error. to_string( ) ,
2155+ format!( "connection error: attempt {}" , MAX_PING_RETRIES + 1 )
2156+ ) ;
2157+ }
2158+
2159+ #[ tokio:: test]
2160+ async fn missing_socket_connection_error_names_configured_path ( ) {
2161+ let tempdir = tempfile:: tempdir ( ) . expect ( "create isolated socket directory" ) ;
2162+ let missing_socket = tempdir. path ( ) . join ( "missing-podman.sock" ) ;
2163+
2164+ let error = PodmanClient :: new ( missing_socket. clone ( ) )
2165+ . ping ( )
2166+ . await
2167+ . expect_err ( "a missing socket should fail to connect" ) ;
2168+
2169+ assert ! ( matches!( error, PodmanApiError :: Connection ( _) ) ) ;
2170+ assert ! (
2171+ error
2172+ . to_string( )
2173+ . contains( & missing_socket. display( ) . to_string( ) )
2174+ ) ;
2175+ }
2176+
21122177 fn cdi_devices_config ( device_ids : & [ & str ] ) -> prost_types:: Struct {
21132178 prost_types:: Struct {
21142179 fields : std:: iter:: once ( (
0 commit comments