mailmind Source hangs forever when Mail Mind restarts mid-call (no client timeout) #45

Closed
opened 2026-11-02 21:54:46 +00:00 by svc-agent-writer · 3 comments

Summary

The mailmind Source can hang forever. _fetch builds Client(_transport(...)) with no timeout, and _transport sets none either. If the Mail Mind server restarts while a call_tool is in flight, the call never returns. run_pass then never writes a sync_log row or a checkpoint, and the worker thread stays blocked until the pod restarts. /healthz/sync reports stale, but nothing recovers the worker.

A second weakness makes the hang expensive: a pass saves progress only at pass end or on a fetch failure. The mail_evidence phase keeps its results in memory, so any failure there repeats every evidence call on the next pass.

Evidence

Local reproduction with the pinned dependencies (fastmcp 3.4.11, mcp 1.30.0, httpx 0.28.1): a FastMCP HTTP server with one tool that sleeps 60 s, killed with SIGKILL 5 s into the call.

Client Result
Client(transport) (as in _fetch) still waiting at 120 s (killed by timeout)
Client(transport, timeout=20) raises McpError at 20.0 s

Production observation on rc.6 (codes and counts only):

  • The first mail pass started at pod start and was making calls at about 2 req/s.
  • The Mail Mind api container restarted about 28 minutes later.
  • After that: no mail sync_log row and no mail log line for more than 9 hours, sync container CPU near 0, no open connection to Mail Mind. The two CardDAV Sources logged 36 passes each in the same period.
  • The stored checkpoint is the one from the last failure before the upgrade: cursor at about 60% of the change log, 5,505 pending addresses, 152,331 pending events.

Ruled out: the per-page rebuild of the sorted pending map. At this checkpoint size it costs about 71 ms per page locally, about 90 s CPU across a full pass.

Proposed fix

  1. Pass a per-request timeout to Client in _fetch (configurable, for example request_timeout_seconds, default 60). The existing except Exception already maps the resulting McpError / timeout to SourceError("mailmind-unavailable"), so the pass fails, checkpoints, and retries on the next interval.
  2. Add a regression test: a server that dies mid-call makes _fetch raise SourceError within the timeout.
  3. Consider making the evidence phase resumable, for example by checkpointing the set of addresses already resolved, so a failure late in a pass does not repeat thousands of evidence calls.

Earlier failures (not this bug)

The mailmind-protocol failures before rc.4 were #39: one malformed envelope address failed every pass at the same page. c611a19 fixed it and shipped in rc.4; no mailmind-protocol row has been written since. The rc.4 pod then ran for about 7 hours without writing a mail sync_log row either. The Mail Mind Deployment was scaled to zero for a backup in that window, which fits this hang, but that pass was not traced.

## Summary The mailmind Source can hang forever. `_fetch` builds `Client(_transport(...))` with no `timeout`, and `_transport` sets none either. If the Mail Mind server restarts while a `call_tool` is in flight, the call never returns. `run_pass` then never writes a `sync_log` row or a checkpoint, and the worker thread stays blocked until the pod restarts. `/healthz/sync` reports `stale`, but nothing recovers the worker. A second weakness makes the hang expensive: a pass saves progress only at pass end or on a fetch failure. The `mail_evidence` phase keeps its results in memory, so any failure there repeats every evidence call on the next pass. ## Evidence Local reproduction with the pinned dependencies (fastmcp 3.4.11, mcp 1.30.0, httpx 0.28.1): a FastMCP HTTP server with one tool that sleeps 60 s, killed with SIGKILL 5 s into the call. | Client | Result | | --- | --- | | `Client(transport)` (as in `_fetch`) | still waiting at 120 s (killed by `timeout`) | | `Client(transport, timeout=20)` | raises `McpError` at 20.0 s | Production observation on rc.6 (codes and counts only): - The first mail pass started at pod start and was making calls at about 2 req/s. - The Mail Mind `api` container restarted about 28 minutes later. - After that: no mail `sync_log` row and no mail log line for more than 9 hours, sync container CPU near 0, no open connection to Mail Mind. The two CardDAV Sources logged 36 passes each in the same period. - The stored checkpoint is the one from the last failure before the upgrade: cursor at about 60% of the change log, 5,505 pending addresses, 152,331 pending events. Ruled out: the per-page rebuild of the sorted pending map. At this checkpoint size it costs about 71 ms per page locally, about 90 s CPU across a full pass. ## Proposed fix 1. Pass a per-request timeout to `Client` in `_fetch` (configurable, for example `request_timeout_seconds`, default 60). The existing `except Exception` already maps the resulting `McpError` / timeout to `SourceError("mailmind-unavailable")`, so the pass fails, checkpoints, and retries on the next interval. 2. Add a regression test: a server that dies mid-call makes `_fetch` raise `SourceError` within the timeout. 3. Consider making the evidence phase resumable, for example by checkpointing the set of addresses already resolved, so a failure late in a pass does not repeat thousands of evidence calls. ## Earlier failures (not this bug) The `mailmind-protocol` failures before rc.4 were #39: one malformed envelope address failed every pass at the same page. `c611a19` fixed it and shipped in rc.4; no `mailmind-protocol` row has been written since. The rc.4 pod then ran for about 7 hours without writing a mail `sync_log` row either. The Mail Mind Deployment was scaled to zero for a backup in that window, which fits this hang, but that pass was not traced.
Author
Member

