]> git.ipfire.org Git - thirdparty/httpx.git/commitdiff
Fix client.send() timeout new Request instance (#3116)
authorAlex <8340441+alexgmin@users.noreply.github.com>
Mon, 26 Feb 2024 16:36:58 +0000 (17:36 +0100)
committerGitHub <noreply@github.com>
Mon, 26 Feb 2024 16:36:58 +0000 (16:36 +0000)
httpx/_client.py
tests/test_timeouts.py

index cf3b30626da4b113f6189ba6d16f3a3b672b1c3b..ba5decf85fd6dc34f11e842e95fcee18c16875d9 100644 (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(
index 09b25160e76cacbf0298a39104f23f734567df38..666cc8e376f31d4307137329f741f3c326689edf 100644 (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"))
+            )