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

asyncio: cancelling gather only cancels 1st awaited task (differs from CPython) #7901

Closed
jdtsmith opened this issue Oct 14, 2021 · 1 comment
Labels

Comments

@jdtsmith
Copy link

jdtsmith commented Oct 14, 2021

In CPython, when a task awaiting asyncio.gather is cancelled, gather gets cancelled, and then all tasks gather is awaiting are cancelled. In MicroPython v1.17, it seems only the first gather task gets cancelled:

import uasyncio as asyncio
task = None
async def test1(r):
    while True:
        print(f"Test1 {r}")
        await asyncio.sleep(1)
        
async def test2(r):
    while True:
        print(f"Test2 {r}")
        await asyncio.sleep(1.65)

async def cancel():
    await asyncio.sleep(10)
    task.cancel()

async def main(r):
    await asyncio.gather(test1(r), test2(r), cancel())

async def run():
    global task
    for i in range(10):
        task = asyncio.create_task(main(i))
        try:
            await task
        except BaseException as e:
            print(f"Caught exception {e}")
            pass

asyncio.run(run())

Note that test2 tasks keep accumulating, whereas test1 gets cancelled correctly. Interestingly, these uncancelled tasks stay in the event loop even after run() completes normally, so that future runs include the "zombie" test2's.

Both of these behavior differ from CPython (v3.9.7).

dpgeorge added a commit to dpgeorge/micropython that referenced this issue Mar 29, 2022
The following fixes are made:
- cancelling a gather now cancels all sub-tasks of the gather (previously
  it would only cancel the first)
- if any sub-task of a gather raises an exception then the gather finishes
  (previously it would only finish if the first sub-task raised)

Fixes issues micropython#5798, micropython#7807, micropython#7901.

Signed-off-by: Damien George <damien@micropython.org>
dpgeorge added a commit to dpgeorge/micropython that referenced this issue Mar 29, 2022
The following fixes are made:
- cancelling a gather now cancels all sub-tasks of the gather (previously
  it would only cancel the first)
- if any sub-task of a gather raises an exception then the gather finishes
  (previously it would only finish if the first sub-task raised)

Fixes issues micropython#5798, micropython#7807, micropython#7901.

Signed-off-by: Damien George <damien@micropython.org>
dpgeorge added a commit to dpgeorge/micropython that referenced this issue Mar 30, 2022
The following fixes are made:
- cancelling a gather now cancels all sub-tasks of the gather (previously
  it would only cancel the first)
- if any sub-task of a gather raises an exception then the gather finishes
  (previously it would only finish if the first sub-task raised)

Fixes issues micropython#5798, micropython#7807, micropython#7901.

Signed-off-by: Damien George <damien@micropython.org>
@dpgeorge
Copy link
Member

Fixed by 90aaf2d

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants