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/memory): fail open when backend init errors instead of 500ing the request (#3257)

Abhay Singh3eba2d606647
diff --git a/headroom/proxy/memory_handler.py b/headroom/proxy/memory_handler.py
index 10917396d..94dd46d87 100644
--- a/headroom/proxy/memory_handler.py
+++ b/headroom/proxy/memory_handler.py
@@ -316,6 +316,27 @@ class MemoryHandler:
self._initialized = False
logger.info(f"Memory: backend initialization cancelled (backend={self.config.backend})")
raise
+ except Exception as exc:
+ # Fail-open for ANY init failure, not just timeout. Memory is an
+ # optional subsystem: a backend that cannot open (e.g. a SQLite
+ # ``unable to open database file`` on a Docker Desktop macOS
+ # bind-mount, issue #3251) must NOT propagate and 500 the whole
+ # request — the docstring's fail-open contract has to hold here too.
+ # Null the possibly-half-assigned backend (same reasoning as the
+ # timeout branch) and leave ``_initialized=False`` so a later
+ # request can retry once the environment recovers.
+ existing_backend = self._backend
+ if existing_backend is not None:
+ await self._close_backend_instance(existing_backend, reason="init_error")
+ self._backend = None
+ self._initialized = False
+ logger.error(
+ "Memory: backend initialization failed (backend=%s); "
+ "serving requests without memory context. Subsequent requests will retry: %s",
+ self.config.backend,
+ exc,
+ )
+ return
async def _init_backend_locked(self) -> None:
"""Actual backend-init body. Must be called with ``_init_lock`` held."""
diff --git a/tests/test_memory_handler_concurrent_init.py b/tests/test_memory_handler_concurrent_init.py
index 418f6b83e..64ed04548 100644
--- a/tests/test_memory_handler_concurrent_init.py
+++ b/tests/test_memory_handler_concurrent_init.py
@@ -100,6 +100,53 @@ async def test_ensure_initialized_noop_when_disabled(tmp_path):
assert handler._backend is None
+async def test_ensure_initialized_fails_open_on_backend_init_error(tmp_path, monkeypatch):
+ """A backend that cannot open must NOT propagate — memory is optional.
+
+ Regression (#3251): a SQLite ``unable to open database file`` on a Docker
+ Desktop macOS bind-mount escaped ``_ensure_initialized`` (which only caught
+ TimeoutError/CancelledError) and 500'd every request. It must fail open: log,
+ leave ``_initialized=False`` and ``_backend=None``, and let the request
+ proceed without memory.
+ """
+ import sqlite3
+
+ closed = {"n": 0}
+
+ class BrokenLocalBackend:
+ def __init__(self, config):
+ self.config = config
+
+ async def _ensure_initialized(self) -> None:
+ # Mirrors the real failure: sqlite3.connect raising mid-init after
+ # the backend object has already been assigned to self._backend.
+ raise sqlite3.OperationalError("unable to open database file")
+
+ async def close(self) -> None:
+ closed["n"] += 1
+
+ import headroom.memory.backends.local as local_mod
+
+ monkeypatch.setattr(local_mod, "LocalBackend", BrokenLocalBackend)
+
+ handler = MemoryHandler(
+ MemoryConfig(enabled=True, backend="local", db_path=str(tmp_path / "mem.db"))
+ )
+
+ # Must not raise — the whole point.
+ await handler._ensure_initialized()
+
+ assert handler._initialized is False
+ assert handler._backend is None
+ # The half-assigned backend was cleaned up.
+ assert closed["n"] == 1
+
+ # A later call retries (and fails open again) rather than short-circuiting.
+ await handler._ensure_initialized()
+ assert handler._initialized is False
+
+
# -------------------------------------------------------------------
# Timeout fail-open
# -------------------------------------------------------------------