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:
+11
-1
@@ -197,9 +197,19 @@ class _PlainFtpAdapter(_FtpAdapter):
|
||||
# SFTP (paramiko)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
SFTP_CONNECT_TIMEOUT = 15 # seconds
|
||||
SFTP_BANNER_TIMEOUT = 15
|
||||
SFTP_AUTH_TIMEOUT = 30
|
||||
|
||||
|
||||
class _SftpAdapter(_FtpAdapter):
|
||||
def __init__(self, server: str, port: int, username: str, password: str):
|
||||
self.transport = paramiko.Transport((server, port))
|
||||
# Build socket with timeout to prevent hangs on unreachable hosts
|
||||
import socket as _socket
|
||||
sock = _socket.create_connection((server, port), timeout=SFTP_CONNECT_TIMEOUT)
|
||||
self.transport = paramiko.Transport(sock)
|
||||
self.transport.banner_timeout = SFTP_BANNER_TIMEOUT
|
||||
self.transport.auth_timeout = SFTP_AUTH_TIMEOUT
|
||||
self.transport.connect(username=username, password=password)
|
||||
self.sftp = paramiko.SFTPClient.from_transport(self.transport)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user