-
Notifications
You must be signed in to change notification settings - Fork 142
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
RuntimeError when cancelling asyncio.Task running within a BlockingPortal. #357
Comments
Yes, please create a minimal reproducing case. This traceback doesn't show where the call really originated from. |
This should do it: import asyncio
from anyio.from_thread import BlockingPortal
async def main():
async with BlockingPortal() as portal:
await asyncio.get_running_loop().run_in_executor(
None, lambda: in_thread(portal)
)
def in_thread(portal):
async def coro():
pass
async def in_loop():
t = asyncio.create_task(coro())
t.cancel()
await t
portal.start_task_soon(in_loop)
asyncio.run(main()) Output:
|
Thanks, I'll start investigating this in a while. |
Thanks @agronholm, I appreciate it! |
In the AnyIO test suite, all the calls to https://github.com/agronholm/anyio/blob/master/src/anyio/from_thread.py#L164 happen in the worker thread. But in your script the call happens in the event loop thread. I'm now trying to figure out why. |
I have a fix for this, but I need to create a regression test too before I commit it. |
I was able to reduce the test case into a smaller snippet: import anyio
from anyio.from_thread import BlockingPortal
from anyio import to_thread
async def main():
async with BlockingPortal() as portal:
await to_thread.run_sync(in_thread, portal)
def in_thread(portal):
async def in_loop():
raise anyio.get_cancelled_exc_class()
portal.start_task_soon(in_loop)
anyio.run(main) But when I copy the same code into a test function, the test passes. Quite infuriating. |
The |
Thanks for looking into it! |
I managed to produce a failing test case. Seems like this is only applicable to asyncio. There seems to be no way for a cancellation exception to come through unless the portal was already stopped. |
Thanks a lot for fixing it that quick! |
Hi, I'm facing some issues with cancellation and blocking portals and wondering whether that's a bug in anyio, or in my code.
To reproduce:
asyncio.Task
and waits for it.asyncio.Task
gets cancelled from elsewhere, within the event loop. So, we don't cancel theconcurrent.futures.Future
that gets returned by theBlockingPortal.start_task_soon
.RuntimeError
gets logged.I don't have a code snippet to reproduce, but if required, I can probably create it. To me, the
RuntimeError
doesn't make sense.The text was updated successfully, but these errors were encountered: