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

Closed
graingert opened this issue Jan 7, 2025 · 4 comments
Closed
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

Linked PRs

@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

graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025
…t missed and introduced incorrect cancellations
graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025
…t missed and introduced incorrect cancellations
kumaraditya303 added a commit that referenced this issue Jan 20, 2025
… introduced incorrect cancellations (#129063)

Co-authored-by: Kumar Aditya <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 20, 2025
…t missed and introduced incorrect cancellations (pythonGH-129063)

(cherry picked from commit ed6934e)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit that referenced this issue Jan 20, 2025
…sed and introduced incorrect cancellations (GH-129063) (#129089)

gh-128588: gh-128550: remove eager tasks optimization that missed and introduced incorrect cancellations (GH-129063)
(cherry picked from commit ed6934e)

Co-authored-by: Thomas Grainger <[email protected]>
Co-authored-by: Kumar Aditya <[email protected]>
graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025
…t missed and introduced incorrect cancellations (python#129063)

Co-authored-by: Kumar Aditya <[email protected]>
kumaraditya303 added a commit that referenced this issue Jan 21, 2025
…er tasks optimization that missed and introduced incorrect cancellations (#129063) (#128586)

Co-authored-by: Kumar Aditya <[email protected]>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 21, 2025
…t missed and introduced incorrect cancellations (python#129063)

Co-authored-by: Kumar Aditya <[email protected]>
@github-project-automation github-project-automation bot moved this to Done in asyncio Jan 21, 2025
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: Done
Development

No branches or pull requests

2 participants