Skip to content

asyncio: interrupting run_until_complete makes the next run stop early #158406

Description

@tjkuson

Bug report

Bug description:

Interrupting loop.run_until_complete() after its future is done means that the next run of the same loop can stop after one iteration. If the future of that run is not done by then, loop.run_until_complete() raises RuntimeError: Event loop stopped before Future completed.

import asyncio
import signal

async def amain():
    asyncio.get_running_loop().call_soon(signal.raise_signal, signal.SIGINT)

loop = asyncio.new_event_loop()
try:
    loop.run_until_complete(amain())
except KeyboardInterrupt:
    pass
loop.run_until_complete(asyncio.sleep(0))

The above raises RuntimeError. The expected behaviour is that no error is raised.

If I understand correctly, this is due to the callback that stops the loop being missed by remove_done_callback() and thus deferred to the next iteration of the loop due to the timing of the signal.

Thus, this also affects asyncio.run() which runs the loop again during shutdown.

import asyncio
import signal

async def amain():
    asyncio.create_task(asyncio.sleep(1))
    asyncio.get_running_loop().call_soon(signal.raise_signal, signal.SIGINT)

asyncio.run(amain())

This raises RuntimeError. The expected behaviour is that KeyboardInterrupt is raised.

gh-66619 and gh-74608 related to similar cases.

CPython versions tested on:

3.15, 3.14, 3.13, 3.11, 3.12, 3.10, CPython main branch

Operating systems tested on:

macOS

Linked PRs

Activity

  1. added
    type-bug
    on Sep 29, 2026
  2. deadlovelll commented on Sep 29, 2026

    @deadlovelll
    Contributor

    hey @tjkuson, i can work on this if you don't mind

  3. tjkuson commented on Sep 29, 2026

    @tjkuson
    ContributorAuthor

    @deadlovelll I have a draft fix locally that I was going to test and tidy up post sometime this week, but if you have a fix by all means open a PR first, I don't mind :)

  4. deadlovelll commented on Sep 29, 2026

    @deadlovelll
    Contributor

    @deadlovelll I have a draft fix locally that I was going to test and tidy up post sometime this week, but if you have a fix by all means open a PR first, I don't mind :)

    It's okay, go ahead, it's your find =)

  5. added 3 commits that reference this issue on Sep 29, 2026
  6. moved this from Todo to In progress in Project_VKon Oct 18, 2026
  7. tjkuson commented on Nov 20, 2026

    @tjkuson
    ContributorAuthor

    I posted #158450 to fix this issue, could someone take a look? It's been over a month and the dev guide suggests I ping here.

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

    topic-asynciotype-bug

    Projects

    • Status
      Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions