Backfill stalls on a FETCH response the client cannot parse #32

Closed
opened 2026-10-31 05:03:06 +00:00 by svc-agent-writer · 0 comments

Found on a production iCloud instance running 0.1.1.

What happens

  1. The backfill fetches each mailbox oldest first, in batches of sync.batch_size.
  2. One message in a batch has an ENVELOPE or BODYSTRUCTURE string with an unescaped ". imapclient's lexer refuses the whole response with ProtocolError: Server replied with a response that violates the IMAP protocol (response_lexer.py:104).
  3. The blade maps it to server_error, not retryable. _backfill lets it end the mailbox's pass.
  4. The next pass restarts above the newest mirrored UID, which is the same batch. It fails again on every pass, so no newer mail in that mailbox is ever mirrored.

Other ProtocolError texts carry the offending token (bad response type: b'…'). Those reach the stored sync error and mail_status.

Fix

  • When a batch fails, refetch the same UIDs with FLAGS only. If that fails too, the server is at fault and the error stands, so a transient refusal never skips mail.
  • If FLAGS parses, halve the batch until each bad message is alone.
  • Mirror each bad message from its raw bytes, which arrive as a literal. Use the whole message up to store.max_text_bytes, else the header only.
  • Skip a message only when that also fails. Log counts only.

Acceptance

  • A regression test drives the synthetic bad line through the blade's real parse path.
  • A mailbox with unparseable messages mirrors every other message in one pass.
  • A server that refuses FLAGS too still records the error and skips nothing.
Found on a production iCloud instance running 0.1.1. ## What happens 1. The backfill fetches each mailbox oldest first, in batches of `sync.batch_size`. 2. One message in a batch has an ENVELOPE or BODYSTRUCTURE string with an unescaped `"`. imapclient's lexer refuses the whole response with `ProtocolError: Server replied with a response that violates the IMAP protocol` (`response_lexer.py:104`). 3. The blade maps it to `server_error`, not retryable. `_backfill` lets it end the mailbox's pass. 4. The next pass restarts above the newest mirrored UID, which is the same batch. It fails again on every pass, so no newer mail in that mailbox is ever mirrored. Other `ProtocolError` texts carry the offending token (`bad response type: b'…'`). Those reach the stored sync error and `mail_status`. ## Fix - When a batch fails, refetch the same UIDs with FLAGS only. If that fails too, the server is at fault and the error stands, so a transient refusal never skips mail. - If FLAGS parses, halve the batch until each bad message is alone. - Mirror each bad message from its raw bytes, which arrive as a literal. Use the whole message up to `store.max_text_bytes`, else the header only. - Skip a message only when that also fails. Log counts only. ## Acceptance - A regression test drives the synthetic bad line through the blade's real parse path. - A mailbox with unparseable messages mirrors every other message in one pass. - A server that refuses FLAGS too still records the error and skips nothing.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
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/mail-mind-mcp#32
No description provided.