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

Cancellation leaks out of asyncio.TaskGroup on 3.12 when using eager tasks #128588

Open
graingert opened this issue Jan 7, 2025 · 4 comments
Open
Labels
3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@graingert
Copy link
Contributor

graingert commented Jan 7, 2025

Bug report

Bug description:

import sys
import asyncio

if sys.platform == "win32":
    EventLoop = asyncio.ProactorEventLoop
else:
    EventLoop = asyncio.SelectorEventLoop


async def demo():
    class MyError(Exception):
        pass

    async def throw_error():
        raise MyError

    try:
        async with asyncio.TaskGroup() as tg:
            tg.create_task(throw_error())
    except* MyError:
        pass


def loop_factory():
    loop = EventLoop()
    loop.set_task_factory(asyncio.eager_task_factory)
    return loop

asyncio.run(demo(), loop_factory=loop_factory)

results in:

Traceback (most recent call last):
  File "/home/graingert/projects/anyio/demo.py", line 29, in <module>
    asyncio.run(demo(), loop_factory=loop_factory)
  File "/usr/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
asyncio.exceptions.CancelledError

CPython versions tested on:

3.12

Operating systems tested on:

Linux

@graingert graingert added type-bug An unexpected behavior, bug, or error 3.12 bugs and security fixes labels Jan 7, 2025
@graingert
Copy link
Contributor Author

I think related to #116720 but a lot easier to trigger by accident.

@kumaraditya303
Copy link
Contributor

This seems fixed on main. Perhaps we missed backporting a fix for it?

@graingert
Copy link
Contributor Author

graingert commented Jan 8, 2025

I believe it was intentionally not backported because the conditions to trigger it were thought to be a bit complex and it needed a change to how Task.cancel worked (re unsetting must_cancel)

@kumaraditya303
Copy link
Contributor

cc @gvanrossum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: No status
Development

No branches or pull requests

2 participants