This was generated by AI during triage.

Agent Brief

Category: bug
Summary: Give the mailmind Source a per-request timeout so a Mail Mind restart mid-call fails the pass instead of hanging the sync worker forever.

Current behavior:
MailmindSource._fetch opens a fastmcp Client over a StreamableHttpTransport with no timeout. When the Mail Mind server dies while a call_tool is in flight, the call never returns. The pass writes no sync_log row and no checkpoint, and the worker stays blocked until the pod restarts.

Triage reproduced this with the pinned dependencies (fastmcp 3.4.11). A FastMCP HTTP server with one tool that sleeps 60 s was killed with SIGKILL 5 s into the call:

  • Client(transport): still waiting at the 45 s cap.
  • Client(transport, timeout=10): raised McpError at 10.0 s.

Desired behavior:
Every Mail Mind request has a bounded wait. A request that exceeds it fails the pass with the value-free code mailmind-unavailable. The pass then follows the existing fetch-failure path: it writes a sync_log row, saves the resumable checkpoint (cursor and pending map), and retries on the next interval. Nothing else about the pass changes.

Key interfaces:

  • MailmindSourceConfig: add request_timeout_seconds, a positive number with default 60. Validate it like the other numeric fields (gt=0).
  • The fastmcp Client built in MailmindSource._fetch: pass the configured value as timeout. fastmcp 3.4.11 Client.__init__ accepts timeout.
  • Error mapping: the existing broad except Exception in _fetch already maps the resulting McpError / timeout to SourceError("mailmind-unavailable"). Keep that mapping. Do not let exception text reach the error code or the logs.
  • Precedent: the CardDAV Source already sets an HTTP timeout of 30 s.

Acceptance criteria:

  • MailmindSourceConfig accepts request_timeout_seconds, defaults to 60, and rejects zero and negative values.
  • A regression test uses a fake Mail Mind tool that never returns (for example, it awaits an event that is never set) and a short timeout. The test asserts that _fetch raises SourceError with code mailmind-unavailable within a bounded wall-clock time.
  • A test asserts that a timeout after at least one accepted mail_changes page leaves the source state at the last accepted checkpoint, so the next pass resumes without refetching. Reuse the shape of the existing failed-pass checkpoint tests.
  • The existing mailmind test suite passes unchanged. The virtual clock and sleep fixtures must still apply to pacing and retry delays.
  • Any config reference that lists mailmind Source fields documents the new field.

Out of scope:

  • Making the mail_evidence phase resumable (fix step 3 in the issue body). Persisting evidence results in the checkpoint conflicts with ADR 0003 §2 (no mail text stored) and §8 (Forget removes everything about a person). It needs its own design decision and a separate issue.
  • Changing retry or backoff behaviour for HTTP 401/429.
  • Changing CardDAV timeouts.
  • A watchdog or liveness probe for the sync worker.
