mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-24 22:27:43 +00:00
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.