Fix client.send() timeout new Request instance (#3116)

This commit is contained in:
Alex 2024-02-26 17:36:58 +01:00 committed by GitHub
parent df5345140e
commit 6d852d319a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 0 deletions

View File

@ -562,6 +562,15 @@ class BaseClient:
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):
"""
@ -911,6 +920,8 @@ class Client(BaseClient):
else follow_redirects
)
self._set_timeout(request)
auth = self._build_request_auth(request, auth)
response = self._send_handling_auth(
@ -1658,6 +1669,8 @@ class AsyncClient(BaseClient):
else follow_redirects
)
self._set_timeout(request)
auth = self._build_request_auth(request, auth)
response = await self._send_handling_auth(

View File

@ -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"))
)