diff --git a/document-parser/infra/local_converter.py b/document-parser/infra/local_converter.py index b469de1..763b90d 100644 --- a/document-parser/infra/local_converter.py +++ b/document-parser/infra/local_converter.py @@ -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)