Merge commit 'ce333434c31a84dac85d5d9cf1737bdd66e7a556' into HEAD

This commit is contained in:
Bingxi Zhao (Frank)
2026-10-27 22:23:08 +08:00
2 changed files with 47 additions and 1 deletions

View File

@@ -97,12 +97,14 @@ class EngineClient:
if process.returncode is None:
with suppress(ProcessLookupError):
process.terminate()
# A caller may start another turn as soon as it receives failure.
# Reap first so _ensure_started cannot reuse the exiting worker.
await process.wait()
for queue in tuple(self._runs.values()):
with suppress(asyncio.QueueFull):
queue.put_nowait(
EngineUnavailableError("TypeScript scheduler stopped during turn")
)
await process.wait()
async def _send(self, frame: dict[str, Any]) -> None:
payload = (json.dumps({"v": 1, **frame}, ensure_ascii=False) + "\n").encode()

View File

@@ -0,0 +1,44 @@
"""Worker loss must be observable only after that worker can no longer be reused."""
import asyncio
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from deeptutor.runtime.ts_engine.client import EngineClient, EngineUnavailableError
@pytest.mark.asyncio
@pytest.mark.parametrize("last_frame", [b"", b"invalid protocol\n"])
async def test_worker_failure_waits_for_process_exit_before_notifying_turns(last_frame):
reaping = asyncio.Event()
exited = asyncio.Event()
process = SimpleNamespace(
stdout=SimpleNamespace(readline=AsyncMock(return_value=last_frame)),
returncode=None,
terminate=lambda: None,
)
async def wait():
reaping.set()
await exited.wait()
process.returncode = -15
return process.returncode
process.wait = wait
client = EngineClient()
client._process = process
queue = asyncio.Queue()
client._runs["turn"] = queue
reader = asyncio.create_task(client._read(process))
try:
await asyncio.wait_for(reaping.wait(), 1)
# A notified caller may immediately start another turn. While returncode
# is None, _ensure_started would still consider this worker reusable.
assert queue.empty()
finally:
exited.set()
await reader
assert process.returncode is not None
assert isinstance(queue.get_nowait(), EngineUnavailableError)