Repository navigation
httpcore.ReadTimeout not mapped to httpx.ReadTimeout #1188
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers
Description
I'm running the following to perform requests and catch httpx.RequestError exceptions:
async def async_get_url(client, url, retry=True):
async with semaphore.get():
try:
response = await client.get(url)
response.raise_for_status()
result = handle_reponse(reponse)
except httpx.RequestError as exc:
if retry:
print(f"Retrying {url} ...")
result = await async_get_url(
client, url, retry=False
)
else:
result = ...
except httpx.HTTPStatusError as exc:
result = ...
return result
At some point it failed with a httpcore._exceptions.ReadTimeout exception:
Traceback (most recent call last):
File "/home/user/main.py", line 351, in <module>
asyncio.run(run())
File "/usr/lib64/python3.8/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/lib64/python3.8/asyncio/base_events.py", line 616, in run_until_complete
return future.result()
File "/home/user/main.py", line 317, in run
await f
File "/usr/lib64/python3.8/asyncio/tasks.py", line 608, in _wait_for_one
return f.result() # May raise f.exception().
File "/home/user/main.py", line 173, in get_updates
item_api_response = await f
File "/usr/lib64/python3.8/asyncio/tasks.py", line 608, in _wait_for_one
return f.result() # May raise f.exception().
File "/home/user/main.py", line 53, in async_get_url
response = await client.get(url)
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_client.py", line 1292, in get
return await self.request(
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_client.py", line 1143, in request
response = await self.send(
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_client.py", line 1167, in send
await response.aread()
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_models.py", line 958, in aread
self._content = b"".join([part async for part in self.aiter_bytes()])
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_models.py", line 958, in <listcomp>
self._content = b"".join([part async for part in self.aiter_bytes()])
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_models.py", line 969, in aiter_bytes
async for chunk in self.aiter_raw():
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpx/_models.py", line 1002, in aiter_raw
async for part in self._raw_stream:
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_async/connection_pool.py", line 50, in __aiter__
async for chunk in self.stream:
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_bytestreams.py", line 72, in __aiter__
async for chunk in self._aiterator:
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_async/http11.py", line 143, in _receive_response_data
event = await self._receive_event(timeout)
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_async/http11.py", line 160, in _receive_event
data = await self.socket.read(self.READ_NUM_BYTES, timeout)
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_backends/asyncio.py", line 139, in read
return data
File "/usr/lib64/python3.8/contextlib.py", line 131, in __exit__
self.gen.throw(type, value, traceback)
File "/home/user/.pyenv/versions/venv/lib64/python3.8/site-packages/httpcore/_exceptions.py", line 12, in map_exceptions
raise to_exc(exc) from None
httpcore._exceptions.ReadTimeout
According to:
Lines 360 to 363 in 8c7729e
| HTTPCORE_EXC_MAP = { | |
| httpcore.TimeoutException: TimeoutException, | |
| httpcore.ConnectTimeout: ConnectTimeout, | |
| httpcore.ReadTimeout: ReadTimeout, |
The
httpcore.ReadTimeout exception should have been mapped to httpx.ReadTimeout and therefore catched appropriately with except httpx.RequestError as exc, but that doesn't happen.
Environment
- OS: Linux
- Python version: 3.8.5
- HTTPX version: 0.14.0
- HTTPCore version: 0.10.0
- Async environment: asyncio
- HTTP proxy: no
- Custom certificates: no
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workinggood first issueGood for newcomersGood for newcomers
Activity
j178 commentedon Aug 17, 2020
Thanks for raising this!
Currently we only mapped exceptions happend in
Client._send_single_request, your exception occured inClient.sendwhile reading the response, thus not being mapped.I think we should add
map_exceptionstoResponse.iter_raw()andResponse.aiter_raw()as well?lovelydinosaur commentedon Aug 17, 2020
Good catch, yup. 👍