#2604·mempalace

HTTP transport holds _HTTP_REQUEST_LOCK across a tool's embedding call, so embedding latency stalls unrelated requests

Author: wstczywCreated Sep 26, 2026Updated Sep 26, 2026

What happened?

On the HTTP transport, _http_dispatch (mempalace/mcp_server/http.py) holds _HTTP_REQUEST_LOCK (the shared read lease or the exclusive lease, per classify_tool()) for a tool's entire dispatch. That window includes the tool's local embedding inference (mempalace/backends/embedding_wrapper.py::_embed_texts), which never touches the palace store.

For a write-classified tool (mempalace_diary_write, mempalace_add_drawer, ...) the exclusive lease is held through the whole embedding call, so every other request, including backend-independent ones like mempalace_status, waits for as long as inference takes.

This is related to #1984 / PR #1985 (same lock) but on a different axis: those are about whether reads should share the lock with other reads, which current develop already does (case 1 below). This report is about the lock covering a sub-step that does not need it. A slow write's embedding phase blocks everyone under either design.

What did you expect?

Embedding inference should not extend how long unrelated requests wait. The lease is needed for the backend query or write before or after the model call, not for the model call itself.

How to reproduce:

Against vanilla develop @ 8c4865f, with a real loopback server from mempalace.mcp_server._build_http_server on an ephemeral port (the same setup as the existing TestPalaceReadsDoNotStarveTheHub tests). Each tool's slow phase is simulated with time.sleep(1.2) in its handler. The lock does not care why a dispatch is slow, so this measures the lock's scope, not embedding speed.

  1. mempalace_search (read) in flight, concurrent mempalace_status (read): under 0.05 s, not blocked. Reads already share the lease.
  2. mempalace_diary_write (write) in flight, concurrent mempalace_status (read): 1.22 s, blocked for the full slow phase.

The mirror case exists too: a write waits behind an in-flight read's embedding phase. test_state_changes_wait_for_in_flight_search asserts that this wait is intended, and it is: a writer must not run concurrently with a read's backend query. The point here is only that the wait is lengthened by an embedding call that needs no lease.

Root cause

_http_dispatch wraps handle_request() in _HTTP_REQUEST_LOCK.read_lock() or the exclusive lock for the tool's whole duration, and _embed_texts runs inside that window.

One possible fix (asking which direction you would prefer)

We have a local patch, with 8 regression tests, that:

  • adds a hook seam in embedding_wrapper.py (set_embedding_section_hook / _embedding_section) so the HTTP transport can release the lease only around the embedding sub-window; CLI and stdio callers leave it unset and are unaffected;
  • in http.py, releases the lease only for mempalace_search, mempalace_check_duplicate and mempalace_diary_write, each of which makes one request-local embedding call before an otherwise independent backend operation; compound read-modify-write tools (mempalace_mine, mempalace_update_drawer, ...) stay fully serialized, and thread-local depth tracking keeps a nested call from double-releasing;
  • adds _HTTP_EMBEDDING_LIFECYCLE_LOCK so mempalace_reconnect cannot close backend handles while another request has released the lease but is still mid-embedding.

That fix costs real complexity (depth counters, a second lock). A simpler alternative is to move the embedding call outside the lease for read-classified tools only and accept that mempalace_reconnect may need to retry. Which would you prefer, or something else? Happy to open a PR for the chosen direction. (The same local patch also serves server/discover without the lock; that part is already proposed separately in #2580.)

Environment:

  • OS: Windows 11
  • Python version: 3.12
  • MemPal version: develop @ 8c4865f