Files
paperless-ngx/src/paperless/tests/test_network.py
T
Trenton H 2d83770098 Fix: Stream AI chat natively async so a disconnect cancels the LLM request (#14850)
* Fix: stream AI chat natively async so a disconnect cancels the LLM request

The chat answer was a sync generator wrapped for ASGI, pulling each chunk
through sync_to_async(next). A client disconnect cancelled the response
task, but the worker thread waiting on the LLM could not be interrupted,
so the upstream request kept generating the whole answer for nobody.

Make stream_chat_with_documents an async generator. All the blocking setup
(ORM queries, the index read lock and its thread-bound sqlite connection,
the query embedding, building the LLM client) moves into one sync
_prepare_chat called once through sync_to_async, which also hands the DB
connection back before the answer streams. The answer then streams through
llama-index's asynthesize over the pinned async httpx clients, so a
disconnect cancels the LLM request directly. The view returns the async
generator as is, replacing the sync-to-async wrapper.

The async pinned transport resolved hostnames with a blocking getaddrinfo
on the event loop, which would stall the whole worker on slow DNS now that
chat uses it in the web server. Resolve in a thread instead, and add the
async counterparts of the existing transport tests.

* Ensures all the resources are closed even in odd cases and not just by gc
2026-11-25 09:54:50 -08:00

151 lines
4.6 KiB
Python

from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
from unittest import mock
import httpx
import pytest
from paperless.network import PinnedHostAsyncHTTPTransport
from paperless.network import PinnedHostHTTPTransport
from paperless.network import is_public_ip
if TYPE_CHECKING:
from pytest_mock import MockerFixture
def test_pinned_host_transport_blocks_internal_rebinding():
transport = PinnedHostHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "http://example.com/test")
with (
mock.patch(
"paperless.network.resolve_hostname_ips",
return_value=["127.0.0.1"],
),
pytest.raises(httpx.ConnectError, match="non-public address"),
):
transport.handle_request(request)
def test_pinned_host_transport_rewrites_to_vetted_ip():
transport = PinnedHostHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "https://example.com:8443/test")
def assert_rewritten_request(
self,
rewritten_request,
):
assert str(rewritten_request.url) == "https://93.184.216.34:8443/test"
assert rewritten_request.headers["Host"] == "example.com:8443"
assert rewritten_request.extensions["sni_hostname"] == "example.com"
return httpx.Response(200, request=rewritten_request)
with (
mock.patch(
"paperless.network.resolve_hostname_ips",
return_value=["93.184.216.34"],
),
mock.patch.object(
httpx.HTTPTransport,
"handle_request",
autospec=True,
side_effect=assert_rewritten_request,
),
):
response = transport.handle_request(request)
assert response.status_code == 200
class TestPinnedHostAsyncHTTPTransport:
def test_blocks_internal_rebinding(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a loopback address
WHEN:
- The async transport sends a request to it
THEN:
- The request is refused as a non-public address
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["127.0.0.1"],
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "http://example.com/test")
with pytest.raises(httpx.ConnectError, match="non-public address"):
asyncio.run(transport.handle_async_request(request))
def test_rewrites_to_vetted_ip(self, mocker: MockerFixture) -> None:
"""
GIVEN:
- A hostname that resolves to a public address
WHEN:
- The async transport sends a request to it
THEN:
- The connection goes to that address, keeping the original Host
header and TLS SNI hostname
"""
mocker.patch(
"paperless.network.resolve_hostname_ips",
return_value=["93.184.216.34"],
)
sent = mocker.patch.object(
httpx.AsyncHTTPTransport,
"handle_async_request",
autospec=True,
side_effect=lambda _self, req: httpx.Response(200, request=req),
)
transport = PinnedHostAsyncHTTPTransport(allow_internal=False)
request = httpx.Request("GET", "https://example.com:8443/test")
response = asyncio.run(transport.handle_async_request(request))
assert response.status_code == 200
rewritten = sent.call_args.args[1]
assert str(rewritten.url) == "https://93.184.216.34:8443/test"
assert rewritten.headers["Host"] == "example.com:8443"
assert rewritten.extensions["sni_hostname"] == "example.com"
@pytest.mark.parametrize(
"address",
[
"127.0.0.1",
"10.0.0.5",
"169.254.169.254",
"::1",
"fc00::1",
"fe80::1",
# RFC 6598 shared address space, incl. both edges of the /10
"100.64.0.0",
"100.64.0.1",
"100.127.255.255",
# RFC 6052 NAT64 well-known prefix, embedding 127.0.0.1 and 10.0.0.5
"64:ff9b::7f00:1",
"64:ff9b::a00:5",
],
)
def test_is_public_ip_blocks_non_public_addresses(address):
assert not is_public_ip(address)
@pytest.mark.parametrize(
"address",
[
"8.8.8.8",
"142.250.185.196",
"2606:4700:4700::1111",
# just outside the ranges above, must stay reachable
"100.63.255.255",
"100.128.0.0",
"64:ff9a::1",
"64:ff9c::1",
],
)
def test_is_public_ip_allows_public_addresses(address):
assert is_public_ip(address)