RemoteWorker.cancel makes no coroutine once the loop is closed

run_coroutine_threadsafe refused it and it was never awaited, a RuntimeWarning
under pytest 9. The cancel still logs that it could not reach the worker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WgBfkm4rCmtd1T4vHWjEPx
This commit is contained in:
2026-10-15 16:17:30 +02:00
co-authored by Claude Opus 5.5
parent 0da0c3875e
commit 24d0190c5d
2 changed files with 22 additions and 0 deletions
+5
View File
@@ -249,6 +249,11 @@ class RemoteWorker:
def cancel(self, call_id: str) -> None: def cancel(self, call_id: str) -> None:
"""Ask the worker to kill what it is running for this call.""" """Ask the worker to kill what it is running for this call."""
# A closed loop refuses the coroutine and leaves it never awaited, so it
# is not made at all.
if self._loop.is_closed():
logger.warning("Could not cancel '%s' on '%s'", call_id, self.name)
return
try: try:
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
self._send({"op": "cancel", "call_id": call_id}), self._loop self._send({"op": "cancel", "call_id": call_id}), self._loop
+17
View File
@@ -8,8 +8,10 @@ what happened rather than a wait that never ends.
""" """
import asyncio import asyncio
import gc
import threading import threading
import time import time
import warnings
from collections.abc import Iterator from collections.abc import Iterator
import pytest import pytest
@@ -241,6 +243,21 @@ def test_cancelling_a_run_reaches_only_that_run(loop):
assert [frame["call_id"] for frame in cancels] == ["run-a:flow.node"] assert [frame["call_id"] for frame in cancels] == ["run-a:flow.node"]
def test_a_cancel_after_the_loop_closed_leaves_no_coroutine_behind():
closed = asyncio.new_event_loop()
closed.close()
worker = RemoteWorker(
name="gpu1", labels=[], send=FakeSocket().send_json, loop=closed
)
with warnings.catch_warnings(record=True) as caught:
warnings.simplefilter("always")
worker.cancel("run-a:flow.node")
gc.collect()
assert not [w for w in caught if "never awaited" in str(w.message)]
# ----------------------------------------------------------------------------- # -----------------------------------------------------------------------------
# Liveness, and what a heartbeat is evidence of # Liveness, and what a heartbeat is evidence of
# #