Skip to content

The agent that grows with you

fix(tools): retry OSV disk-cache load after transient I/O

Christopherc7a87bb11071
diff --git a/tests/tools/test_osv_check.py b/tests/tools/test_osv_check.py
index 695f94214c0..d136602f02d 100644
--- a/tests/tools/test_osv_check.py
+++ b/tests/tools/test_osv_check.py
@@ -1,6 +1,9 @@
"""Tests for OSV malware check on MCP extension packages."""
import json
+import time
+from pathlib import Path
+
import pytest
from unittest.mock import patch, MagicMock
@@ -243,6 +246,44 @@ class TestCheckPackageForMalware:
assert "expiry" in data["entries"][key]
assert data["entries"][key]["result"] is None
+ def test_disk_cache_retries_after_transient_oserror(self, tmp_path, monkeypatch):
+ """A busy/unreadable cache file must not disable disk loads for the process."""
+ from tools import osv_check
+
+ monkeypatch.setenv("HERMES_HOME", str(tmp_path))
+ cache_file = tmp_path / "cache" / "osv_check.json"
+ cache_file.parent.mkdir(parents=True, exist_ok=True)
+ cache_file.write_text(
+ json.dumps({
+ "version": osv_check._DISK_CACHE_VERSION,
+ "entries": {
+ "PyPI|mcp-server-retry|": {
+ "expiry": time.time() + 3600,
+ "result": None,
+ }
+ },
+ }),
+ encoding="utf-8",
+ )
+
+ real_open = open
+ calls = {"n": 0}
+
+ def flaky_open(path, *args, **kwargs):
+ if Path(path) == cache_file:
+ calls["n"] += 1
+ if calls["n"] == 1:
+ raise OSError("resource temporarily unavailable")
+ return real_open(path, *args, **kwargs)
+
+ monkeypatch.setattr("builtins.open", flaky_open)
+ with osv_check._cache_lock:
+ osv_check._load_disk_cache()
+ assert osv_check._disk_cache_loaded is False
+ osv_check._load_disk_cache()
+ assert osv_check._disk_cache_loaded is True
+ assert ("PyPI", "mcp-server-retry", None) in osv_check._cache
+
class TestLiveOsvQuery:
"""Live integration test against the real OSV API. Skipped if offline."""
diff --git a/tools/osv_check.py b/tools/osv_check.py
index 16dd355c560..80a983054e1 100644
--- a/tools/osv_check.py
+++ b/tools/osv_check.py
@@ -80,18 +80,29 @@ def _load_disk_cache() -> None:
global _disk_cache_loaded
if _disk_cache_loaded:
return
- _disk_cache_loaded = True
path = _disk_cache_path()
if path is None:
+ _disk_cache_loaded = True
return
try:
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
+ except FileNotFoundError:
+ _disk_cache_loaded = True
+ return
+ except json.JSONDecodeError:
+ _disk_cache_loaded = True
+ return
+ except OSError:
+ # Transient I/O (file busy, brief permission flap). Retry next call.
+ return
except Exception:
+ _disk_cache_loaded = True
return
+ _disk_cache_loaded = True
if not isinstance(data, dict) or data.get("version") != _DISK_CACHE_VERSION:
return