Add network timeouts to prevent container hangs on unreachable servers

Without timeouts, smtplib.SMTP() / imaplib.IMAP4_SSL() / paramiko.Transport()
hang indefinitely when the remote server is down or firewall-dropped, blocking
the entire background thread and eventually freezing the app.

- SMTP: 30s connect/operation timeout
- IMAP: 30s connect/operation timeout
- SFTP (paramiko): 15s socket connect, 15s banner, 30s auth
- SMB and FTP already had timeouts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 14:03:07 +02:00
parent 4b9df132d7
commit cbdd296aae
2 changed files with 19 additions and 5 deletions
+8 -4
View File
@@ -40,14 +40,18 @@ def _connect_imap(settings: dict) -> imaplib.IMAP4_SSL | imaplib.IMAP4:
if use_ssl:
ctx = ssl.create_default_context()
conn = imaplib.IMAP4_SSL(server, port, ssl_context=ctx)
conn = imaplib.IMAP4_SSL(server, port, ssl_context=ctx, timeout=IMAP_TIMEOUT)
else:
conn = imaplib.IMAP4(server, port)
conn = imaplib.IMAP4(server, port, timeout=IMAP_TIMEOUT)
conn.login(settings["imap_username"], settings["imap_password"])
return conn
SMTP_TIMEOUT = 30 # seconds - prevents hangs when SMTP server is unreachable
IMAP_TIMEOUT = 30 # seconds
def _connect_smtp(settings: dict) -> smtplib.SMTP | smtplib.SMTP_SSL:
server = settings["smtp_server"]
port = int(settings.get("smtp_port", 587))
@@ -55,9 +59,9 @@ def _connect_smtp(settings: dict) -> smtplib.SMTP | smtplib.SMTP_SSL:
if mode == "ssl":
ctx = ssl.create_default_context()
conn = smtplib.SMTP_SSL(server, port, context=ctx)
conn = smtplib.SMTP_SSL(server, port, context=ctx, timeout=SMTP_TIMEOUT)
else:
conn = smtplib.SMTP(server, port)
conn = smtplib.SMTP(server, port, timeout=SMTP_TIMEOUT)
if mode == "starttls":
ctx = ssl.create_default_context()
conn.starttls(context=ctx)