Repository navigation
Conversation
There was a problem hiding this comment.
Code Review
This pull request implements read timeout handling for the Python SDK's HTTPX transport using Trio, preserves raw response headers (allowing multiple headers like Set-Cookie to be retained), and ensures HTTP streams are safely closed on failure or cancellation. The review feedback suggests improving robustness by safely handling cases where the timeout extension is None to avoid an AttributeError, and optimizing performance by bypassing trio.fail_after when the read timeout is infinite.
| def __init__(self, response: StreamedResponse, request: httpx.Request) -> None: | ||
| self._response = response | ||
| self._request = request | ||
| timeout = request.extensions.get("timeout", {}).get("read") | ||
| self._read_timeout = math.inf if timeout is None else timeout | ||
|
|
||
| async def __aiter__(self) -> AsyncIterator[bytes]: | ||
| async for chunk in self._response.iter_body(): | ||
| body = self._response.iter_body() | ||
| while True: | ||
| try: | ||
| with trio.fail_after(self._read_timeout): | ||
| chunk = await anext(body) | ||
| except StopAsyncIteration: | ||
| return | ||
| except trio.TooSlowError as err: | ||
| raise httpx.ReadTimeout("timed out reading the mesh response body", request=self._request) from err | ||
| yield chunk |
There was a problem hiding this comment.
There are two opportunities for improvement here:
- Defensive Programming / Robustness: If
request.extensions.get("timeout")is explicitly set toNone(rather than being absent), calling.get("read")on it will raise anAttributeError. We should safely check if the timeout extension is a dictionary before calling.get()on it. - Performance / Efficiency: Wrapping every single chunk read in a
trio.fail_aftercontext manager when there is no timeout (i.e.,self._read_timeoutismath.inf) adds unnecessary overhead. We can bypasstrio.fail_afterentirely when the timeout is infinite.
def __init__(self, response: StreamedResponse, request: httpx.Request) -> None:
self._response = response
self._request = request
timeout_ext = request.extensions.get("timeout")
timeout = timeout_ext.get("read") if isinstance(timeout_ext, dict) else None
self._read_timeout = math.inf if timeout is None else timeout
async def __aiter__(self) -> AsyncIterator[bytes]:
body = self._response.iter_body()
while True:
try:
if self._read_timeout == math.inf:
chunk = await anext(body)
else:
with trio.fail_after(self._read_timeout):
chunk = await anext(body)
except StopAsyncIteration:
return
except trio.TooSlowError as err:
raise httpx.ReadTimeout("timed out reading the mesh response body", request=self._request) from err
yield chunkThere was a problem hiding this comment.
The first point is already in the PR: 211253d (pushed a minute after this review) reads the read value only when the timeout extension is a dict, and test_httpx_read_timeout.py covers an explicitly unset extension.
I'd rather not take the second. trio.fail_after(math.inf) opens a cancel scope with an infinite deadline; trio does not put an infinite deadline in its deadline queue, so entering it costs a few attribute writes, which is nothing next to awaiting a chunk from a libp2p stream. A separate branch for the infinite case would give the loop two read paths to keep in step, for no measurable gain.
cf3a59c to
211253d
Compare
Three independently reproduced Python SDK HTTP transport defects.
Each commit includes a focused regression. All 173 Python SDK tests passed on Linux with Python 3.12.