fix(cron): close abandoned coroutine when asyncio.run() raises RuntimeError
Cherry-picked from PR #2290 by @Mibayy. Closes #2138. When asyncio.run() raises RuntimeError (running loop exists), the coroutine was created but never awaited, producing a RuntimeWarning on GC. Extract coro before try, call coro.close() in the except branch before falling back to ThreadPoolExecutor.
This commit is contained in:
parent
453f4c5175
commit
df67ae730b
1 changed files with 7 additions and 3 deletions
|
|
@ -170,11 +170,15 @@ def _deliver_result(job: dict, content: str) -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
# Run the async send in a fresh event loop (safe from any thread)
|
# Run the async send in a fresh event loop (safe from any thread)
|
||||||
|
coro = _send_to_platform(platform, pconfig, chat_id, wrapped, thread_id=thread_id)
|
||||||
try:
|
try:
|
||||||
result = asyncio.run(_send_to_platform(platform, pconfig, chat_id, wrapped, thread_id=thread_id))
|
result = asyncio.run(coro)
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
# asyncio.run() fails if there's already a running loop in this thread;
|
# asyncio.run() checks for a running loop before awaiting the coroutine;
|
||||||
# spin up a new thread to avoid that.
|
# when it raises, the original coro was never started — close it to
|
||||||
|
# prevent "coroutine was never awaited" RuntimeWarning, then retry in a
|
||||||
|
# fresh thread that has no running loop.
|
||||||
|
coro.close()
|
||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
|
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as pool:
|
||||||
future = pool.submit(asyncio.run, _send_to_platform(platform, pconfig, chat_id, wrapped, thread_id=thread_id))
|
future = pool.submit(asyncio.run, _send_to_platform(platform, pconfig, chat_id, wrapped, thread_id=thread_id))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue