diff --git a/src/unisync/synchroniser.py b/src/unisync/synchroniser.py index 138d356..6f3e911 100644 --- a/src/unisync/synchroniser.py +++ b/src/unisync/synchroniser.py @@ -123,15 +123,15 @@ class Synchroniser: f"{self.remote_user}@{self.remote_ip}", "-p", str(self.remote_port) ] - master_ssh = subprocess.Popen(command) - # TODO: Raise an exception instead of changing the return value - try: - ret_code = master_ssh.wait(timeout=connection_timeout) - except subprocess.TimeoutExpired as e: - print("Time to login expired", file=sys.stderr) - raise e - except KeyboardInterrupt as e: - raise e + with subprocess.Popen(command) as master_ssh: + # TODO: Raise an exception instead of changing the return value + try: + ret_code = master_ssh.wait(timeout=connection_timeout) + except subprocess.TimeoutExpired as e: + print("Time to login expired", file=sys.stderr) + raise e + except KeyboardInterrupt as e: + raise e if ret_code != 0: print("Login to remote failed", file=sys.stderr) @@ -151,8 +151,10 @@ class Synchroniser: f"{self.remote_user}@{self.remote_ip}", "-p", str(self.remote_port) ] - close = subprocess.Popen(command) - return close.wait() + with subprocess.Popen(command) as close: + retval = close.wait() + + return retval def sync_files(self, paths:list, force:bool=False) -> None: """Synchronises the files. @@ -254,8 +256,10 @@ class Synchroniser: for arg in other: command.append(arg) - proc = subprocess.Popen(command) - ret_code = proc.wait() + with subprocess.Popen(command) as proc: + ret_code = proc.wait() + + # See unison manual section 6.11 if ret_code == 3: raise FatalSyncError("Synchronisation could not be completed")