Skip to content

Commit 2c715b9

Browse files
wackouFizzadar
authored andcommitted
Invalidate SFTP connection cache when disconnecting from SSH
This fixes a bug where we would keep a stale connection in the cache such as in the following example: ``` files.put(src='test.sh', dest='/tmp/test_before.sh', force=True) server.reboot(delay=20, interval=5, reboot_timeout=600) files.put(src='test.sh', dest='/tmp/spinner.sh', force=True) ``` The first `files.put` caches an SFTP connection that gets closed during the reboot, and the second `files.put` will try to use the memoized connection which is now invalid.
1 parent faa366e commit 2c715b9

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

pyinfra/api/host.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def disconnect(self):
406406
# Disconnect is an optional function for connectors if needed
407407
disconnect_func = getattr(self.connector, "disconnect", None)
408408
if disconnect_func:
409-
return disconnect_func()
409+
disconnect_func()
410410

411411
# TODO: consider whether this should be here!
412412
remove_any_sudo_askpass_file(self)

pyinfra/connectors/ssh.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,9 @@ def _connect(self) -> None:
264264
f"Host key for {e.hostname} does not match.",
265265
)
266266

267+
def disconnect(self) -> None:
268+
self.get_sftp_connection.cache.clear()
269+
267270
def run_shell_command(
268271
self,
269272
command: StringCommand,

0 commit comments

Comments
 (0)