fix: replace _converter_lock with timeout-based acquisition (C2)

A frozen conversion holding the lock indefinitely blocks all subsequent
jobs. Using lock.acquire(timeout=300) fails fast with a clear error
instead of waiting forever.

Ref #57 (C2)
This commit is contained in:
Pier-Jean Malandrino 2026-04-07 14:38:17 +02:00
parent 4c76101142
commit c0fb128718

View file

@ -46,8 +46,10 @@ from infra.settings import settings
logger = logging.getLogger(__name__)
# Thread lock — DoclingConverter is not thread-safe
# Thread lock — DoclingConverter is not thread-safe.
# Uses a timeout to prevent a frozen conversion from blocking all others.
_converter_lock = threading.Lock()
_LOCK_TIMEOUT = 300 # seconds — fail fast rather than wait forever
# US Letter page dimensions (points) — fallback when page size is unknown
_DEFAULT_PAGE_WIDTH = 612.0
@ -212,9 +214,17 @@ def _process_content_item(
def _convert_sync(file_path: str, options: ConversionOptions) -> ConversionResult:
with _converter_lock:
acquired = _converter_lock.acquire(timeout=_LOCK_TIMEOUT)
if not acquired:
raise TimeoutError(
f"Could not acquire converter lock within {_LOCK_TIMEOUT}s — "
"a previous conversion may be frozen"
)
try:
conv = _select_converter(options)
result = conv.convert(file_path)
finally:
_converter_lock.release()
doc = result.document
page_count = len(doc.pages)