Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

test: bound async exception control lifecycle (#7086)

LearningCircuit1318f70f603c
diff --git a/changelog.d/+async-task-control-lifecycle.misc.md b/changelog.d/+async-task-control-lifecycle.misc.md
new file mode 100644
index 0000000000..0c37f33df7
--- /dev/null
+++ b/changelog.d/+async-task-control-lifecycle.misc.md
@@ -0,0 +1 @@
+Make asynchronous exception-reporting test controls wait for completion and verify finalization without collecting the entire test process.
diff --git a/tests/web/services/test_socketio_asgi_contracts.py b/tests/web/services/test_socketio_asgi_contracts.py
index acdba8bc1e..8d1580d102 100644
--- a/tests/web/services/test_socketio_asgi_contracts.py
+++ b/tests/web/services/test_socketio_asgi_contracts.py
@@ -77,8 +77,8 @@ Deliberately NOT re-litigated, because it is already pinned well:
import asyncio
import concurrent.futures
import contextlib
-import gc
import threading
+import weakref
from unittest.mock import patch
import pytest
@@ -329,25 +329,26 @@ class TestDiscardedFutureIsSilent:
reported = _collect_loop_exception_reports(background_loop)
async def _create_and_drop():
+ finished = asyncio.Event()
task = asyncio.get_running_loop().create_task(_always_raises())
- await asyncio.sleep(0)
+ task_ref = weakref.ref(task)
+ task.add_done_callback(lambda _task: finished.set())
+ await finished.wait()
+ assert task.done()
+ # Do not await the failed task or retrieve its exception: either
+ # would suppress the reporting this positive control proves.
del task
- gc.collect()
-
- # _LOOP_HANDOFF_TIMEOUT, not a bare 5s: this waits on a genuine
- # cross-thread hand-off to background_loop's daemon thread, and the
- # coroutine it waits for runs a full gc.collect() on that thread.
- # Locally the whole call takes ~1.2s, so 5s was only a 4x margin --
- # which CI's `-n auto` worker contention eats (the observed CI
- # failure here was TimeoutError, not the assertion below: the loop
- # simply had not been scheduled yet). Nothing about what is asserted
- # changes; only how long we are willing to wait for the loop thread.
+ await asyncio.sleep(0)
+ assert task_ref() is None, "the failed task was not finalized"
+
+ # The barrier and weak reference prove completion and finalization
+ # without collecting unrelated objects from the entire test process.
asyncio.run_coroutine_threadsafe(
_create_and_drop(), background_loop
).result(timeout=_LOOP_HANDOFF_TIMEOUT)
- _run_gc_on(background_loop)
+ _flush_loop(background_loop)
- assert reported, (
+ assert reported == ["Task exception was never retrieved"], (
"dropping a failed asyncio.Task reported nothing -- the loop "
"exception handler used as this file's control is not working"
)
@@ -369,9 +370,11 @@ class TestDiscardedFutureIsSilent:
"how a failure is reported"
)
+ future_ref = weakref.ref(future)
+ _flush_loop(background_loop)
del future, stored
- _run_gc_on(background_loop)
- gc.collect()
+ _flush_loop(background_loop)
+ assert future_ref() is None, "the discarded future was not finalized"
assert reported == [], (
"an exception discarded with a run_coroutine_threadsafe Future "
@@ -383,10 +386,9 @@ class TestDiscardedFutureIsSilent:
# Every wait in this file that blocks on background_loop's daemon thread
# doing something uses this bound. It is deliberately generous: these are
-# cross-thread hand-offs (and one of them runs gc.collect() on the loop
-# thread), so under CI's `-n auto` contention the loop can go unscheduled
-# for seconds. A too-tight bound turns that into a TimeoutError that looks
-# like a product failure but is only starvation.
+# cross-thread hand-offs, so under CI's `-n auto` contention the loop can go
+# unscheduled for seconds. A too-tight bound turns that into a TimeoutError
+# that looks like a product failure but is only starvation.
_LOOP_HANDOFF_TIMEOUT = 20
@@ -408,13 +410,8 @@ def _collect_loop_exception_reports(loop) -> list:
return reported
-def _run_gc_on(loop) -> None:
- """Collect on the loop thread, then flush the loop's callback queue.
-
- ``Task.__del__`` schedules the report through ``call_exception_handler``,
- so the collection has to happen where the loop can then run it.
- """
- loop.call_soon_threadsafe(gc.collect)
+def _flush_loop(loop) -> None:
+ """Finish queued callbacks before inspecting finalization and reports."""
asyncio.run_coroutine_threadsafe(asyncio.sleep(0), loop).result(
timeout=_LOOP_HANDOFF_TIMEOUT
)