fix(imap): catch OSError on logout of timed-out socket (BIT-33892)
ci / verify (pull_request) Successful in 27s
ci / verify (pull_request) Successful in 27s
The finally block in sync_account and retry_single_message only caught
imaplib.IMAP4.error from imap.logout(). When the underlying socket had
already timed out, logout() raises OSError('cannot read from timed out
object') which escaped the handler and crashed the sync run.
Add OSError to the except tuple so cleanup completes cleanly.
Paperclip-BIT-33892
This commit is contained in:
1 parent
f39338a2a9
commit
3ca283aa41
2 files changed
+59
-2
No files matched your search
@@ -414,7 +414,7 @@ def sync_account(
|
|||||||
if imap is not None:
|
if imap is not None:
|
||||||
try:
|
try:
|
||||||
imap.logout()
|
imap.logout()
|
||||||
except imaplib.IMAP4.error:
|
except (imaplib.IMAP4.error, OSError):
|
||||||
pass
|
pass
|
||||||
if smtp is not None:
|
if smtp is not None:
|
||||||
try:
|
try:
|
||||||
@@ -567,7 +567,7 @@ def retry_single_message(db: Session, message_id: int) -> Message:
|
|||||||
if imap is not None:
|
if imap is not None:
|
||||||
try:
|
try:
|
||||||
imap.logout()
|
imap.logout()
|
||||||
except imaplib.IMAP4.error:
|
except (imaplib.IMAP4.error, OSError):
|
||||||
pass
|
pass
|
||||||
if smtp is not None:
|
if smtp is not None:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -0,0 +1,57 @@
|
|||||||
|
"""Tests for IMAP logout handling on a timed-out socket.
|
||||||
|
|
||||||
|
Regression test for BIT-33892: ``OSError: cannot read from timed out object``
|
||||||
|
raised by ``imap.logout()`` when the underlying socket has already timed out.
|
||||||
|
The ``finally`` block must swallow ``OSError`` alongside ``imaplib.IMAP4.error``
|
||||||
|
so the sync run can finish cleanly instead of crashing.
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from forwarder.services.sync_service import sync_account
|
||||||
|
|
||||||
|
|
||||||
|
def test_sync_account_swallows_logout_oserror(monkeypatch):
|
||||||
|
"""logout() raising OSError (timed-out socket) must not crash sync_account."""
|
||||||
|
db = MagicMock()
|
||||||
|
|
||||||
|
account = MagicMock()
|
||||||
|
account.id = 1
|
||||||
|
account.source_type = "imap"
|
||||||
|
account.delivery_mode = "smtp"
|
||||||
|
account.imap_host = "imap.example.com"
|
||||||
|
account.imap_port = 993
|
||||||
|
account.imap_user = "[email protected]"
|
||||||
|
account.imap_password_encrypted = b"enc"
|
||||||
|
account.smtp_host = "smtp.example.com"
|
||||||
|
account.smtp_port = 587
|
||||||
|
account.smtp_user = "[email protected]"
|
||||||
|
account.smtp_password_encrypted = b"enc"
|
||||||
|
account.smtp_use_tls = True
|
||||||
|
account.allowed_extensions = None
|
||||||
|
|
||||||
|
imap = MagicMock()
|
||||||
|
imap.logout.side_effect = OSError("cannot read from timed out object")
|
||||||
|
smtp = MagicMock()
|
||||||
|
smtp.quit = MagicMock()
|
||||||
|
|
||||||
|
def fake_list_mailboxes(_imap):
|
||||||
|
return []
|
||||||
|
|
||||||
|
with (
|
||||||
|
patch("forwarder.services.sync_service.get_account", return_value=account),
|
||||||
|
patch("forwarder.services.sync_service.decrypt_password", return_value="secret"),
|
||||||
|
patch("forwarder.services.sync_service.connect_imap", return_value=imap),
|
||||||
|
patch("forwarder.services.sync_service.create_smtp_client", return_value=smtp),
|
||||||
|
patch("forwarder.services.sync_service.list_mailboxes", side_effect=fake_list_mailboxes),
|
||||||
|
patch("forwarder.services.sync_service._get_known_message_keys", return_value=set()),
|
||||||
|
):
|
||||||
|
sync_run = sync_account(db, 1, dry_run=False)
|
||||||
|
|
||||||
|
imap.logout.assert_called_once()
|
||||||
|
smtp.quit.assert_called_once()
|
||||||
|
assert sync_run.status.value == "completed"
|
||||||
Reference in new issue
Block a user