mail_evidence call in flight >90 s makes /healthz time out and the api container is killed #148
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#148
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?
Summary
On a large mirror, one
mail_evidencecall can stay in flight for more than 90 seconds. While it does,GET /healthzstops answering within its 1 s probe timeout, and the kubelet kills theapicontainer (exit 137). This has happened twice, both times during a People Mind mail pass (dev/people-mind-mcp#45). The mechanism is not established: the two obvious causes are ruled out below.Environment
0.10.4@sha256:5ab9a2e0…19a4, schema v14, fastmcp 3.4.11, Python 3.12.16.GET /healthz, timeout 1 s, period 30 s, failure threshold 3.Evidence
Counts and timings from the
apilog (Processing request of type CallToolRequestlines) and pod events. The only client in the window was People Mind, which callsmail_changes(paging) and thenmail_evidence(address=…)once per address, in sorted address order, one call at a time, at most 4 per second.mail_changespagingmail_evidencemail_evidencemail_evidencemail_evidencePod events:
Liveness probe failed: … context deadline exceeded (Client.Timeout exceeded while awaiting headers), then a readiness failure, thenContainer api failed liveness probe, will be restarted.The
synccontainer in the same pod completed passes at 22:31:37 and 22:31:58, so the database was writable and not locked during the stall.The log does not record the tool arguments or the call duration, so the address that caused the stall is not known. Because People Mind sorts the addresses, a rerun of its pass is expected to reach the same address.
Ruled out
Store.evidenceholdsself._lockfor its three queries, but/healthzcallslast_ok_sync, which opens its own read-only connection. A slow evidence query cannot make/healthzwait on the lock.evidence._ADDRESSis quadratic on a long line without whitespace or@(synthetic line: 20,000 chars 1.6 s, 40,000 chars 6.2 s). But run throughasyncio.to_threadon Python 3.12, it did not stall an asyncio ticker (longest stall 0.01 s), so it cannot by itself block/healthz. It is still a cost worth bounding:sign_offruns it on every line of the body before it selects the last paragraph.mail_searchtaking 11–12 s andmail_counttaking about 60 s left/healthzanswering in under 0.01 s.Open questions
/healthzwait on during the stall? Candidates, unconfirmed: the anyio worker-thread limiter (40 tokens) if threads pile up, or contention in the HTTP or auth path.mail_evidencearguments take more than 90 s, and why: the correlatedNOT EXISTScopy check for a high-volume sender, the regex on a large body, or something else.Proposed next steps
faulthandler.dump_traceback_laterarmed per tool call, or a signal handler that dumps every thread's stack, so the next stall shows where each thread is.mail_evidence: cap the body length passed toevidence.signatures, or run the address redaction only on the paragraphssign_offinspects.mail_evidencecall on a sender with a pathological body keeps/healthzanswering within 1 s.Agent Brief
Category: bug
Summary: Make the next
/healthzstall diagnosable, and bound the cost ofmail_evidenceaddress redaction. This brief does not fix the root cause, because the mechanism is not known yet.Current behavior:
sign_offin the evidence module runs the_ADDRESSredaction regex on every line of the trimmed body. The regex is quadratic on a long line without whitespace: 20,000 characters take about 1 s.Triage checked these premises in the code:
/healthzdoes not wait on theStorelock.Store.last_ok_syncopens its own read-only connection, and the route runs it through Starlette'srun_in_threadpool.anyio.to_thread.run_sync. That is the same default anyio limiter (40 threads) that/healthzuses. Exhaustion of this limiter is a candidate cause, not a confirmed one.Desired behavior:
Per-call log. When each tool call completes, the api process logs one line with the tool name, the duration in milliseconds and the outcome:
okor the error code. It logs no argument in any form: no value, no hash, no length of an address. ADR 0008 §6 says "The address in a call is never logged." The issue proposed a hash of the address. A hash still links calls to one sender, so do not log it.Event-loop stall dump. The api process arms
faulthandler.dump_traceback_later(timeout, repeat=True)and re-arms it from an asyncio heartbeat task. If the event loop stops for longer than the timeout, the faulthandler C thread dumps the stack of every thread to stderr. That thread does not need the GIL.Slow-call dump. When one tool call runs longer than a threshold, the api process dumps the stack of every thread once for that call. Note that
dump_traceback_laterhas one timer per process, so it cannot also be armed per call. Use a separate mechanism, for example a Python watchdog thread.Configuration. Both thresholds are config fields with defaults, for example 20 s for the event loop and 60 s for a slow call. A value of 0 turns that dump off. Choose the default event-loop threshold so the dump fires before a liveness kill at the current probe settings (period 30 s, failure threshold 3).
Linear-time redaction. For every input,
sign_offreturns exactly the output it returns today. Its run time is linear in the body length, also on a long line without whitespace, with or without an@. Two traps:_BOILERPLATEmatchesunsubscribeinside an unredacted address such asunsubscribe@list.example, so redaction must run before the boilerplate check on every paragraph that the check reads.MAX_SIGN_OFF_LINE_CHARS) are measured after redaction. A line that redaction makes shorter can still anchor or join a block.Acceptable approaches include a regex rewrite that keeps the same matches, or a cheap guard before the regex runs. The equivalence must hold either way.
Key interfaces:
_renderwrapper in the server module: the place where one tool call starts and ends.build_app()or the api lifespan: where the heartbeat task starts and stops.MailmindConfig: the two new threshold fields.evidence._ADDRESS,evidence.sign_offandevidence.REDACTED: the redaction contract that must not change.Acceptance criteria:
mail_evidencewith a known address and asserts that the address does not appear in any log record, and that no hash or length of it appears.sign_offwith the old redaction on a corpus of crafted lines and asserts equal output. Includeunsubscribe@…, a line that redaction shortens below 72 characters, a line that is only an address, and several@in one token.sign_offon a 40,000-character line without whitespace, once with an@and once without. Each run finishes in under 0.2 s.make checkpasses.Out of scope:
deploy/k8s, and any split of liveness from/healthz. The maintainer rules on this after a stack dump shows the cause.Store.evidence, including the correlatedNOT EXISTScopy check. Change it only if a captured dump implicates it, and do that in a separate issue.mail_evidenceoutput (ADR 0008 §2).⏸ SVD architect gate: dev/mail-mind-mcp#148 mail-evidence-call-in-flight-90-s-makes-healthz-time-out-and
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:
20261102T231527Z-80641e7cHeld because: CLEAN but 3 open risk(s) (2 verification · 1 decision) — HOLD-ALL parks everything. The risks, verbatim:
Open risks:
kubectl logs <api-pod> -c api --previous(or the current container) shows a faulthandler 'Timeout (0:00:20)!' block or a 'slow tool call tool=mail_evidence seconds=…' header with all thread stacks, and the 'tool call' lines name the last call that completed before the stall. [verification]kubectl logs <api-pod> -c api | grep '"msg": "tool call"'returns lines with tool, duration_ms and outcome, and no line contains an '@'. [verification]Change: (1) evidence.py:20,67 — replace the quadratic _ADDRESS.sub with a single-pass _redact: _TOKEN=[^\s<>()[],;:"'|]+ sub'd to REDACTED iff '@' in run[1:-1]. This was proven identical at spec time on 300k random strings plus the crafted lines. Order is unchanged, so redaction still runs before the _BOILERPLATE and 72-char checks. A 40k line takes 0.0008 s, against 1.57 s at 20k today. (2) server.py:277-290 _render, plus mail_triage_rows at 1083-1108: wrap each call in a _tool_call(tool) context manager. It registers the call in a module-level stall.SlowCalls and logs one 'tool call' record with exactly the extras tool, duration_ms and outcome (ok, a BladeError.code, or internal). It logs no argument. (3) server.py:1433-1445 build_app: wrap app.router.lifespan_context so that FastMCP's inner lifespan is entered first, then stall.loop_watch(loop_stall_dump_seconds), which re-arms faulthandler.dump_traceback_later(repeat=True) from an asyncio heartbeat. _slow_calls.start(slow_call_dump_seconds) and stop() run inside it. (4) New stall.py, stdlib only: loop_watch plus a SlowCalls watchdog thread modelled on jmap.py:340-356. It dumps all thread stacks once per call past the threshold, with faulthandler.dump_traceback(all_threads=True) and a header that names only the tool. (5) config.py ServerConfig: two fields, loop_stall_dump_seconds=20.0 and slow_call_dump_seconds=60.0. Each accepts 0 (off) or at least 1, with a validator mirroring config.py:141-146. (6) cli.py:41-62 _JsonFormatter: add 'tool' and 'outcome' to the allowlist. (7) Docs: README [server] row and Health section, config.example.toml [server], CLAUDE.md structure line and Logs rule.
Files:
src/mail_mind_mcp/evidence.py·src/mail_mind_mcp/stall.py·src/mail_mind_mcp/server.py·src/mail_mind_mcp/config.py·src/mail_mind_mcp/cli.py·tests/test_stall.py·tests/test_evidence.py·tests/test_config.py·README.md·config.example.toml·CLAUDE.mdDecision: 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-02-mail-evidence-call-in-flight-90-s-makes-healthz-time-out-and.md— next:codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mail-evidence-call-in-flight-90-s-makes-healthz-time-out-and.mdsvc-svd-writer referenced this issue2026-11-02 23:43:31 +00:00
spec-verify-dispatch Half B for dev/mail-mind-mcp#148 mail-evidence-call-in-flight-90-s-makes-healthz-time-out-and completed integration verify.
4485fd6d334485fd6(987 passed, 8 skipped, audit clean). All spec-mapped tests collect. Remote: no Forgejo commit statuses (absent). One blocker: the PR body hasCloses #148and the spec requiresRefsonly.Closes #148from the PR body; the spec requiresRefs #148only, and the body currently has both.Closes #148from the PR body; the spec requiresRefs #148only, and the body currently has both.; no denial signature; evidence: PR #149 body line 14Refs #148, line 16Closes #148; spec: 'The PR body saysRefs #148, notCloses.'4485fd6updates tests/test_mailbox_search.py per spec Adjustment A1, and the test passes.Co-Authored-By: gpt-6.1-sol. Check that it matches the repo's attribution convention.make checkis the only integration evidence.4485fd6has no commit statuses on Forgejo (empty list). No CI run exists to track. — reported as absent, not blocking (gh#633)Closes #148from the PR body; the spec requiresRefs #148only, and the body currently has both.; edit the PR title or body by hand, then re-run integrate or run the own-execution gate.spec-verify-dispatch Half B for dev/mail-mind-mcp#148 mail-evidence-call-in-flight-90-s-makes-healthz-time-out-and completed integration verify.
4485fd6d334485fd6: local make check is green with 987 passed and 8 skipped. The repo has no remote CI statuses, so that check is absent. No blockers.4485fd6fixed tests/test_mailbox_search.py::test_unknown_mailbox_warns, which the spec names as Agent Adjustment A1. At4485fd6, make check passes: ruff, format, mypy strict, 987 passed, 8 skipped, and pip-audit clean. Update the PR body.4485fd6has no commit statuses on git.groupthink.asia. CLAUDE.md says the architect adds CI files (.woodpecker/*), so the repo reports no CI. — reported as absent, not blocking (gh#633)codex-svd ship --run-id 20261102T233859Z-7aa0e4ad --confirm-merge)Not reproduced on 0.11.0 (
48093ca9…e52f, which carries the #149 diagnostics and the linear sign-off redaction), on the same mirror and the same client.The People Mind mail Source ran two passes against it, because its pod restarted between them. Per-minute counts from the
tool calllines; no arguments are logged.mail_changesphasemail_evidencephasemail_evidencecallsOver 80 minutes: 15,877
mail_evidencecalls at the 4/s pacer, slowest 1.8 s; 0slow tool calllines (threshold 60 s); 0 faulthandler dumps (threshold 20 s); 0apirestarts.On 0.10.4 the stall came about 18 minutes into the
mail_evidencephase, after about 4,300 calls. The client asks in sorted address order, so both passes went well past the address that stalled before.The ruling-out above showed that the quadratic
_ADDRESSregex did not stall the event loop from a worker thread in isolation, so the linear redaction is a candidate fix, not a proven one. Leaving this open for the maintainer: close it, or keep it until a few more full passes have run.