@@ -207,6 +207,9 @@ async def communicate(self) -> tuple[bytes, bytes]:
207207 await asyncio .Event ().wait ()
208208 return b"" , b""
209209
210+ def kill (self ) -> None :
211+ pass
212+
210213 async def wait (self ) -> int :
211214 wait_called .set ()
212215 return - signal .SIGKILL
@@ -227,6 +230,98 @@ async def wait(self) -> int:
227230 assert transport_closed
228231
229232
233+ @pytest .mark .asyncio
234+ async def test_unix_local_exec_cleanup_kills_direct_child_when_group_signal_fails (
235+ monkeypatch : pytest .MonkeyPatch ,
236+ ) -> None :
237+ communicate_started = asyncio .Event ()
238+ wait_called = asyncio .Event ()
239+ direct_kill_called = False
240+ transport_closed = False
241+
242+ class _Transport :
243+ def close (self ) -> None :
244+ nonlocal transport_closed
245+ transport_closed = True
246+
247+ class _Process :
248+ pid = 123
249+ _transport = _Transport ()
250+
251+ async def communicate (self ) -> tuple [bytes , bytes ]:
252+ communicate_started .set ()
253+ await asyncio .Event ().wait ()
254+ return b"" , b""
255+
256+ def kill (self ) -> None :
257+ nonlocal direct_kill_called
258+ direct_kill_called = True
259+
260+ async def wait (self ) -> int :
261+ wait_called .set ()
262+ return - signal .SIGKILL
263+
264+ def _deny_group_signal (* _args : object ) -> None :
265+ raise PermissionError ("not allowed to signal a group member" )
266+
267+ monkeypatch .setattr (unix_local_module .os , "killpg" , _deny_group_signal )
268+ monkeypatch .setattr (unix_local_module , "_PROCESS_CLEANUP_TIMEOUT_SECONDS" , 0.01 )
269+
270+ process = cast (asyncio .subprocess .Process , _Process ())
271+ task = asyncio .create_task (unix_local_module ._terminate_process_group_and_reap (process ))
272+ await communicate_started .wait ()
273+
274+ await asyncio .wait_for (task , timeout = 1 )
275+ assert direct_kill_called
276+ assert wait_called .is_set ()
277+ assert transport_closed
278+
279+
280+ @pytest .mark .asyncio
281+ async def test_unix_local_exec_cleanup_bounds_unreapable_direct_child (
282+ monkeypatch : pytest .MonkeyPatch ,
283+ ) -> None :
284+ communicate_started = asyncio .Event ()
285+ wait_started = asyncio .Event ()
286+ transport_closed = False
287+
288+ class _Transport :
289+ def close (self ) -> None :
290+ nonlocal transport_closed
291+ transport_closed = True
292+
293+ class _Process :
294+ pid = 123
295+ _transport = _Transport ()
296+
297+ async def communicate (self ) -> tuple [bytes , bytes ]:
298+ communicate_started .set ()
299+ await asyncio .Event ().wait ()
300+ return b"" , b""
301+
302+ def kill (self ) -> None :
303+ raise PermissionError ("not allowed to signal the direct child" )
304+
305+ async def wait (self ) -> int :
306+ wait_started .set ()
307+ await asyncio .Event ().wait ()
308+ return - signal .SIGKILL
309+
310+ def _deny_group_signal (* _args : object ) -> None :
311+ raise PermissionError ("not allowed to signal a group member" )
312+
313+ monkeypatch .setattr (unix_local_module .os , "killpg" , _deny_group_signal )
314+ monkeypatch .setattr (unix_local_module , "_PROCESS_CLEANUP_TIMEOUT_SECONDS" , 0.01 )
315+
316+ process = cast (asyncio .subprocess .Process , _Process ())
317+ task = asyncio .create_task (unix_local_module ._terminate_process_group_and_reap (process ))
318+ await communicate_started .wait ()
319+
320+ await asyncio .wait_for (task , timeout = 1 )
321+ assert wait_started .is_set ()
322+ assert transport_closed
323+
324+
230325@pytest .mark .asyncio
231326async def test_unix_local_inherits_host_environment_by_default (
232327 tmp_path : Path ,
0 commit comments