fix(bench): download the logs that take longer than 4 s (#29220)

The logs were downloaded with process_ftp_reply() called in slices of 4.2 s, to send heartbeats between them and to detect a stall. pymavlink terminates the session when process_ftp_reply() times out, so any download longer than one slice was aborted at its first byte count, and reported as stalled at 0 bytes after 30 s: a 290 KB log over a serial link at 115200 baud, or a long log over a telemetry radio. The download is now driven in one call bounded by the budget, the idle detection of pymavlink detects a stall, and the heartbeats are sent from the progress callback.

Assisted-by: Claude:opus-5.5
This commit is contained in:
Benjamin Cabé authored and GitHub committed 2026-10-14 12:33:19 -06:00
1 parent 2114fe2f78
commit fb29b85b63
1 file changed
+28 -34
+28 -34
View File
@@ -1,9 +1,10 @@
"""MAVFTP helpers: directory listing, bounded file download, ULog constants.
Built on pymavlink.mavftp. cmd_get only sends OpenFileRO and returns; the
transfer itself is driven by process_ftp_reply, so ftp_download pumps it in
bounded slices with its own budget and stall detection: a transfer that
stops mid-file becomes an error naming the byte count, never a hang.
transfer itself is driven by process_ftp_reply, so ftp_download drives it
within its own budget, with the idle detection of pymavlink as the stall
detection: a transfer that stops mid-file becomes an error naming the byte
count, never a hang.
"""
import sys
@@ -45,17 +46,21 @@ def ftp_download(mav, ftp, remote, local, report):
cmd_get only sends OpenFileRO and returns; the transfer itself is driven
by process_ftp_reply, which loops recv_match -> __mavlink_packet (burst
read handling, temp-file writes) -> __idle_task until idle or timeout.
We use a completion callback so we know the burst read finished, and we
pump process_ftp_reply in bounded slices so a stalled burst becomes a
named FAIL instead of a hang. process_ftp_reply requires its timeout to
be greater than idle_detection_time (default 3.7s).
read handling, temp-file writes) -> __idle_task until the read completes,
the link is idle for idle_detection_time (default 3.7s), or the timeout.
It is driven in one call bounded by FTP_BUDGET: process_ftp_reply
terminates the session when it times out, so shorter slices would abort
any transfer that takes longer than one slice (a slow link, a big log).
A stalled burst read ends on the idle detection and becomes a named FAIL
with the byte count, never a hang. Heartbeats go out from the progress
callback, which runs on every burst read reply.
Returns (elapsed_seconds, None) on success, (None, error_string) on
failure, so the caller can branch on the error explicitly instead of
ever mixing a message string into arithmetic.
"""
done = {'ok': False}
next_hb = [0.0]
def on_complete(fh):
# fh is a BytesIO on success, or None if the session was terminated
@@ -71,34 +76,23 @@ def ftp_download(mav, ftp, remote, local, report):
except Exception as e: # noqa: BLE001 - surface write failures to the caller
report.info('local write failed: {}'.format(e))
ftp.cmd_get([remote], callback=on_complete)
start = time.monotonic()
last_bytes = 0
last_progress = start
next_hb = 0.0
# process_ftp_reply timeout must exceed idle_detection_time; use a short
# slice and loop so we can enforce our own budget and stall detection.
slice_timeout = max(4.0, ftp.ftp_settings.idle_detection_time + 0.5)
while not done['ok']:
def on_progress(_fraction):
now = time.monotonic()
if now > next_hb:
if now > next_hb[0]:
send_heartbeat(mav)
next_hb = now + 1.0
if now - start > FTP_BUDGET:
cur = ftp.read_total if ftp.read_total else last_bytes
next_hb[0] = now + 1.0
send_heartbeat(mav)
start = time.monotonic()
ftp.cmd_get([remote], callback=on_complete, progress_callback=on_progress)
ret = ftp.process_ftp_reply('OpenFileRO', timeout=FTP_BUDGET)
if not done['ok']:
elapsed = time.monotonic() - start
if elapsed >= FTP_BUDGET:
return None, 'MAVFTP transfer stalled at {} bytes (budget {:.0f}s)'.format(
cur, FTP_BUDGET)
# drive one slice of the FTP state machine
ftp.process_ftp_reply('OpenFileRO', timeout=slice_timeout)
cur = ftp.read_total
if cur > last_bytes:
last_bytes = cur
last_progress = now
elif now - last_progress > 30.0 and not done['ok']:
# no forward progress for 30s and callback never fired
return None, 'MAVFTP transfer stalled at {} bytes (no progress 30s)'.format(cur)
# if the session ended without completing, process_ftp_reply keeps
# returning immediately on idle; break out via the budget/stall checks
ftp.read_total, FTP_BUDGET)
return None, 'MAVFTP transfer stalled at {} bytes ({})'.format(
ftp.read_total, ret.error_code.name)
elapsed = time.monotonic() - start
return elapsed, None