fix: deliver worker failures on Python 3.11 and 3.12

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-28 02:21:35 +08:00
parent 766e626d1f
commit a5b81ca678
2 changed files with 19 additions and 5 deletions

View File

@@ -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

View File

@@ -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