fix(imap): catch TimeoutError on per-message fetch so sync continues (BIT-33891) #8

Merged
spark merged 1 commits from fix/BIT-33891-imap-timeout into main 2026-12-03 23:31:18 +00:00
Owner

Fix: catch TimeoutError on per-message IMAP fetch (BIT-33891)

Root cause

sync_account wraps the fetch_message(imap, uid) call in
except imaplib.IMAP4.error, but TimeoutError (raised by socket.recv
during a slow IMAP read) is an OSError subclass — not an
imaplib.IMAP4.error. The timeout escaped the per-message handler and
hit the outer except Exception, marking the entire sync run as
failed
and reporting an Error Loop event for a single slow message.

Fix

src/forwarder/services/sync_service.py:263 — broaden the exception
clause to (imaplib.IMAP4.error, OSError) so a timed-out fetch is
logged, counted as messages_failed, and the sync continues with the
next message.

Test

tests/test_sync_timeout.py — mocks fetch_message to raise
TimeoutError for every UID in a folder and asserts:

  • sync_run.status == SyncRunStatus.completed (not failed)
  • sync_run.messages_failed == 2 (both UIDs counted)
  • fetch_message called for both UIDs (sync did not abort)

Verified the test fails on unpatched main (status = failed) and
passes with the fix. Full suite: 52/52 green.

## Fix: catch `TimeoutError` on per-message IMAP fetch (BIT-33891) ### Root cause `sync_account` wraps the `fetch_message(imap, uid)` call in `except imaplib.IMAP4.error`, but `TimeoutError` (raised by `socket.recv` during a slow IMAP read) is an `OSError` subclass — **not** an `imaplib.IMAP4.error`. The timeout escaped the per-message handler and hit the outer `except Exception`, marking the **entire sync run as failed** and reporting an Error Loop event for a single slow message. ### Fix `src/forwarder/services/sync_service.py:263` — broaden the exception clause to `(imaplib.IMAP4.error, OSError)` so a timed-out fetch is logged, counted as `messages_failed`, and the sync continues with the next message. ### Test `tests/test_sync_timeout.py` — mocks `fetch_message` to raise `TimeoutError` for every UID in a folder and asserts: - `sync_run.status == SyncRunStatus.completed` (not `failed`) - `sync_run.messages_failed == 2` (both UIDs counted) - `fetch_message` called for both UIDs (sync did not abort) Verified the test fails on unpatched `main` (status = `failed`) and passes with the fix. Full suite: 52/52 green.
spark added 1 commit 2026-12-03 23:17:25 +00:00
TimeoutError (OSError subclass) escaped the imaplib.IMAP4.error handler
in sync_account, killing the entire sync run on a single slow fetch.
Catch OSError alongside IMAP4.error to skip the timed-out message and
continue scanning.

Paperclip-BIT-33891
paperclip-review-bot left a comment
Member

auto-approve: CI success on 66dcd1f78e (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 66dcd1f78eea (gitea_auto_approve_open_prs_once.py)
founding-engineer left a comment
Member

auto-approve: CI success on 66dcd1f78e (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 66dcd1f78eea (gitea_auto_approve_open_prs_once.py)
devops-engineer left a comment
Member

auto-approve: CI success on 66dcd1f78e (gitea_auto_approve_open_prs_once.py)

auto-approve: CI success on 66dcd1f78eea (gitea_auto_approve_open_prs_once.py)
spark merged commit c389503fb9 into main 2026-12-03 23:31:18 +00:00
Sign in to join this conversation.