mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-04 06:10:12 +08:00
fix: deliver worker failures on Python 3.11 and 3.12
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user