11"""
22POSIX-specific functionality for stdio client operations.
33"""
4+ # pragma: exclude file
45
56import logging
67import os
@@ -23,7 +24,7 @@ async def terminate_posix_process_tree(process: Process, timeout_seconds: float
2324 timeout_seconds: Timeout in seconds before force killing (default: 2.0)
2425 """
2526 pid = getattr (process , "pid" , None ) or getattr (getattr (process , "popen" , None ), "pid" , None )
26- if not pid : # pragma: no cover
27+ if not pid :
2728 # No PID means there's no process to terminate - it either never started,
2829 # already exited, or we have an invalid process object
2930 return
@@ -41,22 +42,20 @@ async def terminate_posix_process_tree(process: Process, timeout_seconds: float
4142 except ProcessLookupError :
4243 return
4344
44- try : # pragma: no cover
45+ try :
4546 os .killpg (pgid , signal .SIGKILL )
46- except ProcessLookupError : # pragma: no cover
47+ except ProcessLookupError :
4748 pass
4849
49- except (ProcessLookupError , PermissionError , OSError ) as e : # pragma: no cover
50- logger .warning (
51- f"Process group termination failed for PID { pid } : { e } , falling back to simple terminate"
52- ) # pragma: no cover
53- try : # pragma: no cover
54- process .terminate () # pragma: no cover
55- with anyio .fail_after (timeout_seconds ): # pragma: no cover
56- await process .wait () # pragma: no cover
57- except Exception : # pragma: no cover
58- logger .warning (f"Process termination failed for PID { pid } , attempting force kill" ) # pragma: no cover
59- try : # pragma: no cover
60- process .kill () # pragma: no cover
61- except Exception : # pragma: no cover
62- logger .exception (f"Failed to kill process { pid } " ) # pragma: no cover
50+ except (ProcessLookupError , PermissionError , OSError ) as e :
51+ logger .warning (f"Process group termination failed for PID { pid } : { e } , falling back to simple terminate" )
52+ try :
53+ process .terminate ()
54+ with anyio .fail_after (timeout_seconds ):
55+ await process .wait ()
56+ except Exception :
57+ logger .warning (f"Process termination failed for PID { pid } , attempting force kill" )
58+ try :
59+ process .kill ()
60+ except Exception :
61+ logger .exception (f"Failed to kill process { pid } " )
0 commit comments