memorix icon indicating copy to clipboard operation
memorix copied to clipboard

Embedding transport failures (fetch throws) are not retried — stale keep-alive socket reuse marks provider unavailable

Open chenshuangci opened this issue 3 weeks ago • 0 comments

Problem

fetchWithRetry in src/embedding/api-provider.ts only retries HTTP-status failures (429 / 5xx). Transport-level failures — fetch() throwing before any response arrives, e.g. TypeError: fetch failed — propagate straight up and mark the embedding provider unavailable.

This bites anyone self-hosting an OpenAI-compatible embedding server: those servers close idle keep-alive connections after a few seconds (llama-server / cpp-httplib: ~5s hardcoded; common proxies similar), and Node's undici pool occasionally hands out a socket the server just closed. The failure is instantaneous and happens while the server is healthy and idle — pure stale-connection reuse, not load.

Repro against llama-server --embedding:

import http.client, json, time
conn = http.client.HTTPConnection("127.0.0.1", 11434)
def post():
    conn.request("POST", "/v1/embeddings", body=json.dumps({"input": ["hello"], "model": "x"}), headers={"Content-Type": "application/json"})
    r = conn.getresponse(); r.read(); print(r.status)
post()      # 200
time.sleep(7)
post()      # BrokenPipeError — server closed the idle keep-alive socket

Impact observed in production

memorix 1.10.3, serve-http mode, embedding via local llama-server (qwen3-embedding-8b). Every transport failure marks the provider unavailable, and the next memorix_store then stalls 43–90s in the formation resolve stage while the provider recovery loop runs, blowing past MCP client timeouts (we see clusters of Formation timed out ... at stage resolve in the journal, each preceded by Embedding provider unavailable at runtime: fetch failed, with the embedding server idle and responsive at every failure timestamp). The formation-budget stall is a separate problem — filing another issue for it.

Suggested fix

Retry transport-level failures on the same backoff ladder already used for 429/5xx (MAX_RETRIES / BASE_DELAY_MS): catch in the first stage, before response-status handling. The retry opens a fresh connection; the request body is re-serialized from the plain request object, and the embeddings endpoint is pure compute (idempotent), so retries are safe. Existing retry: false opt-out still honored.

We carry this patch locally (two-case test addition, suite green) and are happy to open a PR if the approach sounds good.

chenshuangci avatar Nov 11 '26 21:11 chenshuangci