@@ -200,6 +200,7 @@ def close(self) -> None:
200200
201201 class _Process :
202202 pid = 123
203+ returncode = None
203204 _transport = _Transport ()
204205
205206 async def communicate (self ) -> tuple [bytes , bytes ]:
@@ -246,6 +247,7 @@ def close(self) -> None:
246247
247248 class _Process :
248249 pid = 123
250+ returncode = None
249251 _transport = _Transport ()
250252
251253 async def communicate (self ) -> tuple [bytes , bytes ]:
@@ -277,6 +279,54 @@ def _deny_group_signal(*_args: object) -> None:
277279 assert transport_closed
278280
279281
282+ @pytest .mark .asyncio
283+ async def test_unix_local_exec_cleanup_does_not_kill_exited_direct_child (
284+ monkeypatch : pytest .MonkeyPatch ,
285+ ) -> None :
286+ communicate_started = asyncio .Event ()
287+ wait_called = asyncio .Event ()
288+ direct_kill_called = False
289+ transport_closed = False
290+
291+ class _Transport :
292+ def close (self ) -> None :
293+ nonlocal transport_closed
294+ transport_closed = True
295+
296+ class _Process :
297+ pid = 123
298+ returncode = 0
299+ _transport = _Transport ()
300+
301+ async def communicate (self ) -> tuple [bytes , bytes ]:
302+ communicate_started .set ()
303+ await asyncio .Event ().wait ()
304+ return b"" , b""
305+
306+ def kill (self ) -> None :
307+ nonlocal direct_kill_called
308+ direct_kill_called = True
309+
310+ async def wait (self ) -> int :
311+ wait_called .set ()
312+ return self .returncode
313+
314+ def _deny_group_signal (* _args : object ) -> None :
315+ raise PermissionError ("not allowed to signal a group member" )
316+
317+ monkeypatch .setattr (unix_local_module .os , "killpg" , _deny_group_signal )
318+ monkeypatch .setattr (unix_local_module , "_PROCESS_CLEANUP_TIMEOUT_SECONDS" , 0.01 )
319+
320+ process = cast (asyncio .subprocess .Process , _Process ())
321+ task = asyncio .create_task (unix_local_module ._terminate_process_group_and_reap (process ))
322+ await communicate_started .wait ()
323+
324+ await asyncio .wait_for (task , timeout = 1 )
325+ assert not direct_kill_called
326+ assert wait_called .is_set ()
327+ assert transport_closed
328+
329+
280330@pytest .mark .asyncio
281331async def test_unix_local_exec_cleanup_bounds_unreapable_direct_child (
282332 monkeypatch : pytest .MonkeyPatch ,
@@ -292,6 +342,7 @@ def close(self) -> None:
292342
293343 class _Process :
294344 pid = 123
345+ returncode = None
295346 _transport = _Transport ()
296347
297348 async def communicate (self ) -> tuple [bytes , bytes ]:
0 commit comments