-
Notifications
You must be signed in to change notification settings - Fork 137
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
Error when using Lock from thread in asyncio #773
Comments
The immediate cause is that it's trying to create a weak reference to |
BTW with #761 the error is different: TracebackTraceback (most recent call last): File "/home/david/git/anyio/foo.py", line 11, in run(main) File "/home/david/git/anyio/src/anyio/_core/_eventloop.py", line 74, in run return async_backend.run(func, args, {}, backend_options) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 2173, in run return runner.run(wrapper()) ^^^^^^^^^^^^^^^^^^^^^ File "/home/david/micromamba/envs/anyio/lib/python3.12/asyncio/runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/david/micromamba/envs/anyio/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 2161, in wrapper return await func(*args) ^^^^^^^^^^^^^^^^^ File "/home/david/git/anyio/foo.py", line 9, in main await to_thread.run_sync(worker, lock) File "/home/david/git/anyio/src/anyio/to_thread.py", line 56, in run_sync return await get_async_backend().run_sync_in_worker_thread( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 2326, in run_sync_in_worker_thread return await future ^^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 872, in run result = context.run(func, *args) ^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/david/git/anyio/foo.py", line 5, in worker from_thread.run_sync(lock.release) File "/home/david/git/anyio/src/anyio/from_thread.py", line 80, in run_sync return async_backend.run_sync_from_thread(func, args, token=token) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 2388, in run_sync_from_thread return f.result() ^^^^^^^^^^ File "/home/david/micromamba/envs/anyio/lib/python3.12/concurrent/futures/_base.py", line 456, in result return self.__get_result() ^^^^^^^^^^^^^^^^^^^ File "/home/david/micromamba/envs/anyio/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result raise self._exception File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 2379, in wrapper f.set_result(func(*args)) ^^^^^^^^^^^ File "/home/david/git/anyio/src/anyio/_backends/_asyncio.py", line 1728, in release raise RuntimeError("The current task is not holding this lock") RuntimeError: The current task is not holding this lock |
That's to be expected. The workaround is to use a nice 'async with lock:` block, and call back to the thread from within that. |
Oh I see, thanks. I guess I should close this issue then, and not give Alex more work than needed. |
I agree with @smurfix 's suggestion, but I would still like to understand why there was an error, and why it only happens with asyncio, as the effects of the problem may not be limited to locking. |
For posterity, the reason for this was that I had opted to use |
I see this is reopened - I think we've just run onto something similar on 4.6 (previously using 4.4). I haven't dug into it much but googling the error brought me straight to this ticket |
Ok have confirmed the same issue on 4.5 and 4.6 of Anyio. Sample trace here:
|
@samskiter oh that's odd, it's using the AsyncIOBackend on trio, can you post your blah.py source code? |
@samskiter ping – would you share your use case? It's hard to determine exactly what went wrong without seeing what code caused it. |
Things to check first
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.4.0
Python version
3.12.5
What happened?
With the
asyncio
backend, this code fails withTypeError: cannot create weak reference to 'NoneType' object
. It works with thetrio
backend.Traceback
How can we reproduce the bug?
The text was updated successfully, but these errors were encountered: