@@ -743,11 +743,6 @@ impl DockerComputeDriver {
743743 . map_err ( |status| {
744744 DockerProvisioningFailure :: new ( "ImagePullFailed" , status. message ( ) )
745745 } ) ?;
746- let token_file_created = write_sandbox_token_file ( sandbox, & self . config )
747- . await
748- . map_err ( |status| {
749- DockerProvisioningFailure :: new ( "SandboxTokenWriteFailed" , status. message ( ) )
750- } ) ?;
751746
752747 let container_name = container_name_for_sandbox ( sandbox) ;
753748 let gpu_devices = self
@@ -758,9 +753,6 @@ impl DockerComputeDriver {
758753 )
759754 . await
760755 . map_err ( |status| {
761- if token_file_created {
762- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
763- }
764756 DockerProvisioningFailure :: new ( "ContainerCreateFailed" , status. message ( ) )
765757 } ) ?;
766758 let create_body = build_container_create_body_with_gpu_devices (
@@ -770,11 +762,13 @@ impl DockerComputeDriver {
770762 gpu_devices. as_deref ( ) ,
771763 )
772764 . map_err ( |status| {
773- if token_file_created {
774- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
775- }
776765 DockerProvisioningFailure :: new ( "ContainerCreateFailed" , status. message ( ) )
777766 } ) ?;
767+ write_sandbox_token_file ( sandbox, & self . config )
768+ . await
769+ . map_err ( |status| {
770+ DockerProvisioningFailure :: new ( "SandboxTokenWriteFailed" , status. message ( ) )
771+ } ) ?;
778772 self . docker
779773 . create_container (
780774 Some (
@@ -786,9 +780,6 @@ impl DockerComputeDriver {
786780 )
787781 . await
788782 . map_err ( |err| {
789- if token_file_created {
790- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
791- }
792783 DockerProvisioningFailure :: from_status (
793784 "ContainerCreateFailed" ,
794785 create_status_from_docker_error ( "create docker sandbox container" , err) ,
@@ -802,24 +793,12 @@ impl DockerComputeDriver {
802793 ) ;
803794
804795 if let Err ( err) = self . docker . start_container ( & container_name, None ) . await {
805- let cleanup = self
806- . docker
807- . remove_container (
808- & container_name,
809- Some ( RemoveContainerOptionsBuilder :: default ( ) . force ( true ) . build ( ) ) ,
810- )
811- . await ;
812- if let Err ( cleanup_err) = cleanup {
813- warn ! (
814- sandbox_id = %sandbox. id,
815- container_name,
816- error = %cleanup_err,
817- "Failed to clean up Docker container after start failure"
818- ) ;
819- }
820- if token_file_created {
821- cleanup_sandbox_token_file ( sandbox, & self . config ) ;
822- }
796+ self . cleanup_created_container_after_failure (
797+ & sandbox. id ,
798+ & container_name,
799+ "container start failure" ,
800+ )
801+ . await ;
823802 return Err ( DockerProvisioningFailure :: from_status (
824803 "ContainerStartFailed" ,
825804 create_status_from_docker_error ( "start docker sandbox container" , err) ,
@@ -845,6 +824,30 @@ impl DockerComputeDriver {
845824 Ok ( ( ) )
846825 }
847826
827+ async fn cleanup_created_container_after_failure (
828+ & self ,
829+ sandbox_id : & str ,
830+ container_name : & str ,
831+ phase : & ' static str ,
832+ ) {
833+ let cleanup = self
834+ . docker
835+ . remove_container (
836+ container_name,
837+ Some ( RemoveContainerOptionsBuilder :: default ( ) . force ( true ) . build ( ) ) ,
838+ )
839+ . await ;
840+ if let Err ( cleanup_err) = cleanup {
841+ warn ! (
842+ sandbox_id = %sandbox_id,
843+ container_name = %container_name,
844+ phase,
845+ error = %cleanup_err,
846+ "Failed to clean up Docker container after provisioning failure"
847+ ) ;
848+ }
849+ }
850+
848851 async fn delete_sandbox_inner (
849852 & self ,
850853 sandbox_id : & str ,
0 commit comments