bounded_response.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Bounded reads of HTTP error response bodies.
  2. On a non-OK *streaming* response Hermes reads the body for a diagnostic (only ever shown truncated to
  3. a few hundred chars). A bare ``response.read()`` is unbounded two ways: arbitrarily large body
  4. (memory) or a body that stalls forever (hang). ``read_streaming_error_body`` caps bytes and enforces a
  5. hard wall-clock deadline; callers use the returned text instead of ``response.text`` (unbounded /
  6. raises after a partial stream read). ``httpx.iter_bytes()`` blocks *inside* the socket read, so the
  7. read runs on a daemon thread; on timeout we close the response (unblocking the read) and return the
  8. partial bytes. Used by the streaming error-body sites: native Gemini, Gemini Cloud Code, Antigravity.
  9. """
  10. from __future__ import annotations
  11. import logging
  12. import threading
  13. from typing import List
  14. import httpx
  15. logger = logging.getLogger(__name__)
  16. # Comfortably holds any real provider error envelope while rejecting pathological bodies.
  17. DEFAULT_ERROR_BODY_MAX_BYTES = 64 * 1024
  18. # Hard deadline for the whole read; past it the connection is closed and the partial bytes are kept.
  19. DEFAULT_ERROR_BODY_TIMEOUT_S = 10.0
  20. def read_streaming_error_body(
  21. response: httpx.Response,
  22. *,
  23. max_bytes: int = DEFAULT_ERROR_BODY_MAX_BYTES,
  24. timeout_s: float = DEFAULT_ERROR_BODY_TIMEOUT_S,
  25. ) -> str:
  26. """Read a non-OK streaming body with a byte cap and a hard deadline.
  27. Returns UTF-8 text (errors replaced) truncated to ``max_bytes``. Never raises: transport errors,
  28. stalls and oversize bodies yield best-effort partial text (or ""), so a read error can't mask the
  29. original failure.
  30. """
  31. chunks: list[bytes] = []
  32. state = {"truncated": False}
  33. done = threading.Event()
  34. def _drain() -> None:
  35. total = 0
  36. try:
  37. for chunk in response.iter_bytes():
  38. if not chunk:
  39. continue
  40. remaining = max_bytes - total
  41. if len(chunk) > remaining:
  42. if remaining > 0:
  43. chunks.append(chunk[:remaining])
  44. state["truncated"] = True
  45. break
  46. chunks.append(chunk)
  47. total += len(chunk)
  48. except Exception as exc:
  49. logger.debug("bounded error-body read failed: %s", exc)
  50. finally:
  51. done.set()
  52. threading.Thread(target=_drain, name="bounded-error-body-read", daemon=True).start()
  53. if not done.wait(timeout=timeout_s):
  54. logger.debug(
  55. "bounded error-body read: hard timeout after %.1fs (%d bytes so far)",
  56. timeout_s, sum(len(c) for c in chunks),
  57. )
  58. # Closing cancels any in-flight socket read so the worker unwinds. No join (daemon, may be blocked in C).
  59. try:
  60. response.close()
  61. except Exception:
  62. pass
  63. if state["truncated"]:
  64. logger.debug(
  65. "bounded error-body read: capped at %d bytes (max=%d)", sum(len(c) for c in chunks), max_bytes,
  66. )
  67. return b"".join(chunks).decode("utf-8", errors="replace")