From 28d1b0d0d09c7c92f25f1172695701d4c3da6fb4 Mon Sep 17 00:00:00 2001 From: Fuxino Date: Thu, 8 Oct 2026 20:45:07 +0200 Subject: [PATCH] Add a timeout to the SSH connection paramiko's connect had no timeout, so a server that doesn't answer made the program hang until the OS gave up (about 2 minutes). Give up after 15 seconds and exit with code 5. --- simple_backup/simple_backup.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/simple_backup/simple_backup.py b/simple_backup/simple_backup.py index 14b1f2e..ebde737 100755 --- a/simple_backup/simple_backup.py +++ b/simple_backup/simple_backup.py @@ -69,6 +69,9 @@ if journal: P = ParamSpec('P') R = TypeVar('R') +# Seconds to wait for the TCP connection to the SSH server +SSH_TIMEOUT = 15 + def timing(func: Callable[P, R]) -> Callable[P, R]: """Decorator to measure execution time of a function @@ -401,7 +404,7 @@ class Backup: # WarningPolicy emits a UserWarning for unknown host keys with warnings.catch_warnings(): warnings.simplefilter('error', UserWarning) - ssh.connect(self.ssh_host, username=self.ssh_user) + ssh.connect(self.ssh_host, username=self.ssh_user, timeout=SSH_TIMEOUT) return ssh except UserWarning: @@ -427,7 +430,7 @@ class Backup: pass try: - ssh.connect(self.ssh_host, username=self.ssh_user) + ssh.connect(self.ssh_host, username=self.ssh_user, timeout=SSH_TIMEOUT) return ssh except paramiko.SSHException: @@ -441,7 +444,7 @@ class Backup: password = getpass( f'{self.ssh_user}@{self.ssh_host}\'s password: ') - ssh.connect(self.ssh_host, username=self.ssh_user, + ssh.connect(self.ssh_host, username=self.ssh_user, timeout=SSH_TIMEOUT, password=password) self._password_auth = True @@ -465,7 +468,7 @@ class Backup: return None try: - ssh.connect(self.ssh_host, username=self.ssh_user, pkey=pkey) + ssh.connect(self.ssh_host, username=self.ssh_user, timeout=SSH_TIMEOUT, pkey=pkey) except paramiko.SSHException: logger.critical('SSH connection to server failed')