Skip to content

TaskGroup fails to cancel tasks pending creation, if that task starts a failing task #128550

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

Closed
graingert opened this issue Jan 6, 2025 · 5 comments
Labels
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error

Comments

@graingert
Copy link
Contributor

graingert commented Jan 6, 2025

Bug report

Bug description:

this 'deadlocks' on eager tasks, but raises an EG as expected on regular 'lazy' tasks.

import sys
import asyncio


if sys.version_info >= (3, 13):
    from asyncio import EventLoop
elif sys.platform == "win32":
    from asyncio import ProactorEventLoop as EventLoop
else:
    from asyncio import SelectorEventLoop as EventLoop


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


async def main():
    async with asyncio.TaskGroup() as tg:
        async def third_task():
            raise RuntimeError("third task failed")

        async def second_task():
            tg.create_task(third_task())
            await asyncio.Event().wait()

        tg.create_task(second_task())

# asyncio.run(main())
asyncio.run(main(), loop_factory=loop_factory)

CPython versions tested on:

3.12, 3.13, 3.14

Operating systems tested on:

Linux

Linked PRs

@graingert graingert added type-bug An unexpected behavior, bug, or error topic-asyncio labels Jan 6, 2025
@github-project-automation github-project-automation bot moved this to Todo in asyncio Jan 6, 2025
graingert added a commit to graingert/cpython that referenced this issue Jan 6, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
graingert added a commit to graingert/cpython that referenced this issue Jan 6, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
@picnixz picnixz added the stdlib Python modules in the Lib dir label Jan 6, 2025
@kumaraditya303
Copy link
Contributor

This is happening because as per the following code, we skip scheduling the done callback to the event loop incase the task completes eagerly, I would lean towards fixing this by removing this case and always add the done callback which would be simpler and fix this.

# optimization: Immediately call the done callback if the task is
# already done (e.g. if the coro was able to complete eagerly),
# and skip scheduling a done callback
if task.done():
self._on_task_done(task)
else:
self._tasks.add(task)
task.add_done_callback(self._on_task_done)

@graingert
Copy link
Contributor Author

@kumaraditya303 do we want this removed on 3.14, 3.13 and 3.12? or just 3.12?

@kumaraditya303
Copy link
Contributor

Yes, all the branches.

@graingert
Copy link
Contributor Author

ok will get that setup,

graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
…t missed and introduced incorrect cancellations
graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
…t missed and introduced incorrect cancellations
@graingert
Copy link
Contributor Author

ok working on news now, looks like it will fix #128588 also

kumaraditya303 added a commit that referenced this issue Jan 20, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
… introduced incorrect cancellations (#129063)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Jan 20, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
…t missed and introduced incorrect cancellations (pythonGH-129063)

(cherry picked from commit ed6934e)

Co-authored-by: Thomas Grainger <tagrain@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
kumaraditya303 added a commit that referenced this issue Jan 20, 2025

Partially verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
We cannot verify signatures from co-authors, and some of the co-authors attributed to this commit require their commits to be signed.
…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 <tagrain@gmail.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
graingert added a commit to graingert/cpython that referenced this issue Jan 20, 2025

Verified

This commit was signed with the committer’s verified signature. The key has expired.
graingert Thomas Grainger
…t missed and introduced incorrect cancellations (python#129063)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this issue Jan 21, 2025

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
…t missed and introduced incorrect cancellations (python#129063)

Co-authored-by: Kumar Aditya <kumaraditya@python.org>
@github-project-automation github-project-automation bot moved this from Todo 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
stdlib Python modules in the Lib dir topic-asyncio type-bug An unexpected behavior, bug, or error
Projects
Status: Done
Development

No branches or pull requests

3 participants