diff -ruN a/python/vyos/progressbar.py b/python/vyos/progressbar.py --- a/python/vyos/progressbar.py 2026-11-13 12:22:17.884366543 +0000 +++ b/python/vyos/progressbar.py 2026-11-13 12:22:21.758238162 +0000 @@ -17,14 +17,29 @@ import os import signal import subprocess +import time from vyos.utils.io import is_dumb_terminal from vyos.utils.io import print_error +# Minimum interval between two redraws of the progressbar (seconds). +# Progress callbacks fire for every received chunk (HTTP/FTP/SFTP), +# i.e. tens of thousands of times per image download. Redrawing on each of +# them floods the TTY (and the SSH session behind it) with writes and burns +# CPU that is otherwise needed for packet forwarding. +REDRAW_INTERVAL = 0.1 + class Progressbar: def __init__(self, step=None): self.total = 0.0 self.step = step + self.col = 20 + # Last rendered state, used to skip redundant redraws. + self._last_state = None + self._last_draw = 0.0 + # Most recent (done, total) that was throttled and not yet drawn. + self._pending = None + self._finished = False # Silently ignore all calls if terminal capabilities are lacking. # This will also prevent the output from littering Ansible logs, # as `ansible.netcommon.network_cli' coaxes the terminal into believing @@ -42,13 +57,24 @@ return self def __exit__(self, exc_type, kexc_val, exc_tb): if not self._dumb: + # Draw the last throttled state so the bar doesn't freeze short of + # its real value, then terminate the line so it isn't overwritten. + if not self._finished: + if self._pending: + self._draw(*self._pending) + print_error() # Revert to the default SIGWINCH handler (ie nothing). signal.signal(signal.SIGWINCH, signal.SIG_DFL) # Reenable line wrapping. subprocess.run(['tput', 'smam'], check=False) def _update_cols(self): # `os.get_terminal_size()' is fast enough for our purposes. - self.col = max(os.get_terminal_size().columns - 15, 20) + try: + self.col = max(os.get_terminal_size().columns - 15, 20) + except OSError: + self.col = 20 + # Force a redraw with the new width on the next update. + self._last_state = None def increment(self): """ Stateful progressbar taking the step fraction at init and no input at @@ -67,11 +93,31 @@ Stateless progressbar taking no input at init and current progress with final size at callback (for SSH) """ - if done <= total and not self._dumb: - length = math.ceil(self.col * done / total) - percentage = str(math.ceil(100 * done / total)).rjust(3) - # Carriage return at the end will make sure the line will get overwritten. - print_error(f'[{length * "#"}{(self.col - length) * "_"}] {percentage}%', end='\r') - # Print a newline to make sure the full progressbar doesn't get overwritten by the next line. + if self._dumb or not total or done > total: + return if done == total: + self._draw(done, total) + # Print a newline to make sure the full progressbar doesn't get overwritten by the next line. print_error() + self._finished = True + return + length = math.ceil(self.col * done / total) + percentage = math.ceil(100 * done / total) + # Nothing visible changed - don't touch the terminal at all. + if (self.col, length, percentage) == self._last_state: + self._pending = None + return + # Throttle intermediate redraws (the first one always goes through). + if self._last_state is not None and \ + time.monotonic() - self._last_draw < REDRAW_INTERVAL: + self._pending = (done, total) + return + self._draw(done, total) + def _draw(self, done, total): + length = math.ceil(self.col * done / total) + percentage = math.ceil(100 * done / total) + self._last_state = (self.col, length, percentage) + self._last_draw = time.monotonic() + self._pending = None + # Carriage return at the end will make sure the line will get overwritten. + print_error(f'[{length * "#"}{(self.col - length) * "_"}] {str(percentage).rjust(3)}%', end='\r') diff -ruN a/python/vyos/remote.py b/python/vyos/remote.py --- a/python/vyos/remote.py 2026-11-13 12:22:17.886111891 +0000 +++ b/python/vyos/remote.py 2026-11-13 12:22:17.890436394 +0000 @@ -60,7 +60,7 @@ from vyos.base import Warning from vyos.defaults import directories -CHUNK_SIZE = 8192 +CHUNK_SIZE = 65536 def _decode(value):