Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

fix(proxy): reset memory backend to None on init timeout

Adryan Eka Vandra3f1f82d7e27d
diff --git a/headroom/proxy/memory_handler.py b/headroom/proxy/memory_handler.py
index f42e038d5..75770d3f9 100644
--- a/headroom/proxy/memory_handler.py
+++ b/headroom/proxy/memory_handler.py
@@ -185,6 +185,12 @@ class MemoryHandler:
await asyncio.wait_for(_do_init(), timeout=STARTUP_INIT_TIMEOUT_SECONDS)
except asyncio.TimeoutError:
# Fail-open: leave _initialized=False so subsequent calls retry.
+ # CRITICAL: also null the backend — _init_backend_locked may have
+ # already assigned ``self._backend`` before its own await raised /
+ # was cancelled by wait_for. Callers that do
+ # ``if self.memory_handler._backend:`` must not see a
+ # truthy-but-broken backend.
+ self._backend = None
logger.error(
"Memory: backend initialization timed out after "
f"{STARTUP_INIT_TIMEOUT_SECONDS}s "
diff --git a/tests/test_memory_handler_concurrent_init.py b/tests/test_memory_handler_concurrent_init.py
index 3c22124de..6a7424b4d 100644
--- a/tests/test_memory_handler_concurrent_init.py
+++ b/tests/test_memory_handler_concurrent_init.py
@@ -149,6 +149,47 @@ async def test_ensure_initialized_timeout_leaves_handler_unready(
assert STARTUP_INIT_TIMEOUT_SECONDS == 30.0
+async def test_ensure_initialized_timeout_nulls_partially_initialized_backend(
+ tmp_path, monkeypatch
+):
+ """If wait_for fires while _init_backend_locked has already set
+ ``self._backend`` but before ``self._initialized = True``, the timeout
+ handler must null ``_backend``. Otherwise callers doing
+ ``if self.memory_handler._backend:`` see a truthy-but-broken backend.
+ """
+
+ class SlowBackend:
+ def __init__(self, config):
+ self.config = config
+
+ async def _ensure_initialized(self) -> None:
+ # Hang long enough to blow the 0.01s timeout below.
+ await asyncio.sleep(5.0)
+
+ async def close(self) -> None:
+ pass
+
+ import headroom.memory.backends.local as local_mod
+
+ monkeypatch.setattr(local_mod, "LocalBackend", SlowBackend)
+
+ handler = MemoryHandler(
+ MemoryConfig(
+ enabled=True, backend="local", db_path=str(tmp_path / "mem.db")
+ )
+ )
+
+ with patch(
+ "headroom.proxy.memory_handler.STARTUP_INIT_TIMEOUT_SECONDS", 0.01
+ ):
+ await handler._ensure_initialized()
+
+ # Both must be consistent after timeout.
+ assert handler._initialized is False
+ assert handler._backend is None
+
+
# -------------------------------------------------------------------
# Real backend init (no monkeypatching) — integration smoke test
# -------------------------------------------------------------------