GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GH-95289: Always call uncancel() when required by gvanrossum · Pull Request #95513 · python/cpython

Conversation

@gvanrossum

@gvanrossum gvanrossum commented Aug 1, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

In __aexit__() we should always call self._parent_task.uncancel() when we called self._parent_task.cancel() -- even if the exception being received by __aexit__() is not CancelledError (since it may have been suppressed or replaced by another exception).

The logic is a bit contorted since we must propagate a cancellation error in two separate situations:

  • if uncancel() is called and it returns a value greater than zero
  • if we don't call uncancel() at all
  • Issue: asyncio.TaskGroup corrupts uncancel() stack when the parent task replaces the cancelled error #95289

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added the awaiting core review label Aug 1, 2022
@gvanrossum
gvanrossum requested review from graingert and kumaraditya303 August 1, 2022 02:32
@YvesDup

YvesDup commented Aug 1, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Hi,
I ran this code with your changes, for the required test in the 'externally cancel' case.
Here a CancelledError is raised, _parent_cancel_requested is False, so uncancel() is never called. Is the expected behavior ?

import asyncio

class MyException(Exception):
    pass

async def async_fn2():
    try:
        await asyncio.sleep(0.0)
        raise MyException
    finally:
        print("raise done")

async def main():
    task = asyncio.current_task()
    try:
        async with asyncio.TaskGroup() as tg:
            tg.create_task(async_fn2())
            await asyncio.sleep(1)

    except* (MyException, asyncio.CancelledError) as e:
        # How to check a private `_parent_cancel_requested` attr here, outside the class ?
        print(f"{e.exceptions} -> {task.cancelling()=}, {tg._parent_cancel_requested=}")

async def supermain():
    t = asyncio.create_task(main())
    await asyncio.sleep(0)

    t.cancel()
    print("cancel main task")
    await asyncio.sleep(1)

    print(f"{t.cancelling()=} must be 1, confirm ?")

asyncio.run(supermain())
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Aug 1, 2022

Copy link
Copy Markdown
Member Author

That code looks racy with the sleep(0.0) calls, but it is correct that since your supermain code cancels main, uncancel will not be called. Note that _parent_cancel_requested is not truly private (single underscore) so for debugging can check it.

YvesDup reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@YvesDup

YvesDup commented Aug 1, 2022

Copy link
Copy Markdown
Contributor

I wonder whether it is important to check if parent task is not cancelling when starts the async with ?
In that case, should we raise a RuntimeError in the __aenter__ ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum

gvanrossum commented Aug 1, 2022

Copy link
Copy Markdown
Member Author

I wonder whether it is important to check if parent task is not cancelling when starts the async with ? In that case, should we raise a RuntimeError in the __aenter__ ?

What problem would that solve?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@gvanrossum
gvanrossum force-pushed the fix-taskgroup-uncancel branch from dde8683 to 839f7f3 Compare August 1, 2022 20:46
@gvanrossum
pythonGH-95289: Always call uncancel() when required
6cd25fc
@gvanrossum
gvanrossum force-pushed the fix-taskgroup-uncancel branch from 839f7f3 to 6cd25fc Compare August 1, 2022 20:50
@YvesDup

YvesDup commented Aug 2, 2022

Copy link
Copy Markdown
Contributor

I wonder whether it is important to check if parent task is not cancelling when starts the async with ? In that case, should we raise a RuntimeError in the aenter ?

What problem would that solve?

Problem is already solved with the 'externally cancel' case.
Sorry for waste your time.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303

kumaraditya303 commented Aug 3, 2022

Copy link
Copy Markdown
Contributor

Superseded by #95602 which includes tests.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kumaraditya303 kumaraditya303 closed this Aug 3, 2022
@gvanrossum
gvanrossum deleted the fix-taskgroup-uncancel branch August 7, 2022 15:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@graingert graingert Awaiting requested review from graingert
@kumaraditya303 kumaraditya303 Awaiting requested review from kumaraditya303 kumaraditya303 is a code owner
@1st1 1st1 Awaiting requested review from 1st1 1st1 will be requested when the pull request is marked ready for review 1st1 is a code owner
@asvetlov asvetlov Awaiting requested review from asvetlov asvetlov will be requested when the pull request is marked ready for review asvetlov is a code owner
@willingc willingc Awaiting requested review from willingc willingc will be requested when the pull request is marked ready for review willingc is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@gvanrossum @YvesDup @kumaraditya303 @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.