/healthz runs the full status() scan in the event loop and fails its 1 s probe on a large mirror #90
Labels
No labels
bug
enhancement
epic
needs-info
needs-triage
ready-for-agent
ready-for-human
svd:go
svd:hold
wontfix
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
dev/mail-mind-mcp#90
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
/healthzis the liveness and readiness probe in the k8s base, with the default 1 s timeout. It calls_current_lag(), which callsstore.status(). That method runs seven queries under the store lock, synchronously, inside the event loop:messages LEFT JOIN embeddings GROUP BY account, a full scan.attachment_text JOIN messages, new in 0.6.0.messages WHERE has_attachments = 1 AND NOT EXISTS (attachment_text …), new in 0.6.0.mailboxes,sync_logandchanges.The probe needs only
last_ok_sync, which is onesync_logaggregate.Evidence (2026-11-01)
/healthztook more than 60 s, measured in-pod. Liveness killed the api 31 times on 0.5.0 over about 5 h, and kept killing it on 0.6.0. The instance served nothing during that time./healthztakes about 0.55 s, already more than half the probe timeout.synccontainer is at about 1 CPU embedding. One 32-message embed batch took 44 s, where 6.5 s is normal.The estate works around this with TCP probes on the large instance. The small instance keeps the HTTP probes.
Asks
/healthzreads only what it reports: oneSELECT account, MAX(finished) FROM sync_log WHERE error IS NULL GROUP BY account, or an in-memory value. It must not callstatus().status()work off the event loop, for example withanyio.to_thread, so a slowmail_statuscannot stall other requests on the same api process.status(), somail_statusstays cheap as a mirror grows.Acceptance
/healthzreturns in under 100 ms on a 2 GB mirror while a backfill writes./healthzdoes not touchmessages,embeddingsorattachment_text.Agent Brief
Category: bug
Summary:
/healthzand/healthz/syncread only the last successful sync per account, off the event loop, and never callStore.status().Scope after the split (operator, 2026-11-01). This issue holds asks 1 and 2. Ask 3, the WAL growth, is #91. Ask 4 is out of scope here; see below.
Verified at triage:
healthzandhealthz_syncareasync defand call_current_lag(), which calls the synchronousStore.status()(server.py:951-967). That runs seven queries under the reader's oneRLock(store.py:422,store.py:1609-1640), inside the event loop.status()took a median of 239 ms. Thesync_logaggregate alone took under 0.1 ms.fastmcp/tools/function_tool.py:457). Somail_statusdoes not block the event loop. It does hold the store lock, so other tools' reads wait behind it. Ask 2 in the body reads as: keep the health routes off the loop and offstatus().Already decided (keep it):
/healthzreports the lag and does not judge it. A stale mirror is still worth reading (server.py:958-959).server.py:926). The response bodies stay exactly as today:ok lag_seconds=N, and for/healthz/sync,ok|stale lag_seconds=N max_lag_seconds=Mwith 200 or 503.Current behavior: each probe runs the full
status()scan ofmessages,embeddingsandattachment_textin the event loop. On a large mirror during a backfill, the probe passed its 1 s timeout, and liveness restarted the api again and again.Desired behavior:
Store.last_ok_sync() -> dict[str, str]runs onlySELECT account, MAX(finished) AS finished FROM sync_log WHERE error IS NULL GROUP BY account, under the lock as other readers do.status()reuses it for itslast_ok_synckey, so the query is spelled once._current_lag()callslast_ok_sync(), notstatus().anyio.to_thread.run_sync, or FastMCP'scall_sync_fn_in_threadpool), so the event loop never runs a store query.mail_statusis unchanged.Key interfaces:
Store.last_ok_sync()(new),Store.status()(reuses it).server._current_lag(),healthz,healthz_sync.sync_lag_seconds()is unchanged; its tests stay as they are.Acceptance criteria:
sync_log. One way:sqlite3.Connection.set_authorizerorset_trace_callbackon the reader's connection during a/healthzand a/healthz/syncrequest. The test fails if any statement readsmessages,embeddingsorattachment_text.threading.get_ident()insidelast_ok_syncand comparing it with the loop's thread.tests/test_server.py::test_health_routes_need_no_token_and_carry_no_mail_dataandtest_health_sync_answers_503_when_an_account_never_syncedpass unchanged. The second one monkeypatches_current_lag, so keep that name./healthzanswers in under 100 ms. Record the number in the PR.make checkpasses.Out of scope:
mail_statusitself cheaper (ask 4). The options are a covering index, a short TTL cache in the api, or trigger-kept counters, which need a schema v8 step. That is a later decision./healthz.⏸ SVD architect gate: dev/imap-mailmind-mcp#90 healthz-runs-the-full-status-scan-in-the-event-loop-and-fail
Status: CLEAN after 0 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity
standard· dispatch effortmediumRun:
20261101T074016Z-a3a1d3cbHeld because: CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
for i in $(seq 20); do curl -s -o /dev/null -w '%{time_total}\n' -H 'Host: localhost' http://127.0.0.1:8420/healthz; doneand expect every sample under 0.1 s while no mail_status call is running. Switching that instance back from TCP to HTTP probes is estate work and out of scope. [verification]Change: Add Store.last_ok_sync() -> dict[str, str] directly above status() (store.py:1735). Model it on the single-query readers at store.py:1668-1684: take
with self._lock:, run onlySELECT account, MAX(finished) AS finished FROM sync_log WHERE error IS NULL GROUP BY account, and build the dict outside the lock. Inside status()'s lock block, replace the last_runs query (store.py:1743-1745) withlast_ok = self.last_ok_sync(); the RLock makes this re-entry safe. Leave every other status() key unchanged. In server.py, import starlette.concurrency.run_in_threadpool, which is already a direct starlette dependency, so nothing is added. Change _current_lag (server.py:1004-1006) to call s.store.last_ok_sync() instead of status(). In healthz and healthz_sync (server.py:1009-1023), callawait run_in_threadpool(_current_lag)inside the coroutine body. Keep the module-global lookup there so the existing monkeypatch of _current_lag still applies. Response bodies and status codes do not change.Files:
src/imap_mailmind_mcp/store.py·src/imap_mailmind_mcp/server.py·tests/test_store.py·tests/test_server.pyDecision: accept the 3 open risk(s) above and dispatch this spec (
svd:go), or park (svd:skip) / re-spec (svd:respec)?Your call (when this issue is held at the gate via
svd:hold):svd:go— approve; Half-B dispatch accepts exactly this spec (open risks accepted)svd:firebesidesvd:go— approve AND authorise the Half-B fire from the phone; never on its ownsvd:skip— park it; dispatch refuses it and the issue stays for a humansvd:respec— discard this spec; the next Half-A run re-produces it with this record as prior-attempt contextSpec:
/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-healthz-runs-the-full-status-scan-in-the-event-loop-and-fail.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-01-healthz-runs-the-full-status-scan-in-the-event-loop-and-fail.mdspec-verify-dispatch Half B for dev/imap-mailmind-mcp#90 healthz-runs-the-full-status-scan-in-the-event-loop-and-fail completed integration verify.
fd0e51e4b5fd0e51e: no Forgejo commit statuses (absent, no CI in repo). Local: ruff clean, mypy strict clean on src, 342 passed, 4 skipped. Ready for merge; not merged.fd0e51e; the repo has no .woodpecker CI (CLAUDE.md: CI files are added by the architect). — reported as absent, not blocking (gh#633)codex-svd ship --run-id 20261101T075718Z-5ee6851d --confirm-merge)