> *This was generated by AI during triage.* ## Agent Brief **Category:** bug **Summary:** Give the mailmind Source a per-request timeout so a Mail Mind restart mid-call fails the pass instead of hanging the sync worker forever. **Current behavior:** `MailmindSource._fetch` opens a fastmcp `Client` over a `StreamableHttpTransport` with no timeout. When the Mail Mind server dies while a `call_tool` is in flight, the call never returns. The pass writes no `sync_log` row and no checkpoint, and the worker stays blocked until the pod restarts. Triage reproduced this with the pinned dependencies (fastmcp 3.4.11). A FastMCP HTTP server with one tool that sleeps 60 s was killed with SIGKILL 5 s into the call: - `Client(transport)`: still waiting at the 45 s cap. - `Client(transport, timeout=10)`: raised `McpError` at 10.0 s. **Desired behavior:** Every Mail Mind request has a bounded wait. A request that exceeds it fails the pass with the value-free code `mailmind-unavailable`. The pass then follows the existing fetch-failure path: it writes a `sync_log` row, saves the resumable checkpoint (cursor and pending map), and retries on the next interval. Nothing else about the pass changes. **Key interfaces:** - `MailmindSourceConfig`: add `request_timeout_seconds`, a positive number with default 60. Validate it like the other numeric fields (`gt=0`). - The fastmcp `Client` built in `MailmindSource._fetch`: pass the configured value as `timeout`. fastmcp 3.4.11 `Client.__init__` accepts `timeout`. - Error mapping: the existing broad `except Exception` in `_fetch` already maps the resulting `McpError` / timeout to `SourceError("mailmind-unavailable")`. Keep that mapping. Do not let exception text reach the error code or the logs. - Precedent: the CardDAV Source already sets an HTTP timeout of 30 s. **Acceptance criteria:** - [ ] `MailmindSourceConfig` accepts `request_timeout_seconds`, defaults to 60, and rejects zero and negative values. - [ ] A regression test uses a fake Mail Mind tool that never returns (for example, it awaits an event that is never set) and a short timeout. The test asserts that `_fetch` raises `SourceError` with code `mailmind-unavailable` within a bounded wall-clock time. - [ ] A test asserts that a timeout after at least one accepted `mail_changes` page leaves the source state at the last accepted checkpoint, so the next pass resumes without refetching. Reuse the shape of the existing failed-pass checkpoint tests. - [ ] The existing mailmind test suite passes unchanged. The virtual clock and sleep fixtures must still apply to pacing and retry delays. - [ ] Any config reference that lists mailmind Source fields documents the new field. **Out of scope:** - Making the `mail_evidence` phase resumable (fix step 3 in the issue body). Persisting evidence results in the checkpoint conflicts with ADR 0003 §2 (no mail text stored) and §8 (Forget removes everything about a person). It needs its own design decision and a separate issue. - Changing retry or backoff behaviour for HTTP 401/429. - Changing CardDAV timeouts. - A watchdog or liveness probe for the sync worker.
Collaborator

⏸ SVD architect gate: dev/people-mind-mcp#45 mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca

Status: CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity standard · dispatch effort medium

Run: 20261102T234709Z-9372eab3

Held because: CLEAN but 3 open risk(s) (1 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. Fix step 3 (a resumable mail_evidence phase) is deliberately out of scope. Persisting evidence results conflicts with ADR 0003 §2 and §8. After this fix, a late timeout still repeats every evidence call on the next pass: about 23 minutes for 5,505 addresses at 4 req/s. Should a separate issue design resumable evidence by checkpointing resolved addresses only, or is the repeat cost acceptable? [decision]
  2. The regression tests use the in-memory FastMCPTransport, so the HTTP path is untested here. Post-merge check on the rc pod: restart the Mail Mind api Deployment during a mail pass. Confirm that a mailmind-unavailable sync_log row appears within about 60 s, that /healthz/sync recovers on the next interval, and that the stored cursor has not regressed. [verification]
  3. The 60 s default comes from the issue and brief. It is longer than CardDAV's 30 s and equal to RETRY_AFTER_MAX. A slow but legitimate mail_evidence call on a large mirror would now fail the pass. Is 60 s the right default? The lean keeps 60, and operators can raise it per Source. [decision]

Change: Add request_timeout_seconds: float = Field(default=60.0, gt=0) after rate_per_second in MailmindSourceConfig (config.py:146). Pass it as timeout= to Client(...) in MailmindSource._fetch (mailmind.py:179). Leave _transport alone. The resulting McpError falls through the existing except Exception: raise SourceError("mailmind-unavailable") from None (mailmind.py:260-261), so no new except branch is needed. Append tests to tests/test_mailmind.py using an async stalling_mail FastMCP helper, mirroring test_mailmind_rate_per_second_config (:946-951) and the checkpoint tests at :413-465 and :495-541. Document the field in README.md: a prose sentence at line 117 and request_timeout_seconds = 60 after line 126 inside the TOML fence at 119-127, re-checked at 2289943602. Add a commented line to deploy/k8s/overlays/example/config.toml:33-39.

Files: src/people_mind_mcp/config.py · src/people_mind_mcp/sources/mailmind.py · tests/test_mailmind.py · README.md · deploy/k8s/overlays/example/config.toml

Decision: 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):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca.md

