The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(models-dev): an abandoned download keeps its slot until it ends
Tekniumb3c2c35e914a
diff --git a/agent/models_dev.py b/agent/models_dev.pyindex a94c57477fe..9968beef2f0 100644--- a/agent/models_dev.py+++ b/agent/models_dev.py@@ -35,6 +35,7 @@ _models_dev_retry_after: float = 0_models_dev_fetch_lock = threading.Lock()_models_dev_refresh_lock = threading.Lock()_models_dev_refresh_in_flight = False+_models_dev_download_slot = threading.Lock() # held by the one live download, abandoned or not@dataclass@@ -349,19 +350,34 @@ def _fetch_models_dev_from_network(*, conditional: bool = False) -> tuple[dict[sif conditional and (etag := _load_etag()):headers["If-None-Match"] = etagurl = _get_models_dev_url()+ # A download abandoned at the deadline keeps its thread and socket until the server stops; it+ # owns this slot until then, so a later attempt (force_refresh skips the backoff) fails fast+ # instead of stacking a second transfer on the same slow link.+ if not _models_dev_download_slot.acquire(blocking=False):+ raise RuntimeError("an abandoned models.dev download is still running")+ started = threading.Event()def download() -> tuple[int, Any, str]:- # (connect, read) bounds each socket op, not the download: a trickling body never trips it.- response = requests.get(url, headers=headers, timeout=(5, 10))- if response.status_code == 304:- return 304, None, ""- response.raise_for_status()- return response.status_code, response.json(), response.headers.get("ETag", "")+ started.set()+ try:+ # (connect, read) bounds each socket op, not the download: a trickling body never trips it.+ response = requests.get(url, headers=headers, timeout=(5, 10))+ if response.status_code == 304:+ return 304, None, ""+ response.raise_for_status()+ return response.status_code, response.json(), response.headers.get("ETag", "")+ finally:+ _models_dev_download_slot.release()# The cold-cache fetch runs inside a caller's request (a fresh home's first session.create# reaches it through tool availability), so the whole exchange, DNS included, gets one# wall-clock cap; on expiry the caller falls back to the stale/empty cache and the backoff.- bounded = run_bounded_sync(download, _MODELS_DEV_FETCH_DEADLINE, label="models.dev fetch")+ try:+ bounded = run_bounded_sync(download, _MODELS_DEV_FETCH_DEADLINE, label="models.dev fetch")+ except BaseException:+ if not started.is_set(): # the worker never ran (thread start failed), so the slot is ours+ _models_dev_download_slot.release()+ raiseif bounded.timed_out:raise DeadlineExpired("models.dev fetch", _MODELS_DEV_FETCH_DEADLINE)status, data, etag = bounded.valuediff --git a/tests/agent/test_models_dev_fetch_deadline.py b/tests/agent/test_models_dev_fetch_deadline.pyindex 1afb1af9cb8..a7870da77e4 100644--- a/tests/agent/test_models_dev_fetch_deadline.py+++ b/tests/agent/test_models_dev_fetch_deadline.py@@ -23,7 +23,7 @@ class _Trickle(BaseHTTPRequestHandler):self.send_header("Content-Type", "application/json")self.send_header("Content-Length", "1000000")self.end_headers()- for _ in range(40): # ~4 s of one byte per 0.1 s: far under any per-read timeout+ for _ in range(80): # ~8 s of one byte per 0.1 s: far under any per-read timeoutself.wfile.write(b" ")self.wfile.flush()if self.server.stop.wait(0.1):@@ -43,6 +43,9 @@ def trickling_registry(monkeypatch):monkeypatch.setattr(md, name, value)yield serverserver.stop.set()+ # The abandoned download ends once the body stops; it must hand its slot back.+ assert md._models_dev_download_slot.acquire(timeout=10)+ md._models_dev_download_slot.release()server.shutdown()server.server_close()@@ -52,9 +55,9 @@ def test_cold_fetch_from_a_trickling_registry_returns_within_the_deadline(tricklstarted = time.monotonic()assert md.fetch_models_dev() == {}assert time.monotonic() - started < 2.0- # The failure arms the backoff, so the next caller does not wait again.- started = time.monotonic()+ # The failure arms the backoff, and a forced refresh (which skips the backoff) does not stack a+ # second transfer while the abandoned one is still trickling.assert md.fetch_models_dev() == {}- assert time.monotonic() - started < 0.2+ assert md.fetch_models_dev(force_refresh=True) == {}assert trickling_registry.gets == 1