overlay_builders: address code-review nits on retry helpers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
mwiegand 2026-05-11 22:48:28 +02:00
parent 13bd2e48f6
commit 6fc7f87943
No known key found for this signature in database
2 changed files with 11 additions and 7 deletions

View file

@ -10,11 +10,11 @@ from __future__ import annotations
import os import os
import subprocess import subprocess
import tempfile import tempfile
import time as _time import time
from pathlib import Path from pathlib import Path
from typing import Callable, Protocol from typing import Callable, Protocol
import requests as _requests import requests
from sqlalchemy import select from sqlalchemy import select
from l4d2host.paths import get_left4me_root from l4d2host.paths import get_left4me_root
@ -34,7 +34,8 @@ SCRIPT_SANDBOX_HELPER = "/usr/local/libexec/left4me/left4me-script-sandbox"
DISK_BUDGET_BYTES = 20 * 1024**3 DISK_BUDGET_BYTES = 20 * 1024**3
DOWNLOAD_RETRY_ATTEMPTS = 3 DOWNLOAD_RETRY_ATTEMPTS = 3
DOWNLOAD_RETRY_BACKOFF_SECONDS = (1.0, 2.0, 4.0) DOWNLOAD_RETRY_BACKOFF_SECONDS = (1.0, 2.0)
assert len(DOWNLOAD_RETRY_BACKOFF_SECONDS) == DOWNLOAD_RETRY_ATTEMPTS - 1
def _sleep_with_cancel( def _sleep_with_cancel(
@ -46,14 +47,14 @@ def _sleep_with_cancel(
"""Sleep up to `seconds`, returning early (True) if `should_cancel` becomes """Sleep up to `seconds`, returning early (True) if `should_cancel` becomes
True. Returns False on a full uninterrupted sleep. Polls every True. Returns False on a full uninterrupted sleep. Polls every
`poll_interval` seconds.""" `poll_interval` seconds."""
deadline = _time.monotonic() + seconds deadline = time.monotonic() + seconds
while True: while True:
if should_cancel(): if should_cancel():
return True return True
remaining = deadline - _time.monotonic() remaining = deadline - time.monotonic()
if remaining <= 0: if remaining <= 0:
return False return False
_time.sleep(min(poll_interval, remaining)) time.sleep(min(poll_interval, remaining))
def _download_with_retry( def _download_with_retry(
@ -73,7 +74,7 @@ def _download_with_retry(
return return
except InterruptedError: except InterruptedError:
raise raise
except (_requests.RequestException, OSError) as exc: except (requests.RequestException, OSError) as exc:
last_exc = exc last_exc = exc
if attempt == DOWNLOAD_RETRY_ATTEMPTS: if attempt == DOWNLOAD_RETRY_ATTEMPTS:
raise raise

View file

@ -475,6 +475,9 @@ def test_download_with_retry_exhausts_and_raises(env, tmp_path, monkeypatch):
meta, tmp_path / "cache", meta, tmp_path / "cache",
on_stderr=on_stderr, should_cancel=lambda: False, on_stderr=on_stderr, should_cancel=lambda: False,
) )
# Two stderr "attempt N/3 failed" lines for attempts 1 and 2; the final
# attempt re-raises without logging.
assert sum(1 for line in err if "attempt" in line and "failed" in line) == 2
def test_download_with_retry_propagates_interrupted(env, tmp_path, monkeypatch): def test_download_with_retry_propagates_interrupted(env, tmp_path, monkeypatch):