~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
"""Streaming / SSE contracts for the WSGI -> ASGI (Flask -> FastAPI) port.
Streaming is where the two execution models diverge hardest. Flask's
``Response(stream_with_context(generator()))`` is pulled by the WSGI server
on the request's own worker thread: nothing else in the process cares when
that generator blocks, and abandoning it costs one thread. Starlette drives
the same sync generator through ``iterate_in_threadpool`` on behalf of a
single event loop, and the loop is the whole instance. Two real bugs on this
branch came straight out of that difference:
1. ``StreamingResponse(BytesIO(pdf_bytes))``. ``StreamingResponse`` iterates
its content to produce ASGI ``http.response.body`` sends, and iterating a
``BytesIO`` yields *lines* -- one send per ``0x0A`` byte in a binary
payload, and no ``Content-Length`` at all. Fixed twice (library.py's PDF
route, research.py's report export) and guarded statically since, by
``tests/web/routers/test_migration_antipattern_guards.py::
test_no_streamingresponse_wraps_bytesio``.
2. ``worker_thread.join(timeout=5.0)`` in a streaming generator's
``finally``. On client disconnect Starlette finalises the sync generator
by closing it, and that close runs on the EVENT LOOP -- so a five-second
drain in the ``finally`` is five seconds during which every other user's
request, every Socket.IO event and the health check are frozen.
WHAT THE SUITE ALREADY COVERS (read before adding here)
-------------------------------------------------------
* ``tests/web/routers/test_migration_antipattern_guards.py`` -- AST guard for
``StreamingResponse(BytesIO(...))`` (bug 1), plus its scanner self-tests.
* ``tests/web/routers/test_streaming_contracts.py`` -- AST invariants: no DB
session held across a ``yield``; media types are SSE-or-NDJSON; no
*unbounded* ``Thread.join()`` in a generator's own scope; binary exports
use plain ``Response``.
* ``tests/web/routers/test_sse_response_headers.py`` -- per-endpoint HTTP
tests that each of the four SSE routes sets ``Cache-Control: no-cache`` and
``X-Accel-Buffering: no``, plus an AST audit for new SSE routes.
* ``tests/web/test_streaming_incremental_delivery.py`` -- DELETED by the
commit that added this file. It intended to prove chunks reach the client
before the generator finishes, behind the app's real rate-limit wiring,
but ``TestClient`` cannot observe incremental delivery, so it could not
fail; the property is re-proven here (see the next section).
WHAT THIS FILE ADDS, AND WHY THE INCREMENTAL-DELIVERY CHECK IS REDONE HERE
--------------------------------------------------------------------------
Starlette's ``TestClient`` cannot observe incremental delivery *at all*. Its
transport does ``with self.portal_factory() as portal: portal.call(self.app,
scope, receive, send)`` (starlette/testclient.py) -- a blocking call that
returns only once the ASGI app has finished, after which the collected body
parts are handed to ``httpx`` as one ``ByteStream``. ``client.stream(...)``
therefore always reads an already-complete response, and
``httpx.ASGITransport`` buffers identically (``body_parts`` joined after
``await self.app(...)``). A test that gates its generator on
``event.wait(timeout=5)`` and then asserts "the first chunk arrived and the
event is still unset" passes for a fully buffered stack too: the wait simply
times out, the generator finishes, and the assertion holds. So the property
is currently unproven, and the only faithful in-process way to check it is to
call the ASGI app directly and record the ``send`` messages -- which is what
``_drive_until_generator_blocks`` below does, with a strict happens-before
(not a timing window) as the assertion.
On top of that this file covers, behaviourally: what disconnect-time cleanup
does and which thread it runs on (bug 2); what an error raised after the
headers are already on the wire looks like to the client; that a fully
materialised binary payload really is one body message with a real
``Content-Length`` (bug 1, executable rather than static); and that an SSE
frame cannot be split by an interpolated newline.
"""
import ast
import asyncio
import io
import json
import threading
from contextlib import contextmanager
from pathlib import Path
import anyio
import pytest
import uvicorn
from fastapi.testclient import TestClient
from slowapi.middleware import SlowAPIMiddleware
from starlette.applications import Starlette
from starlette.middleware import Middleware
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.responses import Response, StreamingResponse
from starlette.routing import Route
import local_deep_research.web.routers as routers_pkg
from local_deep_research.web.fastapi_app import app
pytestmark = pytest.mark.timeout(60)
#: Every blocking wait in this file is bounded so a regression fails the
#: suite instead of hanging CI.
TIMEOUT = 5.0
#: How long to give a deliberately-stalled event loop to prove it is stalled.
#: Only ever used for an assertion in the "must NOT happen" direction: a loop
#: blocked in a generator's ``finally`` can never service the probe, no matter
#: how long the window is, so this cannot flake into a false pass.
STALL_PROBE_SECONDS = 0.25
#: Ceiling on how long a streaming generator's own-scope drain may block.
#: See ``test_disconnect_drain_joins_stay_within_the_stall_budget``.
EVENT_LOOP_STALL_BUDGET_SECONDS = 5.0
#: Test-only routes live under ``/__``: `tests/web/routers/test_all_endpoints
#: .py` skips that prefix precisely because probe routes registered on the
#: live singleton by other test modules must not leak into route sweeps.
PROBE_PREFIX = "/__stream_probe__"
#: A payload that is (a) binary and (b) full of 0x0A bytes, which is the
#: exact combination that made ``StreamingResponse(BytesIO(...))`` shred a
#: PDF into thousands of one-line ASGI sends.
BINARY_PAYLOAD = b"%PDF-1.7\n" + bytes(range(256)) * 8 + b"\ntrailer\n%%EOF\n"
ROUTERS_DIR = Path(routers_pkg.__file__).resolve().parent
ROUTER_FILES = sorted(ROUTERS_DIR.glob("*.py"))
# ===========================================================================
# Harness: drive an ASGI app directly and record what it sends.
# ===========================================================================
def _asgi_scope(path: str) -> dict:
"""A GET scope shaped like the one uvicorn builds.
``spec_version`` matters: ``StreamingResponse.__call__`` picks its
disconnect handling from it (2.4+ relies on ``send`` raising ``OSError``;
below that it races ``stream_response`` against ``listen_for_disconnect``
in a task group). uvicorn advertises "2.3", so that is the branch
production runs and the branch these tests exercise -- pinned by
``test_the_disconnect_branch_under_test_is_the_one_uvicorn_uses``.
"""
return {
"type": "http",
"asgi": {"version": "3.0", "spec_version": "2.3"},
"http_version": "1.1",
"method": "GET",
"scheme": "http",
"path": path,
"raw_path": path.encode(),
"root_path": "",
"query_string": b"",
"headers": [(b"host", b"testserver"), (b"accept", b"*/*")],
"client": ("127.0.0.1", 50000),
"server": ("testserver", 80),
}
class _Gate:
"""A two-chunk sync generator with hand-operated release points.
``chunks()`` is deliberately the shape every SSE route in the app has: a
plain ``def`` generator (so Starlette wraps it in ``iterate_in_threadpool``)
that produces something, blocks waiting on work, produces more, and does
its cleanup in a ``finally``.
"""
def __init__(self, *, block_cleanup: bool = False) -> None:
self.first_chunk_consumed = threading.Event()
self.release = threading.Event()
self.cleanup_entered = threading.Event()
self.cleanup_finished = threading.Event()
self.cleanup_thread_ident: int | None = None
self.cleanup_release = threading.Event()
if not block_cleanup:
self.cleanup_release.set()
def chunks(self):
try:
yield b"chunk-1\n"
# Reached only when the consumer asks for the SECOND chunk, i.e.
# strictly after the first one was handed to `send`. That ordering
# is what makes the buffering check below a happens-before test
# rather than a timing window.
self.first_chunk_consumed.set()
self.release.wait(timeout=TIMEOUT)
yield b"chunk-2\n"
finally:
self.cleanup_thread_ident = threading.get_ident()
self.cleanup_entered.set()
self.cleanup_release.wait(timeout=TIMEOUT)
self.cleanup_finished.set()
class _BufferingMiddleware(BaseHTTPMiddleware):
"""A middleware that drains the response before forwarding any of it.
This is the failure mode the whole "does BaseHTTPMiddleware buffer?"
question is about, written out explicitly so the detector above can be
shown to catch it.
"""
async def dispatch(self, request, call_next):
response = await call_next(request)
body = b"".join([chunk async for chunk in response.body_iterator])
return Response(
body, status_code=response.status_code, media_type="text/plain"
)
async def _drive_until_generator_blocks(asgi_app, path, gate):
"""Run ``asgi_app`` and snapshot delivery progress at a fixed point.
Returns ``(resumed, first_body_already_sent, messages)`` where the
snapshot is taken at the instant the generator is resumed for its second
chunk. A streaming stack must already have forwarded chunk-1 by then; a
buffering stack cannot have forwarded anything.
"""
messages: list[dict] = []
first_body_sent = threading.Event()
async def receive():
# Production's client stays connected for the whole stream, so this
# never resolves; `stream_response` finishing cancels the listener.
await asyncio.Event().wait()
async def send(message):
messages.append(message)
if message["type"] == "http.response.body" and message.get("body"):
first_body_sent.set()
task = asyncio.ensure_future(asgi_app(_asgi_scope(path), receive, send))
try:
resumed = await asyncio.to_thread(
gate.first_chunk_consumed.wait, TIMEOUT
)
snapshot = first_body_sent.is_set()
finally:
gate.release.set()
await task
return resumed, snapshot, messages
async def _collect_response_messages(response):
"""Every ASGI message a Response/StreamingResponse emits, in order."""
messages: list[dict] = []
async def receive():
await asyncio.Event().wait()
async def send(message):
messages.append(message)
await response(_asgi_scope("/download"), receive, send)
return messages
def _header_map(start_message):
return {
key.lower(): value for key, value in start_message.get("headers", [])
}
def _bodies(messages):
return [m for m in messages if m["type"] == "http.response.body"]
def _starts(messages):
return [m for m in messages if m["type"] == "http.response.start"]
@contextmanager
def _background_event_loop():
"""An event loop on its own thread, so the test thread can poke it."""
loop = asyncio.new_event_loop()
ready = threading.Event()
def _run():
asyncio.set_event_loop(loop)
loop.call_soon(ready.set)
loop.run_forever()
thread = threading.Thread(
target=_run, name="streaming-contract-loop", daemon=True
)
thread.start()
ready.wait(TIMEOUT)
try:
yield loop
finally:
loop.call_soon_threadsafe(loop.stop)
thread.join(TIMEOUT)
loop.close()
# ===========================================================================
# Probe routes on the live app singleton.
# ===========================================================================
#
# Registered once, at import time, on the same `app` object production
# serves -- so a request to one of them traverses the real, already-built
# middleware stack (SlowAPI -> SecureCookie -> SecurityHeaders -> BodySizeLimit
# -> RememberMe -> Session -> CSRF -> Database), with nothing re-created or
# stubbed. Same pattern, and same `/__` prefix convention, as
# `tests/web/test_middleware_order_and_headers.py`.
_GATE_SLOT: dict[str, _Gate] = {}
class _ProbeStreamFailure(RuntimeError):
"""Deliberately not a type ``_register_exception_handlers`` binds.
It therefore reaches Starlette's ``ServerErrorMiddleware``, which is the
realistic path for "the generator blew up half way through a response".
"""
@app.get(f"{PROBE_PREFIX}/gated-stream", include_in_schema=False)
async def _probe_gated_stream():
return StreamingResponse(
_GATE_SLOT["gate"].chunks(), media_type="text/plain"
)
@app.get(f"{PROBE_PREFIX}/sse", include_in_schema=False)
async def _probe_sse():
async def frames():
yield f"data: {json.dumps({'type': 'start'})}\n\n".encode()
yield f"data: {json.dumps({'type': 'complete'})}\n\n".encode()
response = StreamingResponse(frames(), media_type="text/event-stream")
# Byte-for-byte the header set rag.py's index_collection sends.
response.headers["Cache-Control"] = "no-cache, no-transform"
response.headers["Connection"] = "keep-alive"
response.headers["X-Accel-Buffering"] = "no"
return response
@app.get(f"{PROBE_PREFIX}/binary", include_in_schema=False)
async def _probe_binary():
return Response(content=BINARY_PAYLOAD, media_type="application/pdf")
@app.get(f"{PROBE_PREFIX}/fails-mid-stream", include_in_schema=False)
async def _probe_fails_mid_stream():
def frames():
yield f"data: {json.dumps({'type': 'progress'})}\n\n".encode()
raise _ProbeStreamFailure("probe: generator failed after the headers")
return StreamingResponse(frames(), media_type="text/event-stream")
@pytest.fixture
def gate():
"""Install a fresh gate for the ``/gated-stream`` probe route."""
installed = _Gate()
_GATE_SLOT["gate"] = installed
try:
yield installed
finally:
# Never leave a worker thread parked in `release.wait(...)`.
installed.release.set()
installed.cleanup_release.set()
_GATE_SLOT.pop("gate", None)
@pytest.fixture
def stream_client(app):
"""TestClient on the real app.
Good for headers, status codes and complete bodies. Useless for
incremental delivery -- see this module's docstring: the transport
materialises the entire response before it returns.
"""
return TestClient(app, raise_server_exceptions=False)
# ===========================================================================
# 1. Incremental delivery through the app's real middleware stack.
# ===========================================================================
def test_the_full_stack_really_wraps_streams_in_a_basehttpmiddleware(app):
"""Premise guard for the delivery test below.
The buffering worry is specifically about ``BaseHTTPMiddleware``, which
wraps the downstream response in an internal ``_StreamingResponse``.
If the app stopped registering one, the delivery test would still pass --
but it would have stopped testing the thing it exists to test, so say so
here rather than let it quietly become a formality.
"""
registered = [entry.cls for entry in app.user_middleware]
assert SlowAPIMiddleware in registered, (
"SlowAPIMiddleware is no longer registered on the app; it is the "
"BaseHTTPMiddleware that wraps every SSE route, and the incremental "
f"delivery test below was written around it. Registered: "
f"{[cls.__name__ for cls in registered]}"
)
assert issubclass(SlowAPIMiddleware, BaseHTTPMiddleware), (
"SlowAPIMiddleware is no longer a BaseHTTPMiddleware -- re-read "
"whether the buffering question still applies before trusting the "
"delivery test's framing"
)
def test_first_chunk_leaves_the_full_stack_before_the_generator_finishes(
app, gate
):
"""The property every progress bar in the product depends on.
Not a timing test: the snapshot is taken at the moment the generator is
resumed for chunk-2, which by construction happens strictly after
``send`` was called with chunk-1 in a streaming stack, and strictly
before any ``send`` at all in a buffering one.
"""
resumed, first_body_already_sent, messages = asyncio.run(
_drive_until_generator_blocks(app, f"{PROBE_PREFIX}/gated-stream", gate)
)
starts = _starts(messages)
assert len(starts) == 1, (
f"expected exactly one http.response.start, got {len(starts)} -- "
"a second one is an ASGI protocol violation"
)
assert starts[0]["status"] == 200, (
f"the probe route did not reach the handler (status "
f"{starts[0]['status']}); the delivery assertion below would be "
"meaningless"
)
# The response really did come through the app's own middleware, not
# some bare router: SecurityHeadersMiddleware stamps this on every
# http.response.start it sees.
assert b"x-content-type-options" in _header_map(starts[0]), (
"the streaming response carries no security headers, so it did not "
"traverse the app's middleware stack -- this test is not testing "
"what it claims to"
)
assert resumed, (
f"the probe generator never produced its first chunk within {TIMEOUT}s"
)
assert first_body_already_sent, (
"chunk-1 had NOT been sent by the time the generator was asked for "
"chunk-2: the app's middleware stack buffers streaming responses. "
"Every SSE progress bar, every bulk-download counter and every "
"indexing log in the product then arrives in one burst at the end."
)
def test_the_delivery_detector_reports_streaming_with_nothing_in_the_way():
"""Positive control for the detector itself."""
control = _Gate()
app_under_test = Starlette(
routes=[
Route(
"/stream",
lambda request: StreamingResponse(
control.chunks(), media_type="text/plain"
),
)
]
)
resumed, first_body_already_sent, _ = asyncio.run(
_drive_until_generator_blocks(app_under_test, "/stream", control)
)
assert resumed
assert first_body_already_sent, (
"with no middleware at all the detector still says 'buffered' -- the "
"detector is broken, not the app"
)
def test_the_delivery_detector_reports_buffering_when_a_middleware_buffers():
"""Negative control: break the property, the test must fail.
Same generator, same harness, one middleware that drains the body
iterator before forwarding anything.
"""
control = _Gate()
app_under_test = Starlette(
routes=[
Route(
"/stream",
lambda request: StreamingResponse(
control.chunks(), media_type="text/plain"
),
)
],
middleware=[Middleware(_BufferingMiddleware)],
)
resumed, first_body_already_sent, _ = asyncio.run(
_drive_until_generator_blocks(app_under_test, "/stream", control)
)
assert resumed
assert not first_body_already_sent, (
"a middleware that joins the whole body before returning was NOT "
"detected as buffering -- the assertion in "
"test_first_chunk_leaves_the_full_stack_before_the_generator_"
"finishes has no teeth"
)
# ===========================================================================
# 2. Client disconnect mid-stream.
# ===========================================================================
def test_generator_cleanup_does_not_run_while_the_stream_is_healthy():
"""Positive control for every cleanup assertion in this section.
Without it, "cleanup ran on disconnect" would be satisfied by a
``finally`` that runs unconditionally at some unrelated moment.
"""
control = _Gate()
async def read_one_chunk():
response = StreamingResponse(control.chunks(), media_type="text/plain")
chunk = await response.body_iterator.__anext__()
return chunk, control.cleanup_entered.is_set()
chunk, cleaned_up = asyncio.run(read_one_chunk())
assert chunk == b"chunk-1\n"
assert not cleaned_up, (
"the generator's finally ran while the stream was still healthy and "
"mid-flight; the disconnect tests below would prove nothing"
)
def test_abandoning_a_stream_runs_generator_cleanup_on_the_event_loop_thread():
"""The mechanism behind the ``join(timeout=5.0)`` freeze.
Starlette wraps a sync generator in ``iterate_in_threadpool``: each
``next()`` is dispatched to a worker thread, but closing the wrapper
throws ``GeneratorExit`` into the generator from whatever context runs
the ``aclose()`` -- the event loop. So the ``finally`` of every SSE route
in this app executes ON the loop, and anything blocking there blocks the
entire instance.
"""
control = _Gate()
loop_thread_ident: list[int] = []
async def abandon():
loop_thread_ident.append(threading.get_ident())
response = StreamingResponse(control.chunks(), media_type="text/plain")
await response.body_iterator.__anext__()
# Locked AnyIO reports `_next()` before its worker clears the call
# arguments. This isolated loop has no competing thread-pool work, so
# the no-op round-trip lets that worker rebind the stale arguments
# before `aclose()` drops the wrapper's reference. This is a test-only
# fence, not a model of production disconnect scheduling.
await anyio.to_thread.run_sync(lambda: None)
await response.body_iterator.aclose()
asyncio.run(abandon())
assert control.cleanup_entered.is_set(), (
"abandoning the stream did not run the generator's finally at all"
)
assert control.cleanup_thread_ident == loop_thread_ident[0], (
"the generator's finally ran on thread "
f"{control.cleanup_thread_ident} rather than the event loop's "
f"{loop_thread_ident[0]}. If this ever becomes true, the "
"'bounded drain' comments in rag.py's index_collection are "
"describing a constraint that no longer exists -- re-derive them "
"before relaxing any join timeout."
)
def test_a_blocking_cleanup_in_a_streaming_generator_freezes_the_loop():
"""What the freeze actually costs, demonstrated end to end.
This is the executable version of the bug report: park the generator's
``finally`` (as ``worker_thread.join(timeout=5.0)`` does while an
embedding batch is in flight) and the event loop cannot service anything
at all until it returns.
"""
control = _Gate(block_cleanup=True)
pinged = threading.Event()
with _background_event_loop() as loop:
async def abandon():
response = StreamingResponse(
control.chunks(), media_type="text/plain"
)
await response.body_iterator.__anext__()
# Use the locked-stack fence from the mechanism test so stale
# worker arguments cannot choose the cleanup thread.
await anyio.to_thread.run_sync(lambda: None)
await response.body_iterator.aclose()
pending = asyncio.run_coroutine_threadsafe(abandon(), loop)
assert control.cleanup_entered.wait(TIMEOUT), (
"the generator's finally never started; nothing to measure"
)
loop.call_soon_threadsafe(pinged.set)
stalled = not pinged.wait(STALL_PROBE_SECONDS)
control.cleanup_release.set()
recovered = pinged.wait(TIMEOUT)
pending.result(TIMEOUT)
assert stalled, (
"the event loop serviced a callback while a streaming generator's "
"finally was blocked -- cleanup is evidently no longer running on "
"the loop, so re-read "
"test_abandoning_a_stream_runs_generator_cleanup_on_the_event_loop_"
"thread before trusting either result"
)
assert recovered, (
f"the event loop never recovered within {TIMEOUT}s after the "
"generator's finally was released"
)
def test_the_disconnect_branch_under_test_is_the_one_uvicorn_uses():
"""Premise guard for the disconnect test below.
``StreamingResponse.__call__`` handles disconnects one of two completely
different ways depending on the ASGI ``spec_version`` the server
advertises. Pinning that uvicorn still says "2.3" keeps
``_asgi_scope`` faithful to production instead of exercising a branch
nothing serves.
"""
h11_source = (
Path(uvicorn.__file__).resolve().parent
/ "protocols"
/ "http"
/ "h11_impl.py"
)
assert h11_source.is_file(), f"uvicorn layout moved: {h11_source}"
assert '"spec_version": "2.3"' in h11_source.read_text(encoding="utf-8"), (
"uvicorn no longer advertises ASGI spec_version 2.3, so "
"StreamingResponse now takes its OTHER disconnect branch (the "
"2.4+ path, where an aborted send raises OSError). Update "
"_asgi_scope and re-verify the disconnect behaviour below."
)
def test_client_disconnect_abandons_the_stream_instead_of_draining_it():
"""A disconnected client must not keep the generator running.
The observable contract at the ASGI boundary: the response stops mid
flight -- no terminal ``more_body: False`` message is ever sent -- rather
than the server dutifully finishing a stream nobody is reading.
"""
control = _Gate()
async def disconnect_after_first_chunk():
response = StreamingResponse(control.chunks(), media_type="text/plain")
messages: list[dict] = []
disconnected = asyncio.Event()
async def receive():
await disconnected.wait()
return {"type": "http.disconnect"}
async def send(message):
messages.append(message)
if message["type"] == "http.response.body" and message.get("body"):
disconnected.set()
try:
await response(_asgi_scope("/stream"), receive, send)
finally:
control.release.set()
return messages
messages = asyncio.run(disconnect_after_first_chunk())
assert len(_starts(messages)) == 1, _starts(messages)
bodies = _bodies(messages)
assert bodies, "not a single body message was sent before the disconnect"
assert bodies[0]["body"] == b"chunk-1\n", bodies[0]
assert all(m.get("more_body") for m in bodies), (
"the response was carried through to its terminal "
"`more_body: False` message even though the client had "
f"disconnected: {[(m.get('body'), m.get('more_body')) for m in bodies]}"
)
def test_without_a_disconnect_the_same_stream_runs_to_completion():
"""Positive control for the test above.
Same generator, same harness, no ``http.disconnect`` -- so the absence of
the terminal message up there is attributable to the disconnect and not
to the harness never getting that far.
"""
control = _Gate()
control.release.set()
messages = asyncio.run(
_collect_response_messages(
StreamingResponse(control.chunks(), media_type="text/plain")
)
)
bodies = _bodies(messages)
assert [m["body"] for m in bodies[:2]] == [b"chunk-1\n", b"chunk-2\n"], (
f"the undisturbed stream did not deliver both chunks: "
f"{[m.get('body') for m in bodies]}"
)
assert not bodies[-1].get("more_body"), (
"an undisturbed stream did not end with a terminal "
f"`more_body: False` message: {bodies[-1]}"
)
# ---------------------------------------------------------------------------
# The residual budget in the real routers.
# ---------------------------------------------------------------------------
#
# `tests/web/routers/test_streaming_contracts.py` already fails on a join with
# NO timeout in a streaming generator's own scope. That is the unbounded case.
# This check is about the bounded one: given the test above -- the finally runs
# ON the event loop -- a `join(timeout=N)` there is a promise to freeze the
# whole instance for up to N seconds, so N is a budget and wants a ceiling of
# its own. Presence detection lives in the sibling; only the VALUE is read
# here.
def _thread_join_timeouts_in_generator_scopes(tree: ast.AST):
"""``(lineno, timeout)`` for each ``<name>.join(...)`` in a generator's
own scope, where ``<name>`` was bound from a ``Thread(...)`` call in that
same scope. ``timeout`` is ``None`` for a call with no timeout argument
and ``float('inf')`` for one whose timeout is not a literal number.
Nested ``def``/``lambda``/``class`` bodies are not searched: handing the
remaining join to a background daemon thread is the sanctioned shape, and
that thread is not the event loop.
"""
results: list[tuple[int, float | None]] = []
def _yields_directly(node) -> bool:
for child in ast.iter_child_nodes(node):
if isinstance(child, (ast.Yield, ast.YieldFrom)):
return True
if isinstance(
child,
(
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.Lambda,
ast.ClassDef,
),
):
continue
if _yields_directly(child):
return True
return False
def _is_thread_call(node) -> bool:
if not isinstance(node, ast.Call):
return False
func = node.func
name = (
func.id
if isinstance(func, ast.Name)
else getattr(func, "attr", None)
)
return name == "Thread"
def _timeout_of(call: ast.Call):
argument = None
if call.args:
argument = call.args[0]
for keyword in call.keywords:
if keyword.arg == "timeout":
argument = keyword.value
if argument is None:
return None
if isinstance(argument, ast.Constant) and isinstance(
argument.value, (int, float)
):
return float(argument.value)
return float("inf")
def _scan(node, thread_names):
for child in ast.iter_child_nodes(node):
if isinstance(
child,
(
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.Lambda,
ast.ClassDef,
),
):
continue
if (
isinstance(child, ast.Assign)
and len(child.targets) == 1
and isinstance(child.targets[0], ast.Name)
and _is_thread_call(child.value)
):
thread_names.add(child.targets[0].id)
if (
isinstance(child, ast.Call)
and isinstance(child.func, ast.Attribute)
and child.func.attr == "join"
and isinstance(child.func.value, ast.Name)
and child.func.value.id in thread_names
):
results.append((child.lineno, _timeout_of(child)))
_scan(child, thread_names)
for node in ast.walk(tree):
if isinstance(
node, (ast.FunctionDef, ast.AsyncFunctionDef)
) and _yields_directly(node):
_scan(node, set())
return results
def test_disconnect_drain_joins_stay_within_the_stall_budget():
found: list[tuple[str, int, float | None]] = []
for path in ROUTER_FILES:
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
for lineno, timeout in _thread_join_timeouts_in_generator_scopes(tree):
found.append((path.name, lineno, timeout))
assert found, (
"premise guard: no Thread.join() was found in any streaming "
"generator's own scope across "
f"{[p.name for p in ROUTER_FILES]} -- either the drain moved (good, "
"delete this test) or the scanner stopped matching (bad)"
)
over_budget = [
f" {name}:{lineno}: join timeout={timeout}"
for name, lineno, timeout in found
if timeout is None or timeout > EVENT_LOOP_STALL_BUDGET_SECONDS
]
assert not over_budget, (
"A streaming generator's own-scope Thread.join() exceeds the "
f"{EVENT_LOOP_STALL_BUDGET_SECONDS}s event-loop stall budget. That "
"finally runs ON the event loop (see "
"test_abandoning_a_stream_runs_generator_cleanup_on_the_event_loop_"
"thread), so the timeout is not a local wait -- it is how long every "
"other user of this instance is frozen when one client closes a tab:"
"\n" + "\n".join(over_budget)
)
class TestJoinTimeoutScannerSelfTest:
"""The scanner must tell the shapes apart, or the fence proves nothing."""
def test_reads_a_keyword_timeout(self):
tree = ast.parse(
"def generate():\n"
" worker = threading.Thread(target=work)\n"
" try:\n"
" yield 'x'\n"
" finally:\n"
" worker.join(timeout=5.0)\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == [(6, 5.0)]
def test_reads_a_positional_timeout(self):
tree = ast.parse(
"def generate():\n"
" worker = threading.Thread(target=work)\n"
" yield 'x'\n"
" worker.join(30)\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == [(4, 30.0)]
def test_reports_a_missing_timeout_as_none(self):
tree = ast.parse(
"def generate():\n"
" worker = threading.Thread(target=work)\n"
" yield 'x'\n"
" worker.join()\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == [(4, None)]
def test_ignores_a_join_deferred_to_a_nested_thread_body(self):
"""rag.py's shape: bounded join inline, unbounded join handed to a
daemon thread, which is not the event loop."""
tree = ast.parse(
"def generate():\n"
" worker = threading.Thread(target=work)\n"
" yield 'x'\n"
" lingering = worker\n"
" def _drain():\n"
" lingering.join()\n"
" threading.Thread(target=_drain, daemon=True).start()\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == []
def test_ignores_str_join(self):
tree = ast.parse(
"def generate():\n parts = ['a']\n yield ', '.join(parts)\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == []
def test_ignores_joins_outside_a_generator(self):
tree = ast.parse(
"def plain():\n"
" worker = threading.Thread(target=work)\n"
" worker.join()\n"
)
assert _thread_join_timeouts_in_generator_scopes(tree) == []
# ===========================================================================
# 3. SSE framing.
# ===========================================================================
#
# An SSE stream is a byte protocol, not a sequence of objects: a frame ends at
# the first blank line, and a lone "\n" in the middle of a `data:` payload
# ends the event early and turns the remainder into a field the browser
# silently discards. Nothing about that is caught by the header fences in
# test_sse_response_headers.py, and the routers build every frame with an
# f-string. `json.dumps` is what makes those safe -- it escapes newlines --
# so the property worth pinning is that every interpolation goes through it.
def _parse_sse(text: str):
"""Minimal event-stream parser (no id/retry/BOM handling).
``data:`` lines accumulate and are joined with "\\n"; a line starting
with ":" is a comment; a blank line dispatches the event; anything else
is a field name a browser ignores, recorded here as ``ignored`` because
that is exactly what a split frame looks like from the client side.
"""
events = []
data: list[str] = []
comments: list[str] = []
ignored: list[str] = []
for line in text.split("\n"):
if line == "":
if data or comments or ignored:
events.append(
{
"data": "\n".join(data),
"comments": list(comments),
"ignored": list(ignored),
}
)
data, comments, ignored = [], [], []
elif line.startswith(":"):
comments.append(line[1:].lstrip())
elif line.startswith("data:"):
data.append(line[len("data:") :].lstrip())
else:
ignored.append(line)
return events
def test_json_dumps_is_what_stops_a_newline_from_splitting_a_frame():
"""Executable proof of the premise the static fence below relies on."""
hostile = "first line\nsecond line"
safe = f"data: {json.dumps({'error': hostile})}\n\n"
events = _parse_sse(safe)
assert len(events) == 1, events
assert json.loads(events[0]["data"])["error"] == hostile
assert events[0]["ignored"] == []
# Negative control 1: the same value interpolated raw.
raw = f"data: {hostile}\n\n"
events = _parse_sse(raw)
assert len(events) == 1, events
assert events[0]["data"] == "first line", (
"a raw newline was expected to truncate the event's data at the "
f"newline; parser returned {events[0]['data']!r}"
)
assert events[0]["ignored"] == ["second line"], (
"the tail of a split payload should surface as an ignored field"
)
# Negative control 2: json.dumps DOES emit newlines when indented.
indented = f"data: {json.dumps({'error': hostile}, indent=2)}\n\n"
events = _parse_sse(indented)
assert len(events) == 1, events
with pytest.raises(json.JSONDecodeError):
json.loads(events[0]["data"])
#: ``(module, interpolated expression)`` -> why a non-``json.dumps``
#: interpolation cannot introduce a newline. Seeded only with cases read in
#: full.
NON_JSON_INTERPOLATION_ALLOWLIST: dict[tuple[str, str], str] = {
("rag.py", "total"): (
"index_collection's keep-alive comment frame, "
"`yield f': heartbeat {total}\\n\\n'`. `total` is `len(doc_info)` "
"(rag.py) -- an int, whose str() is digits only and can never "
"contain a newline."
),
}
def _frame_template(node):
"""``(template, holes)`` for a yielded string literal or f-string, with
every interpolation rendered as ``{}``; ``None`` for anything else."""
if isinstance(node, ast.Constant) and isinstance(node.value, str):
return node.value, []
if isinstance(node, ast.JoinedStr):
parts: list[str] = []
holes: list[ast.FormattedValue] = []
for value in node.values:
if isinstance(value, ast.Constant) and isinstance(value.value, str):
parts.append(value.value)
elif isinstance(value, ast.FormattedValue):
parts.append("{}")
holes.append(value)
else:
return None
return "".join(parts), holes
return None
def _own_scope_yields(func_node):
"""Every ``yield <value>`` belonging to ``func_node`` itself."""
found: list[ast.Yield] = []
def _walk(node):
for child in ast.iter_child_nodes(node):
if isinstance(
child,
(
ast.FunctionDef,
ast.AsyncFunctionDef,
ast.Lambda,
ast.ClassDef,
),
):
continue
if isinstance(child, ast.Yield) and child.value is not None:
found.append(child)
_walk(child)
_walk(func_node)
return found
def _is_json_dumps_call(node) -> bool:
if not isinstance(node, ast.Call):
return False
func = node.func
if isinstance(func, ast.Attribute):
return func.attr == "dumps" and (
isinstance(func.value, ast.Name) and func.value.id == "json"
)
return False
def _sse_frame_violations(path: Path):
"""``(lineno, message)`` for every malformed SSE frame yielded in
``path``.
A generator counts as an SSE generator when at least one of its own
yields is a literal starting with ``data:`` or ``:`` -- which excludes
research.py's NDJSON export (it yields ``"".join(lines)``) without
needing a hand-maintained list of endpoints.
"""
tree = ast.parse(path.read_text(encoding="utf-8"), filename=str(path))
violations: list[tuple[int, str]] = []
frame_count = 0
for node in ast.walk(tree):
if not isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
continue
yields = _own_scope_yields(node)
templates = [(y, _frame_template(y.value)) for y in yields]
looks_like_sse = any(
rendered is not None
and (rendered[0].startswith("data:") or rendered[0].startswith(":"))
for _, rendered in templates
)
if not looks_like_sse:
continue
for yield_node, rendered in templates:
frame_count += 1
if rendered is None:
violations.append(
(
yield_node.lineno,
"yields a non-literal value from an SSE generator; "
"frames must be built inline so they can be audited",
)
)
continue
template, holes = rendered
if not (template.startswith("data: ") or template.startswith(": ")):
violations.append(
(
yield_node.lineno,
f"frame {template!r} is neither a `data: ` field nor "
"a `: ` comment line",
)
)
if not template.endswith("\n\n"):
violations.append(
(
yield_node.lineno,
f"frame {template!r} is not terminated by a blank "
"line, so it never dispatches on the client",
)
)
if "\n" in template[:-2]:
violations.append(
(
yield_node.lineno,
f"frame {template!r} contains a newline before its "
"terminator, which splits it into two events",
)
)
for hole in holes:
expression = ast.unparse(hole.value)
if (path.name, expression) in NON_JSON_INTERPOLATION_ALLOWLIST:
continue
if not _is_json_dumps_call(hole.value):
violations.append(
(
yield_node.lineno,
f"interpolates {expression!r} directly; a "
"newline in that value splits the frame",
)
)
elif any(kw.arg == "indent" for kw in hole.value.keywords):
violations.append(
(
yield_node.lineno,
f"{expression!r} uses json.dumps(indent=...), "
"which emits newlines inside the payload",
)
)
return frame_count, violations
def test_every_sse_frame_in_the_routers_is_well_formed():
violations = []
for path in ROUTER_FILES:
_, found = _sse_frame_violations(path)
violations.extend(
f" {path.name}:{lineno}: {msg}" for lineno, msg in found
)
assert not violations, (
"Malformed SSE frame(s). A frame is `data: <one line>\\n\\n`: the "
"blank line dispatches the event and any newline before it ends the "
"event early, leaving the client to JSON.parse a truncated payload. "
"Wrap interpolated values in json.dumps(...) (no indent=), which "
"escapes newlines:\n" + "\n".join(violations)
)
def test_the_sse_frame_scan_found_the_known_endpoints():
"""Premise guard: an empty scan would make the fence above vacuous."""
counts = {}
for path in ROUTER_FILES:
frame_count, _ = _sse_frame_violations(path)
if frame_count:
counts[path.name] = frame_count
assert counts.get("library.py", 0) >= 8, counts
assert counts.get("rag.py", 0) >= 16, counts
assert sum(counts.values()) >= 24, counts
class TestSseFrameScannerSelfTest:
"""Negative controls: mutate the shape, the scanner must object."""
def _scan(self, tmp_path, source, name="rag.py"):
path = tmp_path / name
path.write_text(source, encoding="utf-8")
return _sse_frame_violations(path)
def test_accepts_the_shape_the_routers_actually_use(self, tmp_path):
count, violations = self._scan(
tmp_path,
"def generate():\n"
" yield f\"data: {json.dumps({'type': 'start'})}\\n\\n\"\n",
)
assert count == 1
assert violations == []
def test_flags_a_raw_interpolation(self, tmp_path):
_, violations = self._scan(
tmp_path,
'def generate():\n yield f"data: {error_message}\\n\\n"\n',
)
assert len(violations) == 1, violations
assert "directly" in violations[0][1]
def test_flags_an_indented_json_dumps(self, tmp_path):
_, violations = self._scan(
tmp_path,
"def generate():\n"
' yield f"data: {json.dumps(payload, indent=2)}\\n\\n"\n',
)
assert len(violations) == 1, violations
assert "indent" in violations[0][1]
def test_flags_a_missing_blank_line_terminator(self, tmp_path):
_, violations = self._scan(
tmp_path,
'def generate():\n yield f"data: {json.dumps(payload)}\\n"\n',
)
assert len(violations) == 1, violations
assert "terminated" in violations[0][1]
def test_flags_a_newline_inside_the_frame(self, tmp_path):
_, violations = self._scan(
tmp_path,
'def generate():\n yield f"data: a\\ndata: b\\n\\n"\n',
)
assert len(violations) == 1, violations
assert "splits it into two events" in violations[0][1]
def test_accepts_the_allowlisted_heartbeat(self, tmp_path):
count, violations = self._scan(
tmp_path,
'def generate():\n yield f": heartbeat {total}\\n\\n"\n',
)
assert count == 1
assert violations == []
def test_the_allowlist_is_scoped_to_its_module(self, tmp_path):
"""The same interpolation in another router is not pre-approved."""
_, violations = self._scan(
tmp_path,
'def generate():\n yield f": heartbeat {total}\\n\\n"\n',
name="library.py",
)
assert len(violations) == 1, violations
def test_ignores_the_ndjson_export_shape(self, tmp_path):
count, violations = self._scan(
tmp_path,
"def generate():\n lines = ['{}']\n yield ''.join(lines)\n",
name="research.py",
)
assert count == 0
assert violations == []
def test_sse_headers_and_framing_survive_the_full_middleware_stack(
stream_client,
):
"""The header fences prove the ROUTE sets them; this proves the STACK
keeps them.
``SecurityHeadersMiddleware`` combines its no-store policy with the
route's ``no-transform`` directive in one Cache-Control field. The
route's directive must survive so proxies cannot transform the stream.
"""
response = stream_client.get(f"{PROBE_PREFIX}/sse")
assert response.status_code == 200, response.text[:300]
assert response.headers["content-type"].startswith("text/event-stream")
assert response.headers.get("x-accel-buffering") == "no", (
"X-Accel-Buffering was lost between the route and the client; nginx "
"will buffer the whole stream"
)
cache_control = response.headers.get("cache-control", "")
assert "no-cache" in cache_control, cache_control
assert "no-transform" in cache_control, (
"the route's own Cache-Control was replaced rather than added to "
f"(got {cache_control!r})"
)
events = _parse_sse(response.text)
assert [json.loads(e["data"])["type"] for e in events] == [
"start",
"complete",
]
assert all(not e["ignored"] for e in events), events
# ===========================================================================
# 4. Fully materialised bytes belong in Response, not StreamingResponse.
# ===========================================================================
def test_the_binary_payload_is_line_rich_enough_to_expose_the_bug():
"""Premise guard: a payload with no 0x0A bytes proves nothing."""
assert BINARY_PAYLOAD.count(b"\n") >= 10, BINARY_PAYLOAD.count(b"\n")
assert b"\x00" in BINARY_PAYLOAD, "payload is not actually binary"
def test_materialised_bytes_are_one_body_message_with_a_content_length():
messages = asyncio.run(
_collect_response_messages(
Response(content=BINARY_PAYLOAD, media_type="application/pdf")
)
)
start = _starts(messages)[0]
bodies = _bodies(messages)
assert (
_header_map(start).get(b"content-length")
== str(len(BINARY_PAYLOAD)).encode()
), (
"a fully materialised payload must advertise its real "
f"Content-Length; headers were {_header_map(start)}"
)
assert len(bodies) == 1, (
f"a fully materialised payload took {len(bodies)} ASGI body sends"
)
assert bodies[0]["body"] == BINARY_PAYLOAD
def test_streamingresponse_over_bytesio_shreds_the_payload():
"""The historical bug, executable rather than asserted statically.
This is also the negative control for the test above: the AST guard in
test_migration_antipattern_guards.py bans the shape, and this shows what
the shape actually costs, so the ban cannot be dismissed as cargo cult.
"""
messages = asyncio.run(
_collect_response_messages(
StreamingResponse(
io.BytesIO(BINARY_PAYLOAD), media_type="application/pdf"
)
)
)
start = _starts(messages)[0]
bodies = _bodies(messages)
assert b"content-length" not in _header_map(start), (
"StreamingResponse unexpectedly produced a Content-Length; the "
"premise of the anti-pattern guard has changed"
)
# One send per line, plus the terminal empty body: iterating a BytesIO
# splits on every 0x0A byte.
assert len(bodies) == BINARY_PAYLOAD.count(b"\n") + 1, len(bodies)
assert len(bodies) > 1
assert b"".join(m.get("body", b"") for m in bodies) == BINARY_PAYLOAD
def test_a_binary_payload_round_trips_byte_identically_through_the_stack(
stream_client,
):
"""And the same payload survives the app's real middleware unchanged."""
response = stream_client.get(f"{PROBE_PREFIX}/binary")
assert response.status_code == 200
assert response.content == BINARY_PAYLOAD, (
f"payload came back {len(response.content)} bytes, sent "
f"{len(BINARY_PAYLOAD)}"
)
assert response.headers["content-length"] == str(len(BINARY_PAYLOAD))
assert response.headers["content-type"] == "application/pdf"
# ===========================================================================
# 5. An error raised after the headers are already on the wire.
# ===========================================================================
def test_an_error_mid_stream_cannot_change_the_status_the_client_saw():
"""Headers are sent with the first chunk, so the status is spent.
Recorded at the ASGI boundary (through a real Starlette app, so
``ServerErrorMiddleware`` is in the path) because that is what a server
sees and what any HTTP client is downstream of: one
``http.response.start`` with 200, the pre-error chunk delivered, no
terminal message, and the exception handed back to the server to abort
the connection with. There is no 500 anywhere -- the only signal the
client gets is a truncated body.
"""
async def endpoint(request):
def frames():
yield b'data: {"type": "progress"}\n\n'
raise _ProbeStreamFailure("probe: failed after the headers")
return StreamingResponse(frames(), media_type="text/event-stream")
async def drive():
bare = Starlette(routes=[Route("/fail", endpoint)])
messages: list[dict] = []
async def receive():
await asyncio.Event().wait()
async def send(message):
messages.append(message)
raised = None
try:
await bare(_asgi_scope("/fail"), receive, send)
except _ProbeStreamFailure as exc:
raised = exc
return messages, raised
messages, raised = asyncio.run(drive())
assert raised is not None, (
"the generator did not fail at all; the rest of this test would be "
"describing a healthy stream"
)
starts = _starts(messages)
assert len(starts) == 1, (
f"{len(starts)} http.response.start messages -- sending a second one "
"to report the failure is an ASGI protocol violation"
)
assert starts[0]["status"] == 200, starts[0]["status"]
bodies = _bodies(messages)
assert [m["body"] for m in bodies] == [b'data: {"type": "progress"}\n\n']
assert all(m.get("more_body") for m in bodies), (
"a failed stream was closed off with a terminal `more_body: False`, "
"which tells the client the truncated body was complete"
)
def test_the_real_app_answers_200_for_a_stream_that_fails_after_the_headers(
app, stream_client
):
"""Same thing through the whole app, and the server does not hang.
The strict client is the anti-tautology: without it a route that never
raised would satisfy the 200 assertion just as well.
"""
response = stream_client.get(f"{PROBE_PREFIX}/fails-mid-stream")
assert response.status_code == 200, (
"a generator that raises after the headers are sent must not be able "
f"to produce a 500 -- got {response.status_code}"
)
assert response.headers["content-type"].startswith("text/event-stream")
strict = TestClient(app)
with pytest.raises(_ProbeStreamFailure):
strict.get(f"{PROBE_PREFIX}/fails-mid-stream")