From 66dcd1f78eea9847935ae5244af2b58ca1188e46 Mon Sep 17 00:00:00 2001 From: Xika Date: Thu, 3 Dec 2026 23:17:08 +0000 Subject: [PATCH] fix(imap): catch TimeoutError on per-message fetch so sync continues (BIT-33891) 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 --- src/forwarder/services/sync_service.py | 2 +- tests/test_sync_timeout.py | 54 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 tests/test_sync_timeout.py diff --git a/src/forwarder/services/sync_service.py b/src/forwarder/services/sync_service.py index 5867457..bc06100 100644 --- a/src/forwarder/services/sync_service.py +++ b/src/forwarder/services/sync_service.py @@ -260,7 +260,7 @@ def sync_account( try: msg = fetch_message(imap, uid) - except imaplib.IMAP4.error as exc: + except (imaplib.IMAP4.error, OSError) as exc: logger.error("Fetch error UID %s in %s: %s", uid_str, folder, exc) result.messages_failed += 1 continue diff --git a/tests/test_sync_timeout.py b/tests/test_sync_timeout.py new file mode 100644 index 0000000..3361052 --- /dev/null +++ b/tests/test_sync_timeout.py @@ -0,0 +1,54 @@ +"""Tests for IMAP timeout handling in sync_account.""" + +from __future__ import annotations + +from unittest.mock import MagicMock, patch + +from forwarder.db.models import DeliveryMode, SourceType, SyncRunStatus +from forwarder.services.sync_service import sync_account + + +def _make_account() -> MagicMock: + account = MagicMock() + account.source_type = SourceType.imap + account.imap_host = "imap.example.com" + account.imap_port = 993 + account.imap_user = "user@example.com" + account.imap_password_encrypted = b"encrypted" + account.delivery_mode = DeliveryMode.api + account.bizdocs_password_encrypted = b"encrypted" + account.allowed_extensions = None + account.imap_folder_selection_mode = "default" + account.imap_selected_folders = None + account.include_all_attachments = False + return account + + +def test_sync_continues_after_fetch_timeout(): + """A TimeoutError on fetch_message must not kill the entire sync run.""" + db = MagicMock() + db.query.return_value.filter.return_value.all.return_value = [] + + imap = MagicMock() + imap.uid.side_effect = lambda *args: ("OK", [b"1 2"]) if args[0] == "search" else ("OK", [None]) + + account = _make_account() + + fetch_calls: list = [] + + def _fetch_side_effect(_imap, uid): + fetch_calls.append(uid) + raise TimeoutError("The read operation timed out") + + with patch("forwarder.services.sync_service.get_account", return_value=account): + with patch("forwarder.services.sync_service.decrypt_password", return_value="secret"): + with patch("forwarder.services.sync_service.connect_imap", return_value=imap): + with patch("forwarder.services.sync_service.list_mailboxes", return_value=["INBOX"]): + with patch("forwarder.services.sync_service.select_folder_readonly", return_value=True): + with patch("forwarder.services.sync_service.fetch_message", side_effect=_fetch_side_effect): + with patch("forwarder.services.sync_service.report_error_with_cooldown"): + sync_run = sync_account(db, 1, dry_run=True) + + assert sync_run.status == SyncRunStatus.completed + assert sync_run.messages_failed == 2 + assert len(fetch_calls) == 2 -- 2.47.3