Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'AsyncLock' object has no attribute '_anyio_lock' #825

Closed
T-256 opened this issue Oct 12, 2023 · 2 comments · Fixed by #826
Closed

AttributeError: 'AsyncLock' object has no attribute '_anyio_lock' #825

T-256 opened this issue Oct 12, 2023 · 2 comments · Fixed by #826

Comments

@T-256
Copy link
Contributor

T-256 commented Oct 12, 2023

Try install httpx + httpcore 1.0 then remove anyio. Error:

(.venv) E:\hcore1>python app.py
Traceback (most recent call last):
  File "E:\hcore1\app.py", line 6, in main
    r = await client.get('https://www.example.com/')
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1757, in get
    return await self.request(
           ^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1530, in request
    return await self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1617, in send
    response = await self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1645, in _send_handling_auth
    response = await self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1682, in _send_handling_redirects
    response = await self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 1719, in _send_single_request
    response = await transport.handle_async_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_transports\default.py", line 366, in handle_async_request
    resp = await self._pool.handle_async_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 224, in handle_async_request
    async with self._pool_lock:
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_synchronization.py", line 62, in __aenter__
    self.setup()
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_synchronization.py", line 55, in setup
    raise RuntimeError(
RuntimeError: Running with asyncio requires installation of 'httpcore[asyncio]'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\hcore1\app.py", line 8, in <module>
    asyncio.run(main())
  File "C:\Users\User\.rye\py\[email protected]\install\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\User\.rye\py\[email protected]\install\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\.rye\py\[email protected]\install\Lib\asyncio\base_events.py", line 664, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "E:\hcore1\app.py", line 5, in main
    async with httpx.AsyncClient() as client:
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_client.py", line 2003, in __aexit__
    await self._transport.__aexit__(exc_type, exc_value, traceback)
  File "E:\hcore1\.venv\Lib\site-packages\httpx\_transports\default.py", line 345, in __aexit__
    await self._pool.__aexit__(exc_type, exc_value, traceback)
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 327, in __aexit__
    await self.aclose()
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_async\connection_pool.py", line 312, in aclose
    async with self._pool_lock:
  File "E:\hcore1\.venv\Lib\site-packages\httpcore\_synchronization.py", line 67, in __aenter__
    await self._anyio_lock.acquire()
          ^^^^^^^^^^^^^^^^
AttributeError: 'AsyncLock' object has no attribute '_anyio_lock'

(.venv) E:\hcore1>

Originally posted by @T-256 in encode/httpx#2885 (comment)

@tomchristie
Copy link
Member

tomchristie commented Oct 13, 2023

Here's the same thing slimmed down to just an httpcore example...

example.py

import httpcore
import asyncio


async def main():
    async with httpcore.AsyncConnectionPool() as pool:
        response = await pool.request("GET", "https://www.example.com")
        print(response)


asyncio.run(main())

console:

$ python3 -m venv venv
$ venv/bin/pip install httpcore[asyncio]
$ venv/bin/pip uninstall anyio
$ venv/bin/python example.py
Traceback (most recent call last):
  File "/Users/tomchristie/Temp/./example.py", line 7, in main
    response = await pool.request("GET", "https://www.example.com")
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_async/interfaces.py", line 43, in request
    response = await self.handle_async_request(request)
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 224, in handle_async_request
    async with self._pool_lock:
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_synchronization.py", line 62, in __aenter__
    self.setup()
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_synchronization.py", line 55, in setup
    raise RuntimeError(
RuntimeError: Running with asyncio requires installation of 'httpcore[asyncio]'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/tomchristie/Temp/./example.py", line 11, in <module>
    asyncio.run(main())
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/Users/tomchristie/Temp/./example.py", line 6, in main
    async with httpcore.AsyncConnectionPool() as pool:
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 327, in __aexit__
    await self.aclose()
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_async/connection_pool.py", line 312, in aclose
    async with self._pool_lock:
  File "/Users/tomchristie/Temp/venv/lib/python3.10/site-packages/httpcore/_synchronization.py", line 67, in __aenter__
    await self._anyio_lock.acquire()
AttributeError: 'AsyncLock' object has no attribute '_anyio_lock'

This is okay... can see an appropriate RuntimeError when we deliberately break the installation, but it could be neater.


edit:

A better replication of this...

$ python3 -m venv venv
$ venv/bin/pip install httpcore  # Intention was to install `httpcore[asyncio]`
$ venv/bin/python example.py

@chamikabm
Copy link

chamikabm commented Oct 30, 2023

The same thing happened to me with the following dependencies, with httpx

httpx==0.25.0 sniffio==1.3.0 certifi==2023.7.22 idna==3.4 httpcore==0.18.0 anyio>=3.0,<5.0

Error:
graphql-async-test-service | File "/app/graphql_service/manager_libraries/base.py", line 119, in async_http_request graphql-async-test-service | resp = await httpx_client(*args, **kwargs) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1848, in post graphql-async-test-service | return await self.request( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1530, in request graphql-async-test-service | return await self.send(request, auth=auth, follow_redirects=follow_redirects) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1617, in send graphql-async-test-service | response = await self._send_handling_auth( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1645, in _send_handling_auth graphql-async-test-service | response = await self._send_handling_redirects( graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1682, in _send_handling_redirects graphql-async-test-service | response = await self._send_single_request(request) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_client.py", line 1719, in _send_single_request graphql-async-test-service | response = await transport.handle_async_request(request) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpx/_transports/default.py", line 366, in handle_async_request graphql-async-test-service | resp = await self._pool.handle_async_request(req) graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_async/connection_pool.py", line 224, in handle_async_request graphql-async-test-service | async with self._pool_lock: graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_synchronization.py", line 48, in __aenter__ graphql-async-test-service | self.setup() graphql-async-test-service | File "/usr/local/lib/python3.9/site-packages/httpcore/_synchronization.py", line 41, in setup graphql-async-test-service | raise RuntimeError( graphql-async-test-service | RuntimeError: Running under asyncio requires the 'anyio' package to be installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants