From a5b81ca678fca846489b4f37de132a47826ea556 Mon Sep 17 00:00:00 2001 From: "Bingxi Zhao (Frank)" <13051345080@163.com> Date: Wed, 28 Oct 2026 02:21:35 +0800 Subject: [PATCH] fix: deliver worker failures on Python 3.11 and 3.12 --- deeptutor/plugins/runtime.py | 6 +++++- tests/modules/test_worker_stream.py | 18 ++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/deeptutor/plugins/runtime.py b/deeptutor/plugins/runtime.py index 2082dc713..d856f44fe 100644 --- a/deeptutor/plugins/runtime.py +++ b/deeptutor/plugins/runtime.py @@ -187,6 +187,7 @@ class _WorkerClient: ) queue: asyncio.Queue = asyncio.Queue(maxsize=64) + consumer_closed = False async def run(): try: @@ -196,7 +197,9 @@ class _WorkerClient: group.create_task(produce()) finally: # Wake the consumer even when the worker fails with an empty stream. - if not asyncio.current_task().cancelling(): + # Python 3.11/3.12 TaskGroup failures leave an internal cancellation + # count behind. Only consumer shutdown may suppress this signal. + if not consumer_closed: await queue.put(None) task = asyncio.create_task(run()) @@ -205,6 +208,7 @@ class _WorkerClient: yield item await task finally: + consumer_closed = True task.cancel() # Kill the entire session, including render helpers inheriting pipes. from deeptutor.runtime.process import kill_process_tree diff --git a/tests/modules/test_worker_stream.py b/tests/modules/test_worker_stream.py index 1f42efb7b..f4d87452b 100644 --- a/tests/modules/test_worker_stream.py +++ b/tests/modules/test_worker_stream.py @@ -59,12 +59,22 @@ print(json.dumps({"kind":"complete"}),flush=True) @pytest.mark.asyncio -@pytest.mark.parametrize("output", ["{}", '{"kind":"event","event":{}}', '{"kind":"complete"}\n{}']) +@pytest.mark.parametrize( + "output", ["", "not-json", "{}", '{"kind":"event","event":{}}', '{"kind":"complete"}\n{}'] +) async def test_malformed_or_truncated_stream_fails(tmp_path, output): - client = worker(tmp_path, f"import sys\nsys.stdin.read()\nprint({output!r},flush=True)\n") + ready = tmp_path / "pid" + client = worker( + tmp_path, + f"import os,sys\nfrom pathlib import Path\nsys.stdin.read()\n" + f"Path({str(ready)!r}).write_text(str(os.getpid()))\nprint({output!r},flush=True)\n", + ) with pytest.raises(ExceptionGroup): - async for _ in client.stream("run_capability", {}): - pass + async with asyncio.timeout(5): + async for _ in client.stream("run_capability", {}): + pass + with pytest.raises(ProcessLookupError): + os.kill(int(ready.read_text()), 0) @pytest.mark.asyncio