Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

fix(hooks): close unawaited coroutine when run_coroutine_sync rejects loop

kaushikharsh99f91fc593d756
diff --git a/laya/hooks.py b/laya/hooks.py
index f2e3696e..5f1b6b6a 100644
--- a/laya/hooks.py
+++ b/laya/hooks.py
@@ -284,12 +284,16 @@ def run_coroutine_sync(coro: Awaitable[Any], loop: Optional[asyncio.AbstractEven
"""
if loop is not None:
if not loop.is_running():
+ if asyncio.iscoroutine(coro):
+ coro.close()
raise ValueError("run_coroutine_sync: the loop passed is not running")
try:
running = asyncio.get_running_loop()
except RuntimeError:
running = None
if loop is running:
+ if asyncio.iscoroutine(coro):
+ coro.close()
raise ValueError(
"run_coroutine_sync: the loop passed is running in the calling thread; "
"blocking on it would deadlock"
diff --git a/tests/test_hooks.py b/tests/test_hooks.py
index 7a9b6fb6..21d684d1 100644
--- a/tests/test_hooks.py
+++ b/tests/test_hooks.py
@@ -7,6 +7,7 @@ with `_encode_state` / `_forward` / `_decode_answers` stubbed). The Router path
Run: python tests/test_hooks.py
"""
import contextvars
+import inspect
import os
import sys
import threading
@@ -1487,22 +1488,26 @@ check_raises("timeout/zero per call is rejected", ValueError,
not_running = asyncio.new_event_loop()
try:
+ c = _seven()
check_raises("async/a non-running loop is rejected", ValueError,
- lambda: run_coroutine_sync(_seven(), loop=not_running))
+ lambda: run_coroutine_sync(c, loop=not_running))
+ check_true("async/a non-running loop closes the coroutine",
+ inspect.getcoroutinestate(c) == "CORO_CLOSED")
finally:
not_running.close()
async def _own_loop():
own = asyncio.get_running_loop()
+ c = _seven()
try:
- run_coroutine_sync(_seven(), loop=own)
+ run_coroutine_sync(c, loop=own)
except ValueError:
- return "raised"
+ return "closed" if inspect.getcoroutinestate(c) == "CORO_CLOSED" else "raised"
return "no"
-check("async/the calling thread's own loop is rejected", asyncio.run(_own_loop()), "raised")
+check("async/the calling thread's own loop is rejected", asyncio.run(_own_loop()), "closed")
check_raises("async/AsyncHook rejects an object with no events", TypeError,
lambda: AsyncHook(object()))