### ⏸ SVD architect gate: dev/people-mind-mcp#45 mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca **Status:** CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `standard` · dispatch effort `medium` **Run:** `20261102T234709Z-9372eab3` **Held because:** CLEAN but 3 open risk(s) (1 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. Fix step 3 (a resumable mail_evidence phase) is deliberately out of scope. Persisting evidence results conflicts with ADR 0003 §2 and §8. After this fix, a late timeout still repeats every evidence call on the next pass: about 23 minutes for 5,505 addresses at 4 req/s. Should a separate issue design resumable evidence by checkpointing resolved addresses only, or is the repeat cost acceptable? [decision] 2. The regression tests use the in-memory FastMCPTransport, so the HTTP path is untested here. Post-merge check on the rc pod: restart the Mail Mind api Deployment during a mail pass. Confirm that a mailmind-unavailable sync_log row appears within about 60 s, that /healthz/sync recovers on the next interval, and that the stored cursor has not regressed. [verification] 3. The 60 s default comes from the issue and brief. It is longer than CardDAV's 30 s and equal to RETRY_AFTER_MAX. A slow but legitimate mail_evidence call on a large mirror would now fail the pass. Is 60 s the right default? The lean keeps 60, and operators can raise it per Source. [decision] **Change:** Add `request_timeout_seconds: float = Field(default=60.0, gt=0)` after `rate_per_second` in MailmindSourceConfig (config.py:146). Pass it as `timeout=` to `Client(...)` in MailmindSource._fetch (mailmind.py:179). Leave `_transport` alone. The resulting McpError falls through the existing `except Exception: raise SourceError("mailmind-unavailable") from None` (mailmind.py:260-261), so no new except branch is needed. Append tests to tests/test_mailmind.py using an async `stalling_mail` FastMCP helper, mirroring test_mailmind_rate_per_second_config (:946-951) and the checkpoint tests at :413-465 and :495-541. Document the field in README.md: a prose sentence at line 117 and `request_timeout_seconds = 60` after line 126 inside the TOML fence at 119-127, re-checked at 22899436023f. Add a commented line to deploy/k8s/overlays/example/config.toml:33-39. **Files:** `src/people_mind_mcp/config.py` · `src/people_mind_mcp/sources/mailmind.py` · `tests/test_mailmind.py` · `README.md` · `deploy/k8s/overlays/example/config.toml` **Decision:** 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`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-02-mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/people-mind-mcp#45 mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca completed integration verify.

  • PR: #46
  • Branch: feat/svd-dev-people-mind-mcp-45-mailmind-source-hangs-forever-5cc1378c
  • Commit: 090f28518e
  • Build: none
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/73/pipeline/12/1
  • Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll
  • Warning: Remote check ci/woodpecker/pr/validation is still pending; the runner re-polls it.
  • Warning: Spec open risks R1 (resumable mail_evidence), R2 (post-merge rc pod restart check) and R3 (60 s default) remain architect follow-ups.
  • CI wait: ci/woodpecker/pr/validation -> pass
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261103T000545Z-5cc1378c --confirm-merge)
spec-verify-dispatch Half B for dev/people-mind-mcp#45 mailmind-source-hangs-forever-when-mail-mind-restarts-mid-ca completed integration verify. - PR: https://git.groupthink.asia/dev/people-mind-mcp/pulls/46 - Branch: feat/svd-dev-people-mind-mcp-45-mailmind-source-hangs-forever-5cc1378c - Commit: 090f28518ed576daaaaabc9234af7084d5727b8c - Build: none - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Check ci/woodpecker/pr/validation: green https://ci.groupthink.asia/repos/73/pipeline/12/1 - Checks: required ci/woodpecker/pr/validation resolved pass by CI-wait poll - Warning: Remote check ci/woodpecker/pr/validation is still pending; the runner re-polls it. - Warning: Spec open risks R1 (resumable mail_evidence), R2 (post-merge rc pod restart check) and R3 (60 s default) remain architect follow-ups. - CI wait: ci/woodpecker/pr/validation -> pass - Decision: review the PR and merge? (`codex-svd ship --run-id 20261103T000545Z-5cc1378c --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/people-mind-mcp#45
No description provided.