The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(read_extract): retry anydoc init after failure instead of sticky disable
Adolanium997a913a58dd
diff --git a/tests/tools/test_read_extract.py b/tests/tools/test_read_extract.pyindex b1e690635e6..4b656e2de6b 100644--- a/tests/tools/test_read_extract.py+++ b/tests/tools/test_read_extract.py@@ -15,6 +15,7 @@ import osimport tempfileimport unittestimport zipfile+from unittest import mockfrom tools.read_extract import (ExtractionError,@@ -164,6 +165,97 @@ class TestAnydocAbsent(unittest.TestCase):self.assertTrue(is_extractable_document("a.xlsx"))+class TestAnydocInitLifecycle(unittest.TestCase):+ """First-load lifecycle: one failed load must not disable extraction+ for the rest of the process, and concurrent first use must not race."""++ def setUp(self):+ from tools import read_extract++ self.rex = read_extract+ self._saved_module = read_extract._anydoc_module+ self._saved_failed_at = read_extract._anydoc_failed_at+ self._saved_retry = read_extract.ANYDOC_RETRY_SECONDS+ read_extract._anydoc_module = read_extract._ANYDOC_UNSET+ read_extract._anydoc_failed_at = None++ def tearDown(self):+ self.rex._anydoc_module = self._saved_module+ self.rex._anydoc_failed_at = self._saved_failed_at+ self.rex.ANYDOC_RETRY_SECONDS = self._saved_retry++ def test_successful_load_is_cached(self):+ fake = object()+ calls = []++ def fake_import(name):+ calls.append(name)+ return fake++ with mock.patch("importlib.import_module", side_effect=fake_import):+ self.assertIs(self.rex._anydoc(), fake)+ self.assertIs(self.rex._anydoc(), fake)+ self.assertEqual(calls, ["anydoc"])++ def test_failed_load_is_retried_after_cooldown(self):+ fake = object()+ calls = []++ def fake_import(name):+ calls.append(name)+ if len(calls) == 1:+ raise ImportError("boom")+ return fake++ self.rex.ANYDOC_RETRY_SECONDS = 0.0+ with mock.patch("importlib.import_module", side_effect=fake_import):+ self.assertIsNone(self.rex._anydoc())+ self.assertIs(self.rex._anydoc(), fake)+ self.assertEqual(calls, ["anydoc", "anydoc"])++ def test_failed_load_not_retried_within_cooldown(self):+ calls = []++ def fake_import(name):+ calls.append(name)+ raise ImportError("boom")++ self.rex.ANYDOC_RETRY_SECONDS = 3600.0+ with mock.patch("importlib.import_module", side_effect=fake_import):+ self.assertIsNone(self.rex._anydoc())+ self.assertIsNone(self.rex._anydoc())+ # One import attempt total, and the handle stays UNSET so a retry+ # remains possible once the cooldown expires.+ self.assertEqual(calls, ["anydoc"])+ self.assertIs(self.rex._anydoc_module, self.rex._ANYDOC_UNSET)++ def test_concurrent_first_load_imports_once(self):+ import threading++ fake = object()+ calls = []+ barrier = threading.Barrier(4)++ def fake_import(name):+ calls.append(name)+ return fake++ def worker(out):+ barrier.wait(5)+ out.append(self.rex._anydoc())++ with mock.patch("importlib.import_module", side_effect=fake_import):+ results = []+ threads = [threading.Thread(target=worker, args=(results,)) for _ in range(3)]+ for t in threads:+ t.start()+ barrier.wait(5)+ for t in threads:+ t.join(5)+ self.assertEqual(calls, ["anydoc"])+ self.assertEqual(results, [fake, fake, fake])++# ---------------------------------------------------------------------------# Notebooks (.ipynb) — #10733# ---------------------------------------------------------------------------diff --git a/tools/read_extract.py b/tools/read_extract.pyindex 502369d231a..1230e986ade 100644--- a/tools/read_extract.py+++ b/tools/read_extract.py@@ -15,6 +15,8 @@ from __future__ import annotationsimport importlibimport jsonimport posixpath+import threading+import timeimport zipfilefrom pathlib import Pathfrom typing import Any, Optional@@ -56,12 +58,32 @@ def _extension(path: str) -> str:_ANYDOC_UNSET = object()_anydoc_module: Any = _ANYDOC_UNSET+_anydoc_lock = threading.Lock()+# After a failed first load, wait this long before trying again. The attempt+# can shell out to pip, so retrying on every call would hammer the network+# in environments where the install can never succeed.+ANYDOC_RETRY_SECONDS = 300.0+_anydoc_failed_at: Optional[float] = Nonedef _anydoc() -> Optional[Any]:- """Lazily import the optional anydoc converter; None when unavailable."""- global _anydoc_module- if _anydoc_module is _ANYDOC_UNSET:+ """Lazily import the optional anydoc converter; None when unavailable.++ A failed load is retried after :data:`ANYDOC_RETRY_SECONDS` rather than+ disabling extraction for the rest of the process, so one transient+ failure (network blip, pip race) does not stick in long-lived workers.+ """+ global _anydoc_module, _anydoc_failed_at+ if _anydoc_module is not _ANYDOC_UNSET:+ return _anydoc_module+ with _anydoc_lock:+ if _anydoc_module is not _ANYDOC_UNSET:+ return _anydoc_module+ if (+ _anydoc_failed_at is not None+ and time.monotonic() - _anydoc_failed_at < ANYDOC_RETRY_SECONDS+ ):+ return Nonetry:from tools.lazy_deps import ensure as _lazy_ensure@@ -72,7 +94,8 @@ def _anydoc() -> Optional[Any]:try:_anydoc_module = importlib.import_module("anydoc")except Exception: # ImportError or a broken native binding- _anydoc_module = None+ _anydoc_failed_at = time.monotonic()+ return Nonereturn _anydoc_module # type: ignore[return-value]