X Tutup
The Wayback Machine - https://web.archive.org/web/20250609152908/https://github.com/python/cpython/issues/113151
Skip to content

ctrl+c cause GeneratorExit after pressed more than twice in asyncio #113151

Open
@NewUserHa

Description

@NewUserHa

Bug report

Bug description:

import asyncio
import logging

logging.basicConfig(level=logging.DEBUG)


async def myfunc(i):
    logging.info("start %r", i)
    try:
        await asyncio.sleep(i)
    except BaseException as e:
        logging.error("caught in myfunc %r == %r", i, e)
    logging.info("done myfunc %r %r", i, asyncio.get_running_loop())


async def pas():
    try:
        await myfunc(3)
        await myfunc(4)
        await myfunc(5)
        await myfunc(6)
        await myfunc(7)
    except BaseException as e:
        logging.error("caught in pas %r", e)
    logging.info("done pas")


async def main():
    try:
        await pas()
    except BaseException as e:
        logging.error("caught in main %r", e)
    logging.info("done main")


if __name__ == "__main__":
    try:
        asyncio.run(main(), debug=True)
    except:
        logging.exception("caught in __main__")
    logging.error("program ended.")

after pressing ctrl+c once, myfunc() caught CancelledError and loop is running and all code can finish normally;
after pressing ctrl+c twice, myfunc() caught CancelledError and loop is running and all code can finish normally;
after pressing ctrl+c thrice, myfunc() caught GeneratorExit(), but loop and the "program" is already closed before it.

excepted:
after pressing ctrl+c more than 2 times, it should be consistent as before.
OR
the KeyboardInterrupt should propaganda to the asyncio.Runner as _on_sigint() of it raised after pressing twice ctrl+c

CPython versions tested on:

3.11, 3.12

Operating systems tested on:

Windows

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      X Tutup