jawah / niquests

“Safest, Fastest, Easiest, and Most advanced” Python HTTP Client. Production Ready! Drop-in replacement for Requests. HTTP/1.1, HTTP/2, and HTTP/3 supported. With WebSocket!
https://niquests.readthedocs.io/en/latest/
Apache License 2.0
1.06k stars 23 forks source link

AttributeError: 'NoneType' object has no attribute '_get_extra_info' #184

Open oussama-gourari opened 4 hours ago

oussama-gourari commented 4 hours ago

Switched from aiohttp to niquests, while using AsyncSession sometimes I get this error AttributeError: 'NoneType' object has no attribute '_get_extra_info', the requests that throw this error will work fine if repeated later for some reason:

import asyncio
from http import HTTPMethod, HTTPStatus
from platform import platform, python_version
from urllib3.util import Retry

from niquests import AsyncSession

from config import CONTACT_URL
from const import NAME, VERSION

REQUESTS_USER_AGENT = (
    f"{NAME}/{VERSION} ({platform(terse=True)}; "
    f"Python-{python_version()}; +{CONTACT_URL})"
)
REQUESTS_TIMEOUT = 60  # In seconds.
HTTP_RETRY_CODES = (
    HTTPStatus.INTERNAL_SERVER_ERROR,
    HTTPStatus.BAD_GATEWAY,
    HTTPStatus.SERVICE_UNAVAILABLE,
    HTTPStatus.GATEWAY_TIMEOUT,
    520,  # Cloudflare error.
)

session = AsyncSession(
    disable_http3=True,
    retries=Retry(
        total=6,
        backoff_factor=1,
        status_forcelist=HTTP_RETRY_CODES,
        raise_on_status=False,
    )
)
session.headers.update({"User-Agent": REQUESTS_USER_AGENT})

async def req():
    response = await session.request(
        HTTPMethod.HEAD,
        "https://d15f34w2p8l1cc.cloudfront.net/hearthstone/e472633a76dd179a9d7d3b685c20470becce3ddf6338e5075e2b33b1d6669378.png",
        timeout=REQUESTS_TIMEOUT,
    )

asyncio.run(req())
      |     ^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\niquests\_async.py", line 856, in request
      |     return await self.send(prep, **send_kwargs)
      |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\niquests\_async.py", line 482, in send
      |     r = await adapter.send(request, **kwargs)
      |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\niquests\adapters.py", line 2038, in send
      |     resp_or_promise = await conn.urlopen(  # type: ignore[call-overload,misc]
      |                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      |     ...<18 lines>...
      |     )
      |     ^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_async\connectionpool.py", line 1817, in urlopen
      |     await conn.close()
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\_async\connection.py", line 294, in close
      |     await super().close()
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\backend\_async\hface.py", line 1652, in close
      |     await self.sock.wait_for_close()
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\contrib\ssa\__init__.py", line 102, in wait_for_close
      |     is_ssl = self._writer.get_extra_info("ssl_object") is not None
      |              ~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\asyncio\streams.py", line 361, in get_extra_info
      |     return self._transport.get_extra_info(name, default)
      |            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^
      |   File "C:\Users\Utilisateur\AppData\Local\Programs\Python\Python313\Lib\asyncio\sslproto.py", line 95, in get_extra_info
      |     return self._ssl_protocol._get_extra_info(name, default)
      |            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      | AttributeError: 'NoneType' object has no attribute '_get_extra_info'

System Information

$ python -m niquests.help
{
  "charset_normalizer": {
    "version": "3.4.0"
  },
  "http1": {
    "h11": "0.14.0"
  },
  "http2": {
    "jh2": "5.0.4"
  },
  "http3": {
    "enabled": true,
    "qh3": "1.2.1"
  },
  "idna": {
    "version": "3.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.13.0"
  },
  "niquests": {
    "version": "3.11.1"
  },
  "ocsp": {
    "enabled": true
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "system_ssl": {
    "version": "300000f0"
  },
  "urllib3.future": {
    "cohabitation_version": null,
    "version": "2.11.911"
  },
  "wassima": {
    "certifi_fallback": false,
    "enabled": true,
    "version": "1.1.5"
  }
}
Ousret commented 2 hours ago

A fix is available, the preview for it is hosted on our git.

pip install "git+https://github.com/jawah/urllib3.future.git@alt-timeout" -U

Try it and report back if the issue no longer appear. Then we will move on it.

Regards,