From ad5ab560b4aae94a8c6cd5e78a19e127f6a2821c Mon Sep 17 00:00:00 2001 From: adeci Date: Sat, 7 Jun 2025 04:39:33 -0400 Subject: [PATCH] bugfix for persistent loginctl sessions --- pkgs/clan-cli/clan_lib/ssh/remote.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/pkgs/clan-cli/clan_lib/ssh/remote.py b/pkgs/clan-cli/clan_lib/ssh/remote.py index 6f9cc677e..0517d1901 100644 --- a/pkgs/clan-cli/clan_lib/ssh/remote.py +++ b/pkgs/clan-cli/clan_lib/ssh/remote.py @@ -111,7 +111,7 @@ class Remote: ): directory = "/tmp/" with TemporaryDirectory(prefix="clan-ssh", dir=directory) as temp_dir: - yield Remote( + remote = Remote( address=self.address, user=self.user, command_prefix=self.command_prefix, @@ -126,6 +126,25 @@ class Remote: _control_path_dir=Path(temp_dir), _askpass_path=self._askpass_path, ) + try: + yield remote + finally: + # Terminate the SSH master connection + socket_path = Path(temp_dir) / "socket" + if socket_path.exists(): + try: + exit_cmd = [ + "ssh", + "-o", + f"ControlPath={socket_path}", + "-O", + "exit", + ] + exit_cmd.append(remote.target) + subprocess.run(exit_cmd, capture_output=True, timeout=5) + except (subprocess.TimeoutExpired, subprocess.CalledProcessError): + # If exit fails still try to stop the master connection + pass @contextmanager def become_root(self) -> Iterator["Remote"]: