pydantic / pydantic/httpx2

Random httpx.ConnectError

Open
#836 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
78
Avg merge
8h 59m
Merged PRs (30d)
24

Description

Originally opened by @psykokwak-com on 2024-08-02 12:01:05 in encode/httpx

httpx version:

0.27.0

Current Behavior:

Having random "httpx.ConnectError"
Just have to retry again to make it work (or not)
I tried with curl and browser (chrome) and it works every time.

Expected Behavior:

Connection should work

Steps To Reproduce:
import httpx
import asyncio

async def fetch(url):
    async with httpx.AsyncClient() as client:
        response = await client.get(url)
        return response

async def main():
    url = "https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe"  # Replace with your target URL
    response = await fetch(url)

    print("Status Code:", response.status_code)
    print("Response Body:", response.text)


asyncio.run(main())
Anything else:

When it works :

DEBUG [2024-08-02 13:52:27] asyncio - Using proactor: IocpProactor
DEBUG [2024-08-02 13:52:27] httpx - load_ssl_context verify=True cert=None trust_env=True http2=False
DEBUG [2024-08-02 13:52:27] httpx - load_verify_locations cafile='C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\certifi\cacert.pem'
DEBUG [2024-08-02 13:52:27] httpcore.connection - connect_tcp.started host='api.telegram.org' port=443 local_address=None timeout=5.0 socket_options=None
DEBUG [2024-08-02 13:52:27] httpcore.connection - connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x000002E13E3FB460>
DEBUG [2024-08-02 13:52:27] httpcore.connection - start_tls.started ssl_context=<ssl.SSLContext object at 0x000002E13E41CFC0> server_hostname='api.telegram.org' timeout=5.0
DEBUG [2024-08-02 13:52:27] httpcore.connection - start_tls.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x000002E13E4780D0>
DEBUG [2024-08-02 13:52:27] httpcore.http11 - send_request_headers.started request=<Request [b'GET']>
DEBUG [2024-08-02 13:52:27] httpcore.http11 - send_request_headers.complete
DEBUG [2024-08-02 13:52:27] httpcore.http11 - send_request_body.started request=<Request [b'GET']>
DEBUG [2024-08-02 13:52:27] httpcore.http11 - send_request_body.complete
DEBUG [2024-08-02 13:52:27] httpcore.http11 - receive_response_headers.started request=<Request [b'GET']>
DEBUG [2024-08-02 13:52:27] httpcore.http11 - receive_response_headers.complete return_value=(b'HTTP/1.1', 401, b'Unauthorized', [(b'Server', b'nginx/1.18.0'), (b'Date', b'Fri, 02 Aug 2024 11:52:27 GMT'), (b'Content-Type', b'application/json'), (b'Content-Length', b'58'), (b'Connection', b'keep-alive'), (b'Strict-Transport-Security', b'max-age=31536000; includeSubDomains; preload'), (b'Access-Control-Allow-Origin', b'*'), (b'Access-Control-Expose-Headers', b'Content-Length,Content-Type,Date,Server,Connection')])
INFO [2024-08-02 13:52:27] httpx - HTTP Request: GET https://api.telegram.org/bot123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11/getMe "HTTP/1.1 401 Unauthorized"
DEBUG [2024-08-02 13:52:27] httpcore.http11 - receive_response_body.started request=<Request [b'GET']>
DEBUG [2024-08-02 13:52:27] httpcore.http11 - receive_response_body.complete
DEBUG [2024-08-02 13:52:27] httpcore.http11 - response_closed.started
DEBUG [2024-08-02 13:52:27] httpcore.http11 - response_closed.complete
DEBUG [2024-08-02 13:52:27] httpcore.connection - close.started
DEBUG [2024-08-02 13:52:27] httpcore.connection - close.complete
Status Code: 401
Response Body: {"ok":false,"error_code":401,"description":"Unauthorized"}

When it does not work :

DEBUG [2024-08-02 13:49:43] asyncio - Using proactor: IocpProactor
DEBUG [2024-08-02 13:49:43] httpx - load_ssl_context verify=True cert=None trust_env=True http2=False
DEBUG [2024-08-02 13:49:43] httpx - load_verify_locations cafile='C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\certifi\cacert.pem'
DEBUG [2024-08-02 13:49:43] httpcore.connection - connect_tcp.started host='api.telegram.org' port=443 local_address=None timeout=5.0 socket_options=None
DEBUG [2024-08-02 13:49:43] httpcore.connection - connect_tcp.complete return_value=<httpcore._backends.anyio.AnyIOStream object at 0x00000216EC62F400>
DEBUG [2024-08-02 13:49:43] httpcore.connection - start_tls.started ssl_context=<ssl.SSLContext object at 0x00000216EC64CFC0> server_hostname='api.telegram.org' timeout=5.0
DEBUG [2024-08-02 13:49:43] httpcore.connection - start_tls.failed exception=ConnectError(BrokenResourceError())
Traceback (most recent call last):
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_transports\default.py", line 69, in map_httpcore_exceptions
yield
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_transports\default.py", line 373, in handle_async_request
resp = await self._pool.handle_async_request(req)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_async\connection_pool.py", line 216, in handle_async_request
raise exc from None
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_async\connection_pool.py", line 196, in handle_async_request
response = await connection.handle_async_request(
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_async\connection.py", line 99, in handle_async_request
raise exc
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_async\connection.py", line 76, in handle_async_request
stream = await self._connect(request)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_async\connection.py", line 154, in _connect
stream = await stream.start_tls(**kwargs)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_backends\anyio.py", line 66, in start_tls
with map_exceptions(exc_map):
File "C:\Users\Jyce\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 153, in exit
self.gen.throw(typ, value, traceback)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpcore_exceptions.py", line 14, in map_exceptions
raise to_exc(exc) from exc
httpcore.ConnectError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\Jyce\Desktop\MyProject\llama_test.py", line 443, in
asyncio.run(main())
File "C:\Users\Jyce\AppData\Local\Programs\Python\Python310\lib\asyncio\runners.py", line 44, in run
return loop.run_until_complete(main)
File "C:\Users\Jyce\AppData\Local\Programs\Python\Python310\lib\asyncio\base_events.py", line 646, in run_until_complete
return future.result()
File "C:\Users\Jyce\Desktop\MyProject\llama_test.py", line 437, in main
response = await fetch(url)
File "C:\Users\Jyce\Desktop\MyProject\llama_test.py", line 432, in fetch
response = await client.get(url)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1801, in get
return await self.request(
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1574, in request
return await self.send(request, auth=auth, follow_redirects=follow_redirects)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1661, in send
response = await self._send_handling_auth(
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1689, in _send_handling_auth
response = await self._send_handling_redirects(
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1726, in _send_handling_redirects
response = await self._send_single_request(request)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_client.py", line 1763, in _send_single_request
response = await transport.handle_async_request(request)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_transports\default.py", line 372, in handle_async_request
with map_httpcore_exceptions():
File "C:\Users\Jyce\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 153, in exit
self.gen.throw(typ, value, traceback)
File "C:\Users\Jyce\Desktop\MyProject\venv\lib\site-packages\httpx_transports\default.py", line 86, in map_httpcore_exceptions
raise mapped_exc(message) from exc
httpx.ConnectError

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the failing example and compare its successful and failed logs, then trace the reported path through httpx/_transports/default.py, httpcore/_async/connection.py, and httpcore/_backends/anyio.py. Reproduce the intermittent TLS failure and determine a specific, repeatable cause before defining a fix and regression coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.