Skip to content

Run the hashing embedder off the event loop - #7

Merged
sinnercode228 merged 1 commit into
mainfrom
fix/hashing-embedder-thread
Oct 27, 2026
Merged

sinnercode228 merged 1 commit into
mainfrom
fix/hashing-embedder-thread

Conversation

@sinnercode228

Copy link
Copy Markdown
Owner

What was wrong: HashingEmbedder.embed_documents in backend/src/docmind/embeddings/hashing.py:61 was async def but its body was a plain list comprehension over the sync self.embed(text). Hashing a document with hundreds of chunks kept the asyncio loop busy for the whole embed. The ingest pipeline in backend/src/docmind/ingestion/pipeline.py:106-113 runs extract in a worker thread and awaits embed_documents right after it, so the API worker was still blocked on the CPU-bound half.

The fix: dispatch the inner loop to a worker thread with asyncio.to_thread, same as extraction. embed_query is a single call and stays on the loop.

How it was tested: new test_embed_documents_yields_to_the_event_loop in backend/tests/test_retrieval.py; a concurrent ticker that await asyncio.sleep(0) in a loop had to advance more than five times while embed_documents ran on 40 long inputs. It stayed at 0 before the fix and reaches hundreds after. Locally ran what CI runs: ruff check, ruff format --check, mypy src, pytest --cov (116 passed), and on the frontend npm ci, lint, typecheck, test (34 passed), build:pages.

Fixes #6

HashingEmbedder.embed_documents was async but its body was a plain list
comprehension over the sync self.embed(text), so a document with hundreds
of chunks kept the asyncio loop busy for the whole embed. The ingest
pipeline runs extract in a thread and awaits embed_documents right after,
so the API worker was blocked on the CPU-bound half.

Dispatch the inner loop to a worker thread the same way extraction does.
embed_query is a single call and stays on the loop.

Fixes #6
@sinnercode228
sinnercode228 merged commit e2b23a1 into main Oct 27, 2026
3 checks passed
@sinnercode228
sinnercode228 deleted the fix/hashing-embedder-thread branch October 27, 2026 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hashing embedder blocks the event loop while embedding chunks

1 participant