🐛(back) fix keepalives not sent during document parsing

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 <lp@providenz.fr>
This commit is contained in:
Laurent Paoletti
2026-01-26 16:00:06 +01:00
parent 120b204729
commit 3e467cacf2
+7 -2
View File
@@ -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,