From 3e467cacf2b08164d1aabff231be6607434bb8b3 Mon Sep 17 00:00:00 2001 From: Laurent Paoletti Date: Mon, 26 Jan 2026 16:00:06 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B(back)=20fix=20keepalives=20not=20s?= =?UTF-8?q?ent=20during=20document=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wrap document_store.parse_and_store_document() calls with asyncio.to_thread() to prevent blocking the event loop. Previously, synchronous document parsing (e.g., PDF) blocked the event loop, preventing keepalive messages from being sent and causing nginx timeouts in production Signed-off-by: Laurent Paoletti --- src/backend/chat/clients/pydantic_ai.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/backend/chat/clients/pydantic_ai.py b/src/backend/chat/clients/pydantic_ai.py index 71ac2dd..dfab669 100644 --- a/src/backend/chat/clients/pydantic_ai.py +++ b/src/backend/chat/clients/pydantic_ai.py @@ -6,6 +6,7 @@ implementation while keeping the *exact* same public API so that no changes are needed in views.py or tests. """ +import asyncio import dataclasses import functools import json @@ -273,7 +274,9 @@ class AIAgentService: # pylint: disable=too-many-instance-attributes # Retrieve the document data with default_storage.open(key, "rb") as file: document_data = file.read() - parsed_content = document_store.parse_and_store_document( + # Run in thread to avoid blocking the event loop during parsing + parsed_content = await asyncio.to_thread( + document_store.parse_and_store_document, name=document.identifier, content_type=document.media_type, content=document_data, @@ -282,7 +285,9 @@ class AIAgentService: # pylint: disable=too-many-instance-attributes # Remote URL raise ValueError("External document URL are not accepted yet.") else: - parsed_content = document_store.parse_and_store_document( + # Run in thread to avoid blocking the event loop during parsing + parsed_content = await asyncio.to_thread( + document_store.parse_and_store_document, name=document.identifier, content_type=document.media_type, content=document.data,