Skip to content

Commit 277cb6f

Browse files
committed
Fix retry handling on SFTP upload failures
1 parent 07a1d5a commit 277cb6f

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

pyinfra/connectors/ssh.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -471,16 +471,21 @@ def get_file(
471471

472472
def _put_file(host: "Host", filename_or_io, remote_location):
473473
attempts = 1
474+
last_e = None
474475

475-
try:
476-
with get_file_io(filename_or_io) as file_io:
477-
sftp = _get_sftp_connection(host)
478-
sftp.putfo(file_io, remote_location)
479-
except OSError as e:
480-
if attempts > 3:
481-
raise
482-
logger.warning(f"Failed to upload file, retrying: {e}")
483-
attempts += 1
476+
while attempts < 4:
477+
try:
478+
with get_file_io(filename_or_io) as file_io:
479+
sftp = _get_sftp_connection(host)
480+
sftp.putfo(file_io, remote_location)
481+
return
482+
except OSError as e:
483+
logger.warning(f"Failed to upload file, retrying: {e}")
484+
attempts += 1
485+
last_e = e
486+
487+
if last_e is not None:
488+
raise last_e
484489

485490

486491
def put_file(

0 commit comments

Comments
 (0)