Skip to content

Commit

Permalink
Fix client.send() timeout new Request instance (#3116)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgmin committed Feb 26, 2024
1 parent df53451 commit 6d852d3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions httpx/_client.py
Expand Up @@ -562,6 +562,15 @@ def _redirect_stream(

return request.stream

def _set_timeout(self, request: Request) -> None:
if "timeout" not in request.extensions:
timeout = (
self.timeout
if isinstance(self.timeout, UseClientDefault)
else Timeout(self.timeout)
)
request.extensions = dict(**request.extensions, timeout=timeout.as_dict())


class Client(BaseClient):
"""
Expand Down Expand Up @@ -911,6 +920,8 @@ def send(
else follow_redirects
)

self._set_timeout(request)

auth = self._build_request_auth(request, auth)

response = self._send_handling_auth(
Expand Down Expand Up @@ -1658,6 +1669,8 @@ async def send(
else follow_redirects
)

self._set_timeout(request)

auth = self._build_request_auth(request, auth)

response = await self._send_handling_auth(
Expand Down
11 changes: 11 additions & 0 deletions tests/test_timeouts.py
Expand Up @@ -42,3 +42,14 @@ async def test_pool_timeout(server):
with pytest.raises(httpx.PoolTimeout):
async with client.stream("GET", server.url):
await client.get(server.url)


@pytest.mark.anyio
async def test_async_client_new_request_send_timeout(server):
timeout = httpx.Timeout(1e-6)

async with httpx.AsyncClient(timeout=timeout) as client:
with pytest.raises(httpx.TimeoutException):
await client.send(
httpx.Request("GET", server.url.copy_with(path="/slow_response"))
)

0 comments on commit 6d852d3

Please sign in to comment.