Skip to content

The agent that grows with you

fix(photon): `photon status` routing row gives up after 5 s on an unreachable API

Tekniumce02e4686d73
diff --git a/plugins/platforms/photon/auth.py b/plugins/platforms/photon/auth.py
index 06f5d84a63c..9727562a855 100644
--- a/plugins/platforms/photon/auth.py
+++ b/plugins/platforms/photon/auth.py
@@ -550,18 +550,20 @@ def _normalize_phone(phone: str) -> str:
return re.sub(r"[^\d+]", "", phone or "")
-def list_users(project_id: str, project_secret: str) -> list[dict[str, Any]]:
+def list_users(project_id: str, project_secret: str, *, timeout: float = 30.0) -> list[dict[str, Any]]:
"""GET Spectrum Cloud ``/projects/{id}/users/`` → ``SpectrumUser[]``."""
_require_httpx()
url = f"{_spectrum_host()}/projects/{project_id}/users/"
- resp = httpx.get(url, headers=_basic(project_id, project_secret), timeout=30.0)
+ resp = httpx.get(url, headers=_basic(project_id, project_secret), timeout=timeout)
_raise_for_status(resp, "list-users")
return _unwrap_list(resp.json())
-def find_user_by_phone(project_id: str, project_secret: str, phone_number: str) -> Optional[dict[str, Any]]:
+def find_user_by_phone(
+ project_id: str, project_secret: str, phone_number: str, *, timeout: float = 30.0,
+) -> Optional[dict[str, Any]]:
"""Existing Spectrum user with the given phone number (the routable one), or None."""
- return find_routable_user(list_users(project_id, project_secret), phone_number)
+ return find_routable_user(list_users(project_id, project_secret, timeout=timeout), phone_number)
def create_user(
@@ -752,6 +754,9 @@ def print_credential_summary(emit: Any = print) -> None:
emit("\n".join(rows))
+_STATUS_TIMEOUT_S = 5.0 # `photon status` stays quick when the API is unreachable
+
+
def print_routing_status(emit: Any = print) -> None:
"""Live opt-in row for ``status``: a registered number that never texted its assigned line
looks configured everywhere but never routes. Verified against the API, not local markers;
@@ -761,9 +766,9 @@ def print_routing_status(emit: Any = print) -> None:
if not (phone and sid and sec):
return
try:
- user = find_user_by_phone(sid, sec, phone)
+ user = find_user_by_phone(sid, sec, phone, timeout=_STATUS_TIMEOUT_S)
except (httpx.HTTPError, RuntimeError, ValueError) as e:
- emit(f" routing : ? could not verify ({e})")
+ emit(f" routing : ? could not verify ({str(e) or type(e).__name__})")
return
if user_opted_in(user):
emit(" routing : ✓ opted in (verified live)")
diff --git a/tests/plugins/platforms/photon/test_auth.py b/tests/plugins/platforms/photon/test_auth.py
index acc64e1a82d..8cabee9c9a5 100644
--- a/tests/plugins/platforms/photon/test_auth.py
+++ b/tests/plugins/platforms/photon/test_auth.py
@@ -380,6 +380,24 @@ def test_register_user_if_absent_reuses_opted_in_duplicate(
assert created is False and user["id"] == "live"
+def test_print_routing_status_uses_short_timeout(monkeypatch: pytest.MonkeyPatch) -> None:
+ """`photon status` must not stall ~30 s on a black-holed API: the live row uses a short
+ timeout and reports "could not verify" instead of hanging or raising."""
+ seen: list = []
+
+ def timing_out_get(url: str, **kwargs: Any) -> _FakeResponse:
+ seen.append(kwargs.get("timeout"))
+ raise photon_auth.httpx.ReadTimeout("")
+
+ monkeypatch.setattr(photon_auth, "load_user_numbers", lambda: ("+15551234567", "+16282679185"))
+ monkeypatch.setattr(photon_auth, "load_project_credentials", lambda: ("proj", "secret"))
+ monkeypatch.setattr(photon_auth.httpx, "get", timing_out_get)
+ lines: list = []
+ photon_auth.print_routing_status(lines.append)
+ assert seen and seen[0] <= 5.0
+ assert lines == [" routing : ? could not verify (ReadTimeout)"]
+
+
# ---------------------------------------------------------------------------
# Lines (assigned number)