Skip to content

asyncio.TaskGroup swallows cancellations coming from outside of it #155433

Description

@deadlovelll

Bug report

Bug description:

After tg.cancel() taskgroup silently swallows any cancellation requested from outside it

Repro:

import asyncio

async def worker():
    try:
        await asyncio.sleep(3600)
    finally:
        await asyncio.sleep(1)

async def main():
    async with asyncio.timeout(0.5):
        async with asyncio.TaskGroup() as tg:
            tg.create_task(worker())
            await asyncio.sleep(0.1)
            tg.cancel()
    print("no error")

asyncio.run(main())

Expected output:

raise TimeoutError from exc_val
TimeoutError

But actually:

no error

Proposed fix - add else branch: if uncancel() leaves requests pending, cancellation was not ours, so we need to propogate it

        if self._parent_cancel_requested:
            # If this flag is set we *must* call uncancel().
            if self._parent_task.uncancel() == 0:
                # If there are no pending cancellations left,
                # don't propagate CancelledError.
                propagate_cancellation_error = None
            else:
                propagate_cancellation_error = exceptions.CancelledError()

Have a fix ready

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. kumaraditya303 commented on Aug 20, 2026

    @kumaraditya303
    Contributor

    I think there might be issues if the parent task is already cancelling though I haven't verified it, do you want to look into that?

  2. deadlovelll commented on Aug 20, 2026

    @deadlovelll
    ContributorAuthor

    I think there might be issues if the parent task is already cancelling though I haven't verified it, do you want to look into that?

    Yeah, i'll take a look today

  3. deadlovelll commented on Aug 20, 2026

    @deadlovelll
    ContributorAuthor

    I think there might be issues if the parent task is already cancelling though I haven't verified it, do you want to look into that?

    I just checked this, and if i understand you well - no, there is no issues:

    import asyncio
    
    async def main():
        t = asyncio.current_task()
        async with asyncio.TaskGroup() as tg:
            tg.create_task(asyncio.sleep(3600))
            t.cancel()
            tg.cancel()
        print("swallowed, still pending:", t.cancelling())
    
    asyncio.run(main())

    On main this code prints "swallowed, still pending: 1", in the fix branch it raises CancelledError

  4. added a commit that references this issue on Aug 23, 2026
  5. added a commit that references this issue on Sep 1, 2026
  6. moved this from Todo to Done in asyncioon Sep 11, 2026
  7. added a commit that references this issue on Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-asynciotype-bugAn unexpected behavior, bug, or error

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions