~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""What crosses the ``run_coroutine_threadsafe`` seam, and what silently does not.
Every sync entry point in ``web/services/socketio_asgi.py`` -- ``emit_socket_event``,
``emit_to_user``, ``emit_to_subscribers``, ``_disconnect_matching`` (behind
``disconnect_user`` / ``disconnect_session``) and
``remove_subscriptions_for_research`` -- is called from a background thread
(research workers, the loguru log queue, the scheduler) and hands its real work
to uvicorn's loop with ``asyncio.run_coroutine_threadsafe``. All five DISCARD
the ``concurrent.futures.Future`` that call returns.
That discard is the quiet part. An ``asyncio.Task`` dropped with an unretrieved
exception is reported -- ``Task.__del__`` calls the loop's exception handler and
logs "Task exception was never retrieved". ``concurrent.futures.Future`` has no
such hook: it defines no ``__del__`` at all, so an exception stored in one that
nobody reads is collected in total silence -- no warning, no log, no loop
exception handler. The wrapper has ALREADY returned ``True`` by then, and its
caller records a successful emit. ``TestDiscardedFutureIsSilent`` proves that
asymmetry rather than asserting it from memory.
The consequence is a hard contract on the other side of the seam: **a coroutine
scheduled by this module must handle its own failures, because nothing on the
outside can observe them.** ``TestScheduledCoroutinesHandleTheirOwnFailures``
drives each wrapper with a transport that raises and reads the captured Future
back. All five must contain their transport failure and return a settled Future
instead of losing an exception after the synchronous caller reported success.
The second thing about that seam is what DOES cross it: the ``enable_logging``
flag. It is a ``contextvars.ContextVar`` and not a ``threading.local`` on
purpose. ``frontend_progress_sink`` is a loguru sink, so a failed emit that logs
produces a record carrying the same ``research_id``, which fires the sink, which
emits, which fails -- 500+ emits from one research log line, measured. A
thread-local set on the calling thread is invisible to a coroutine running on
the loop thread, which would read the default (``True``) and log anyway; and the
caller's own ``finally`` resets the flag before the coroutine has even started,
so the suppression window would close early too. ``run_coroutine_threadsafe``
snapshots the calling context at scheduling time, which is exactly what makes
the ContextVar work. ``TestLoggingSuppressionCrossesTheSeam`` pins both halves
(it reaches the coroutine; the caller's reset does not cancel it) and had no
coverage anywhere in the suite.
Two smaller holes are closed here as well:
* ``disconnect`` sweeping a sid out of ``_subscriptions`` is covered for one
user's own keys, but not for the case the key shape exists to prevent:
``_subscriptions`` is keyed ``(username, research_id)`` because a benchmark id
autoincrements inside each user's own encrypted database, so every user's
first run is id ``1``. Keyed by the bare id, Alice's and Bob's run 1 shared
``_subscriptions["1"]`` and Bob received Alice's benchmark progress.
``TestDisconnectCleanupAcrossOwners`` proves Alice disconnecting leaves Bob's
colliding-id bucket -- and Bob's delivery -- untouched.
* ``tests/web/routers/test_fastapi_migration.py::test_socketio_mount_path``
requests the legacy Flask path ``/socket.io`` and comments "should not work",
but never asserts anything about the response. The documented breaking change
(clients must move to ``/ws/socket.io``, or degrade to long-polling) is
therefore pinned in one direction only. ``TestLegacyFlaskPathIsGone`` asserts
the absent half against the live route table.
Deliberately NOT re-litigated, because it is already pinned well:
* ``cors_allowed_origins`` -- ``None`` (derive a same-origin allowlist) versus
``[]`` (engine.io guards validation with ``if self.cors_allowed_origins != []``,
so an empty list disables origin checking outright). See
``tests/security/test_socket_ownership_edges_fastapi.py``
``TestWebSocketOriginPolicyDerivation`` / ``TestNoneVersusEmptyListAreNotInterchangeable``
/ ``TestLiveServerOriginPolicy``, which cover the env-var derivation, the live
server's configured object, and a real cross-origin handshake refusal.
* ``(username, research_id)`` emit scoping and owner-scoped removal --
``tests/web/services/test_subscription_owner_scoping.py``.
* ``emit_to_user`` sid selection and its no-loop branches --
``tests/web/services/test_socketio_asgi_user_scoping.py``; the caller-side
fail-closed skip when there is no request username is
``tests/web/services/test_settings_manager_extended.py::test_emit_settings_changed_skipped_without_request_username``.
* The mount at ``/ws`` and its cross-check against the frontend's hardcoded
client path -- ``tests/web/test_socketio_asgi_contracts.py::TestMountTable``.
"""
import asyncio
import concurrent.futures
import contextlib
import threading
import weakref
from unittest.mock import patch
import pytest
from local_deep_research.web.services import socketio_asgi
from local_deep_research.web.services.socketio_asgi import (
disconnect,
disconnect_session,
disconnect_user,
emit_socket_event,
emit_to_subscribers,
emit_to_user,
remove_subscriptions_for_research,
)
# ---------------------------------------------------------------------------
# Doubles
# ---------------------------------------------------------------------------
class _ExplodingSio:
"""An AsyncServer stand-in whose transport calls always fail.
Records every attempt so a "nothing was reported" assertion can never
pass vacuously because the operation was never tried.
"""
def __init__(self):
self.attempts: list[tuple] = []
async def emit(self, event, data=None, room=None):
self.attempts.append(("emit", event, room))
raise RuntimeError("the transport dropped this emit")
async def disconnect(self, sid):
self.attempts.append(("disconnect", sid))
raise RuntimeError("the transport dropped this disconnect")
class _RecordingSio:
"""An AsyncServer stand-in that succeeds and records delivery."""
def __init__(self):
self.delivered: list[tuple] = []
async def emit(self, event, data=None, room=None):
self.delivered.append((room, event, data))
async def disconnect(self, sid):
self.delivered.append(("disconnect", sid, None))
class _RecordingLogger:
"""Records every loguru call the module makes, by level."""
def __init__(self):
self.calls: list[tuple[str, str]] = []
def messages(self, level: str) -> list[str]:
return [text for lvl, text in self.calls if lvl == level]
def messages_containing(self, needle: str) -> list[str]:
return [text for _, text in self.calls if needle in text]
def __getattr__(self, level):
def _log(message="", *args, **kwargs):
self.calls.append((level, str(message)))
return _log
# ---------------------------------------------------------------------------
# Fixtures
# ---------------------------------------------------------------------------
@pytest.fixture(autouse=True)
def _isolated_socketio_state():
"""Snapshot/restore the module's process-global socket state.
``_lock`` is restored too: ``background_loop`` rebinds it to the loop it
stands up (an asyncio.Lock belongs to the loop that created it), and that
must not be left behind for whatever runs next in this process.
"""
saved_users = dict(socketio_asgi._sid_users)
saved_sessions = dict(socketio_asgi._sid_sessions)
saved_subs = {k: set(v) for k, v in socketio_asgi._subscriptions.items()}
saved_lock = socketio_asgi._lock
socketio_asgi._sid_users.clear()
socketio_asgi._sid_sessions.clear()
socketio_asgi._subscriptions.clear()
yield
socketio_asgi._sid_users.clear()
socketio_asgi._sid_users.update(saved_users)
socketio_asgi._sid_sessions.clear()
socketio_asgi._sid_sessions.update(saved_sessions)
socketio_asgi._subscriptions.clear()
socketio_asgi._subscriptions.update(saved_subs)
socketio_asgi._lock = saved_lock
@pytest.fixture
def background_loop():
"""A real event loop on a background thread, installed as the module's
captured main loop -- what ``set_main_loop`` does under uvicorn, and the
only configuration in which the sync wrappers do anything at all."""
loop = asyncio.new_event_loop()
thread = threading.Thread(target=loop.run_forever, daemon=True)
started = False
try:
thread.start()
started = True
async def _make_lock():
socketio_asgi._lock = asyncio.Lock()
asyncio.run_coroutine_threadsafe(_make_lock(), loop).result(
timeout=_LOOP_HANDOFF_TIMEOUT
)
with patch.object(socketio_asgi, "_get_main_loop", return_value=loop):
yield loop
finally:
if started:
if loop.is_running() and not loop.is_closed():
loop.call_soon_threadsafe(loop.stop)
thread.join(timeout=_LOOP_HANDOFF_TIMEOUT)
assert not thread.is_alive(), (
"background event-loop thread did not stop"
)
if not loop.is_closed():
loop.close()
@contextlib.contextmanager
def _capturing_schedule():
"""Capture the Futures the module discards, so a test can read them.
This is the whole point of the file: production drops these, so the only
way to see what happened inside a scheduled coroutine is to grab the
handle at the seam.
"""
captured: list[concurrent.futures.Future] = []
real_schedule = asyncio.run_coroutine_threadsafe
def _capture(coro, loop):
future = real_schedule(coro, loop)
captured.append(future)
return future
with patch.object(asyncio, "run_coroutine_threadsafe", _capture):
yield captured
def _settle(captured: list[concurrent.futures.Future]) -> list[BaseException]:
"""Wait for every captured coroutine to finish; return their exceptions.
Must be called while the ``sio`` / ``logger`` patches are still active:
the scheduled coroutines resolve those module globals when they RUN, not
when they were created.
"""
concurrent.futures.wait(captured, timeout=_LOOP_HANDOFF_TIMEOUT)
return [
future.exception(timeout=_LOOP_HANDOFF_TIMEOUT) for future in captured
]
def _seed_alice_and_bob():
"""Two users whose research ids COLLIDE -- both hold benchmark run "1".
That collision is not contrived: ``BenchmarkRun.id`` autoincrements inside
each user's own encrypted database, so every user's first benchmark run is
id 1.
"""
socketio_asgi._sid_users.update(
{"sid-alice": "alice", "sid-bob": "bob"},
)
socketio_asgi._sid_sessions.update(
{"sid-alice": "session-alice", "sid-bob": "session-bob"},
)
socketio_asgi._subscriptions.update(
{("alice", 1): {"sid-alice"}, ("bob", 1): {"sid-bob"}},
)
# ---------------------------------------------------------------------------
# Captured-loop fallback during startup/shutdown transitions.
# ---------------------------------------------------------------------------
class TestMainLoopFallback:
def test_a_closed_captured_loop_falls_back_to_the_current_running_loop(
self,
):
"""A stale lifespan loop must not mask a usable caller-side loop.
This is the transition state after one server loop has closed but an
async caller is already running on its replacement. Returning the
closed loop would make every synchronous bridge reject otherwise
deliverable work.
"""
closed = asyncio.new_event_loop()
closed.close()
async def _probe():
running = asyncio.get_running_loop()
with patch.object(socketio_asgi, "_main_loop", closed):
assert socketio_asgi._get_main_loop() is running
asyncio.run(_probe())
# ---------------------------------------------------------------------------
# 1. The seam itself: a dropped concurrent Future reports nothing.
# ---------------------------------------------------------------------------
class TestDiscardedFutureIsSilent:
"""Why the contract in section 2 has to exist.
If a discarded Future did surface its exception, a failing scheduled emit
would at least leave a trace in the logs. It does not.
"""
def test_concurrent_futures_has_no_collection_time_reporting_hook(self):
"""The mechanism, structurally: asyncio reports via ``Task.__del__``;
``concurrent.futures.Future`` defines no ``__del__`` to report from."""
assert "__del__" in vars(asyncio.Task), (
"asyncio.Task no longer defines __del__ -- the 'Task exception "
"was never retrieved' report this file contrasts against has "
"moved, so the comparison below needs revisiting"
)
assert "__del__" not in vars(concurrent.futures.Future), (
"concurrent.futures.Future grew a __del__; if it now reports "
"unretrieved exceptions, the silent-failure hazard this module "
"is built around may have changed"
)
def test_a_dropped_asyncio_task_exception_is_reported(
self, background_loop
):
"""POSITIVE CONTROL for the test below.
Without this, "nothing was reported" would be unfalsifiable -- it
would also pass if the reporting machinery were simply not wired up
in this test at all.
"""
reported = _collect_loop_exception_reports(background_loop)
async def _create_and_drop():
finished = asyncio.Event()
task = asyncio.get_running_loop().create_task(_always_raises())
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
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)
_flush_loop(background_loop)
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"
)
def test_a_dropped_run_coroutine_threadsafe_exception_is_not_reported(
self, background_loop
):
"""The hazard: same failure, scheduled the way this module schedules
it, vanishes completely."""
reported = _collect_loop_exception_reports(background_loop)
future = asyncio.run_coroutine_threadsafe(
_always_raises(), background_loop
)
concurrent.futures.wait([future], timeout=_LOOP_HANDOFF_TIMEOUT)
stored = future.exception(timeout=_LOOP_HANDOFF_TIMEOUT)
assert isinstance(stored, RuntimeError), (
"the coroutine did not fail, so this test proves nothing about "
"how a failure is reported"
)
future_ref = weakref.ref(future)
_flush_loop(background_loop)
del future, stored
_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 "
f"was reported after all ({reported}) -- if that is now true, "
"the five call sites in socketio_asgi.py that drop this Future "
"are no longer silent and this file's premise has changed"
)
# 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, 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
async def _always_raises():
raise RuntimeError("scheduled work failed")
def _collect_loop_exception_reports(loop) -> list:
"""Install a recording exception handler on ``loop``; return its log."""
reported: list = []
def _handler(_loop, context):
reported.append(context.get("message"))
loop.call_soon_threadsafe(loop.set_exception_handler, _handler)
asyncio.run_coroutine_threadsafe(asyncio.sleep(0), loop).result(
timeout=_LOOP_HANDOFF_TIMEOUT
)
return reported
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
)
# ---------------------------------------------------------------------------
# 2. The contract that silence forces: scheduled coroutines must not raise.
# ---------------------------------------------------------------------------
def _drive_emit_to_user():
return emit_to_user("settings_changed", "alice", {"api_key": "sk-secret"})
def _drive_emit_to_subscribers():
return emit_to_subscribers(
"research_progress", "1", {"progress": 10}, owner="alice"
)
def _drive_disconnect_user():
return disconnect_user("alice")
def _drive_disconnect_session():
return disconnect_session("session-alice")
def _drive_emit_socket_event():
return emit_socket_event("chat_response", {"text": "hi"}, room="sid-alice")
class TestScheduledCoroutinesHandleTheirOwnFailures:
"""Nothing outside can observe a scheduled coroutine's exception, so the
coroutine has to contain it. A raise here is a failure that reaches no
log and no caller."""
@pytest.mark.parametrize(
"drive",
[
pytest.param(_drive_emit_to_user, id="emit_to_user"),
pytest.param(_drive_emit_to_subscribers, id="emit_to_subscribers"),
pytest.param(_drive_disconnect_user, id="disconnect_user"),
pytest.param(_drive_disconnect_session, id="disconnect_session"),
pytest.param(_drive_emit_socket_event, id="emit_socket_event"),
],
)
def test_a_failing_transport_does_not_escape_into_the_dropped_future(
self, background_loop, drive
):
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
scheduled_ok = drive()
exceptions = _settle(captured)
assert scheduled_ok is True, (
"the wrapper reported it could not schedule its work, so the "
"coroutine under test never ran"
)
assert exploding.attempts, (
"the failing transport was never called -- 'no exception "
"escaped' would pass for free"
)
assert exceptions == [None], (
"a scheduled coroutine let an exception escape into the "
"concurrent.futures.Future that socketio_asgi discards: "
f"{exceptions}. Nothing retrieves that Future, so the failure "
"is invisible -- no log, no loop exception handler -- while the "
"wrapper already returned True to its caller"
)
def test_the_wrappers_return_true_even_when_every_delivery_fails(
self, background_loop
):
"""The other half of the same contract, stated so no caller writes a
delivery assertion on this boolean: it means 'scheduled', never
'delivered'."""
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
reported = emit_to_user("settings_changed", "alice", {})
_settle(captured)
assert reported is True
assert [kind for kind, *_ in exploding.attempts] == ["emit"], (
"expected exactly one failed delivery attempt for alice's single "
f"socket, got {exploding.attempts}"
)
# ---------------------------------------------------------------------------
# 3. What DOES cross the seam: the logging-suppression ContextVar.
# ---------------------------------------------------------------------------
class TestLoggingSuppressionCrossesTheSeam:
"""``enable_logging=False`` must reach the coroutine, or the log-sink
emit path amplifies: a failed emit logs, the record carries the same
research_id, the sink fires, emits, fails again."""
def test_positive_control_a_failed_emit_logs_when_logging_is_enabled(
self, background_loop
):
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
emit_to_subscribers(
"research_progress", "1", {}, owner="alice", enable_logging=True
)
_settle(captured)
assert recorder.messages_containing(
"Error emitting to subscriber sid-alice"
), (
"a failed emit produced no debug log with logging enabled, so "
f"the suppression test below is unfalsifiable: {recorder.calls}"
)
def test_suppression_reaches_the_coroutine_on_the_loop_thread(
self, background_loop
):
"""The threading.local bug, pinned: a thread-local set on the calling
thread is invisible to a coroutine running on the loop thread, which
would read the default (True) and log anyway."""
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
emit_to_subscribers(
"research_progress",
"1",
{},
owner="alice",
enable_logging=False,
)
_settle(captured)
assert exploding.attempts, (
"the emit never failed, so there was nothing to suppress"
)
assert recorder.calls == [], (
"enable_logging=False did not reach the coroutine: it logged "
f"{recorder.calls}. That log record inherits the research_id, "
"re-enters frontend_progress_sink, and emits again"
)
def test_the_callers_reset_does_not_close_the_window_early(
self, background_loop
):
"""``emit_to_subscribers`` resets the ContextVar in ``finally``, which
runs BEFORE the coroutine does. Suppression must survive that, which
only works because ``run_coroutine_threadsafe`` snapshots the calling
context at scheduling time."""
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
# Wedge the loop THREAD (not a coroutine on it) so the scheduled
# work provably cannot start until this test releases it.
blocked = threading.Event()
released = threading.Event()
def _wedge():
blocked.set()
released.wait(timeout=_LOOP_HANDOFF_TIMEOUT)
background_loop.call_soon_threadsafe(_wedge)
assert blocked.wait(timeout=_LOOP_HANDOFF_TIMEOUT), (
"the loop thread never reached the wedge"
)
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
emit_to_subscribers(
"research_progress",
"1",
{},
owner="alice",
enable_logging=False,
)
# The caller has returned, so its ``finally`` has already run.
assert socketio_asgi._logging_is_enabled() is True, (
"emit_to_subscribers leaked its suppression into the calling "
"thread's context instead of resetting it"
)
assert not captured[0].done(), (
"the scheduled coroutine already ran, so this test is not "
"actually proving that suppression outlives the caller's reset"
)
released.set()
_settle(captured)
assert exploding.attempts, "the emit never failed; nothing to suppress"
assert recorder.calls == [], (
"the coroutine logged after the caller had already reset the "
f"ContextVar: {recorder.calls} -- the suppression window closed "
"before the work it was meant to cover"
)
def test_two_threads_do_not_corrupt_each_others_suppression(
self, background_loop
):
"""Why this is a ContextVar and not a module global: concurrent emits
from different worker threads each need their own window."""
_seed_alice_and_bob()
exploding = _ExplodingSio()
recorder = _RecordingLogger()
start = threading.Barrier(2)
results: dict[str, bool] = {}
def _worker(name: str, enable: bool):
start.wait(timeout=_LOOP_HANDOFF_TIMEOUT)
emit_to_subscribers(
"research_progress",
"1",
{"from": name},
owner="alice",
enable_logging=enable,
)
results[name] = socketio_asgi._logging_is_enabled()
with (
patch.object(socketio_asgi, "sio", exploding),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
threads = [
threading.Thread(target=_worker, args=("loud", True)),
threading.Thread(target=_worker, args=("quiet", False)),
]
for thread in threads:
thread.start()
for thread in threads:
thread.join(timeout=_LOOP_HANDOFF_TIMEOUT)
assert not thread.is_alive(), "emit worker thread did not stop"
_settle(captured)
assert len(exploding.attempts) == 2, (
f"expected both emits to be attempted and fail, got "
f"{exploding.attempts}"
)
assert results == {"loud": True, "quiet": True}, (
"a worker's suppression escaped its own context after the call "
f"returned: {results}"
)
logged = recorder.messages_containing("Error emitting to subscriber")
assert len(logged) == 1, (
"exactly one of the two concurrent emits asked for logging, so "
f"exactly one emit failure should have been logged; got {logged} "
f"out of {recorder.calls}"
)
def test_suppression_is_reset_even_when_scheduling_never_happens(self):
"""The no-loop early return still runs the ``finally``. If it did not,
a background thread that emitted once before startup captured the loop
would stay silenced for its whole life."""
with patch.object(socketio_asgi, "_get_main_loop", return_value=None):
scheduled = emit_to_subscribers(
"research_progress",
"1",
{},
owner="alice",
enable_logging=False,
)
assert scheduled is False, (
"emit_to_subscribers claimed it scheduled an emit with no event "
"loop available"
)
assert socketio_asgi._logging_is_enabled() is True, (
"the suppression flag was left set after an early return -- this "
"thread will never log a socket failure again"
)
# ---------------------------------------------------------------------------
# 4. Disconnect cleanup must not reach across owners.
# ---------------------------------------------------------------------------
class TestDisconnectCleanupAcrossOwners:
"""Alice and Bob both hold run "1" -- the id collision that made the
bare-id keying a cross-user leak. One disconnecting must not touch the
other."""
def test_a_disconnect_drops_only_the_disconnecting_users_bucket(
self, background_loop
):
_seed_alice_and_bob()
recording = _RecordingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", recording),
patch.object(socketio_asgi, "logger", recorder),
):
asyncio.run_coroutine_threadsafe(
disconnect("sid-alice"), background_loop
).result(timeout=_LOOP_HANDOFF_TIMEOUT)
assert ("alice", 1) not in socketio_asgi._subscriptions, (
"the disconnecting sid's own subscription bucket survived"
)
assert socketio_asgi._subscriptions == {("bob", 1): {"sid-bob"}}, (
"alice disconnecting mutated bob's subscription to HIS run 1: "
f"{socketio_asgi._subscriptions}. The ids collide by design "
"(every user's first benchmark run is id 1), which is why the "
"map is keyed by (username, research_id)"
)
assert socketio_asgi._sid_users == {"sid-bob": "bob"}
assert socketio_asgi._sid_sessions == {"sid-bob": "session-bob"}
def test_bob_still_receives_his_own_run_after_alice_disconnects(
self, background_loop
):
"""Delivery-level proof, with the positive control first: Bob gets his
run-1 event both before and after Alice's socket goes away."""
_seed_alice_and_bob()
recording = _RecordingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", recording),
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
emit_to_subscribers(
"research_progress", "1", {"whose": "bob"}, owner="bob"
)
_settle(captured)
before = list(recording.delivered)
asyncio.run_coroutine_threadsafe(
disconnect("sid-alice"), background_loop
).result(timeout=_LOOP_HANDOFF_TIMEOUT)
recording.delivered.clear()
captured.clear()
emit_to_subscribers(
"research_progress", "1", {"whose": "bob"}, owner="bob"
)
_settle(captured)
after = list(recording.delivered)
assert before == [
("sid-bob", "research_progress_1", {"whose": "bob"})
], (
f"positive control failed -- bob never received his own run: {before}"
)
assert after == before, (
"alice disconnecting silently unsubscribed bob from his own "
f"benchmark run 1: {after}"
)
def test_an_unknown_sid_disconnecting_changes_nothing(
self, background_loop
):
"""The sweep iterates every bucket regardless of owner, so it must be
a no-op for a sid it does not hold."""
_seed_alice_and_bob()
recording = _RecordingSio()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "sio", recording),
patch.object(socketio_asgi, "logger", recorder),
):
asyncio.run_coroutine_threadsafe(
disconnect("sid-nobody"), background_loop
).result(timeout=_LOOP_HANDOFF_TIMEOUT)
assert socketio_asgi._subscriptions == {
("alice", 1): {"sid-alice"},
("bob", 1): {"sid-bob"},
}
assert set(socketio_asgi._sid_users) == {"sid-alice", "sid-bob"}
# ---------------------------------------------------------------------------
# 5. remove_subscriptions_for_research: the branch with no loop.
# ---------------------------------------------------------------------------
class TestRemoveSubscriptionsWithoutALoop:
"""Called from research teardown, which can run before the loop is
captured or after it has closed. It must drop the cleanup, not the
subscriptions -- the disconnect handler reaps those anyway."""
def test_positive_control_a_live_loop_removes_the_owners_bucket(
self, background_loop
):
_seed_alice_and_bob()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "logger", recorder),
_capturing_schedule() as captured,
):
remove_subscriptions_for_research("1", "alice")
_settle(captured)
assert socketio_asgi._subscriptions == {("bob", 1): {"sid-bob"}}
def test_no_loop_drops_the_cleanup_without_touching_state_or_raising(self):
_seed_alice_and_bob()
recorder = _RecordingLogger()
with (
patch.object(socketio_asgi, "_get_main_loop", return_value=None),
patch.object(socketio_asgi, "logger", recorder),
):
remove_subscriptions_for_research("1", "alice")
assert socketio_asgi._subscriptions == {
("alice", 1): {"sid-alice"},
("bob", 1): {"sid-bob"},
}, "state was mutated off the event loop, racing the async path"
def test_a_stopped_loop_is_treated_as_unavailable(self):
"""``_get_main_loop`` can hand back a closed/stopped loop during
shutdown; scheduling onto it would raise inside a teardown path."""
stopped = asyncio.new_event_loop()
recorder = _RecordingLogger()
try:
with (
patch.object(
socketio_asgi, "_get_main_loop", return_value=stopped
),
patch.object(socketio_asgi, "logger", recorder),
):
remove_subscriptions_for_research("1", "alice")
assert (
emit_to_subscribers(
"research_progress", "1", {}, owner="alice"
)
is False
), (
"emit_to_subscribers reported success against a loop that "
"is not running -- the emit can never be delivered"
)
finally:
stopped.close()
# ---------------------------------------------------------------------------
# 6. The documented breaking change: /socket.io is gone.
# ---------------------------------------------------------------------------
def _route_path(route) -> str:
return getattr(route, "path", "")
class TestLegacyFlaskPathIsGone:
"""``test_fastapi_migration.py::test_socketio_mount_path`` requests
``/socket.io`` and comments "should not work", but asserts nothing about
it. Clients that still use the Flask-SocketIO path must fail to find a
Socket.IO endpoint rather than half-work."""
def test_the_route_table_still_serves_ws(self, app):
"""Premise guard: a scan that found no socket route at all would make
the assertion below vacuous."""
assert "/ws" in [_route_path(r) for r in app.routes], (
"the '/ws' mount is not visible in the live app's route table, "
"so scanning that table for the legacy path proves nothing"
)
def test_nothing_is_served_on_the_legacy_socket_io_prefix(self, app):
offenders = [
_route_path(r)
for r in app.routes
if _route_path(r).startswith("/socket.io")
]
assert offenders == [], (
f"the legacy Flask-SocketIO path is served again: {offenders}. "
"PR #3299 moved the client path to /ws/socket.io as a documented "
"breaking change; serving both re-splits the endpoint and lets an "
"un-updated client reach a Socket.IO server other than the one "
"behind the handshake gate"
)
def test_the_engineio_path_carries_the_full_mount_prefix(self):
"""``socketio_path="/ws/socket.io"`` looks like a doubled prefix next
to ``app.mount("/ws", socket_app)``, and is not.
Starlette's ``Mount.matches`` builds a child scope that advances
``root_path`` and leaves ``scope["path"]`` untouched, while
``socketio.ASGIApp.__call__`` matches ``scope["path"]`` against
``engineio_path``. The sub-app therefore still sees the FULL path.
Trimming ``socketio_path`` to "socket.io" to "fix" the duplication
makes every handshake miss, with no server-side error -- just a
frozen progress UI.
"""
assert (
socketio_asgi.socket_app.engineio_path.rstrip("/")
== "/ws/socket.io"
), (
"socket_app.engineio_path is "
f"{socketio_asgi.socket_app.engineio_path!r}; it must be the full "
"path including the /ws mount prefix, because socketio.ASGIApp "
"matches the unmodified scope['path'], not a mount-stripped "
"remainder"
)