@@ -825,18 +825,7 @@ async def start_async(
825825 _logger .info ("Backend launcher PID: %d (logs: %s)" , process .pid , self ._log_path )
826826 await self ._wait_for_readiness_async (plan = plan , host = host , startup_state = startup_state )
827827 finally :
828- if startup_state .phase is not _StartupPhase .READY and startup_state .process is not None :
829- if self ._process is None :
830- self ._process = startup_state .process
831- self ._listener_pid = startup_state .process .pid
832- self ._port = port if startup_state .pid_record_written else None
833- try :
834- await startup_state .cleanup_async ()
835- finally :
836- if startup_state .cleanup_succeeded :
837- self ._clear_process_state ()
838- elif startup_state .cleanup_succeeded is False :
839- _logger .warning ("Failed to stop backend launcher process %d" , startup_state .process .pid )
828+ await self ._cleanup_failed_startup_async (startup_state = startup_state , port = port )
840829
841830 if startup_state .timed_out :
842831 cleanup_message = (
@@ -856,6 +845,22 @@ async def start_async(
856845 )
857846 return plan .base_url
858847
848+ async def _cleanup_failed_startup_async (self , * , startup_state : _ServerStartupState , port : int ) -> None :
849+ """Clean up even when spawning raised before the launcher stored its process."""
850+ if startup_state .phase is _StartupPhase .READY or startup_state .process is None :
851+ return
852+ if self ._process is None :
853+ self ._process = startup_state .process
854+ self ._listener_pid = startup_state .process .pid
855+ self ._port = port if startup_state .pid_record_written else None
856+ try :
857+ await startup_state .cleanup_async ()
858+ finally :
859+ if startup_state .cleanup_succeeded :
860+ self ._clear_process_state ()
861+ elif startup_state .cleanup_succeeded is False :
862+ _logger .warning ("Failed to stop backend launcher process %d" , startup_state .process .pid )
863+
859864 def _clear_process_state (self ) -> None :
860865 """Clear process state after the owned backend exits."""
861866 self ._process = None
0 commit comments