8000 asyncio: cancelling gather only cancels 1st awaited task (differs from CPython) · Issue #7901 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content
asyncio: cancelling gather only cancels 1st awaited task (differs from CPython) #7901
Closed
@jdtsmith

Description

@jdtsmith

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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    extmodRelates to extmod/ directory in source

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0