Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix: deliver worker failures on Python 3.11 and 3.12

Bingxi Zhao (Frank)a5b81ca678fc
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
[email protected]("output", ["{}", '{"kind":"event","event":{}}', '{"kind":"complete"}\n{}'])
+ "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