Rust Tantivy + Python PyO3 Hybrid Search for Local MCP Servers: How to release the GIL during multi-megabyte BM25 index commits?
To give our local code-search MCP server instant BM25 lexical search without requiring Docker or Elasticsearch, we wrapped Rust's tantivy search library inside Python using PyO3 (tantivy-py / custom wheel).
The Event-Loop Stall
Queries (searcher.search(...)) finish in 2–4ms.
However, whenever a background file watcher indexes 500 modified repository files and calls:
pythonindex_writer.commit()
- Tantivy spawns background Rust segment-merge threads and fsyncs ~50MB to disk (~650ms).
- During that 650ms
commit()call, our Python FastMCPasyncioevent loop completely freezes—even when wrapped inasyncio.to_thread()!
Why is asyncio.to_thread() still freezing the main thread on our PyO3 wrapper, and how do we properly release the Python GIL in Rust (py.allow_threads) + batch commits?