Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

docs(embeddings): correct import lock narrative and document parent-first warm-up invariant (#5691)

S63f1c5c72c0a
diff --git a/changelog.d/5691.bugfix.md b/changelog.d/5691.bugfix.md
new file mode 100644
index 0000000000..45bd3b368c
--- /dev/null
+++ b/changelog.d/5691.bugfix.md
@@ -0,0 +1,4 @@
+Corrected the concurrency narrative and lock documentation for lazy
+text-splitter initialization to explain the parent-first import
+invariant that prevents CPython `_DeadlockError` during concurrent cold
+starts, and documented `BoundedSemanticChunker`'s warm-up dependency.
diff --git a/src/local_deep_research/embeddings/splitters/bounded_semantic_chunker.py b/src/local_deep_research/embeddings/splitters/bounded_semantic_chunker.py
index c6534c2466..1ac6fcd8bc 100644
--- a/src/local_deep_research/embeddings/splitters/bounded_semantic_chunker.py
+++ b/src/local_deep_research/embeddings/splitters/bounded_semantic_chunker.py
@@ -31,7 +31,17 @@ class BoundedSemanticChunker(BaseDocumentTransformer):
chunk_size: int,
separators: list[str] | None = None,
) -> None:
- """Create recursive pre- and post-semantic bounding stages."""
+ """Create recursive pre- and post-semantic bounding stages.
+
+ Note:
+ This constructor performs a submodule import from
+ ``langchain_text_splitters.character``. Callers (such as
+ ``get_text_splitter``) must ensure the parent package
+ ``langchain_text_splitters`` has already been warmed up
+ parent-first (under ``_LANGCHAIN_TEXT_SPLITTERS_IMPORT_LOCK``)
+ to prevent CPython ``_DeadlockError`` from submodule-first import
+ order inversion during concurrent cold starts.
+ """
from langchain_text_splitters.character import (
RecursiveCharacterTextSplitter,
)
diff --git a/src/local_deep_research/embeddings/splitters/text_splitter_registry.py b/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
index 46d063e19d..8c530d8763 100644
--- a/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
+++ b/src/local_deep_research/embeddings/splitters/text_splitter_registry.py
@@ -26,17 +26,18 @@ from loguru import logger
# it (the original intent of issue #4490, which the concrete-submodule
# imports did not actually achieve since they still run the package init).
-# Serializes the cold ``langchain_text_splitters`` import. Its package
-# ``__init__`` eagerly imports ~14 splitter submodules with enough internal
-# cross-referencing that importing different submodules from multiple threads
-# at once observes a partially-initialized package and raises
-# ``ImportError: cannot import name ... from partially initialized module``.
-# The module-level import this replaced warmed ``sys.modules`` single-threaded
-# at boot; deferring it to call time reintroduced the race for the RAG
-# auto-index ``ThreadPoolExecutor`` and the per-user document scheduler, which
-# call ``get_text_splitter`` from several threads. Warm the package once under
-# this lock before the function-local submodule imports; once warm the lock is
-# uncontended (a ``sys.modules`` hit).
+# Serializes the cold ``langchain_text_splitters`` import. In CPython,
+# concurrent submodule-first imports (e.g. two threads importing different
+# concrete submodules like ``langchain_text_splitters.character`` and
+# ``langchain_text_splitters.base`` before the parent package is initialized)
+# cause import lock order inversion, raising ``_DeadlockError`` or deadlocking
+# during package initialization.
+#
+# The load-bearing fix is the parent-first import invariant: warming the
+# parent package (``import langchain_text_splitters``) once under this lock
+# executes package ``__init__`` and populates ``sys.modules`` before any
+# thread performs submodule-level imports. Once warm, subsequent submodule
+# imports are fast dictionary lookups and the lock is uncontended.
_LANGCHAIN_TEXT_SPLITTERS_IMPORT_LOCK = threading.Lock()
if TYPE_CHECKING:
@@ -99,11 +100,11 @@ def get_text_splitter(
# importing the package eagerly pulls torch/sentence-transformers and
# must stay off the app-startup import path.
#
- # Recursive, token, and sentence branches import from
- # ``langchain_text_splitters`` here. Semantic warms it below only after
- # embeddings and experimental-dependency validation, before constructing
- # its bounded recursive stages. Warm the package under the lock so
- # concurrent first-callers cannot observe a half-initialized package.
+ # Enforce the parent-first import invariant: warm the parent package
+ # under the lock before any submodule-level imports occur. Non-semantic
+ # branches warm it here; the semantic branch warms it below only after
+ # embeddings and dependency validation, before constructing its
+ # BoundedSemanticChunker (which imports langchain_text_splitters.character).
if splitter_type != "semantic":
with _LANGCHAIN_TEXT_SPLITTERS_IMPORT_LOCK:
import langchain_text_splitters # noqa: F401
@@ -179,6 +180,10 @@ def get_text_splitter(
"Install it with: pip install langchain-experimental"
) from e
+ # Enforce the parent-first import invariant before constructing
+ # BoundedSemanticChunker: BoundedSemanticChunker.__init__ imports
+ # langchain_text_splitters.character. Warming the parent package here
+ # ensures sys.modules is populated and prevents CPython _DeadlockError.
with _LANGCHAIN_TEXT_SPLITTERS_IMPORT_LOCK:
import langchain_text_splitters # noqa: F401