Skip to content

Asyncio.Event.wait doesn't resume execution when the event is set if no other tasks are active. #122938

Open
@agustinhuman

Description

@agustinhuman

Bug report

Bug description:

This is a toy program that sets an asyncio.Event when a signal is received, and runs an asynchronous function (passive_wait) that waits for that signal to be set.

When the following code is executed and a signal is sent externally, it is detected by the program, but the function waiting function does not resume execution.

It does finish correctly if another async function is executed alongside, like when uncommenting the line active_wait(),.

import asyncio
import signal

event = asyncio.Event()

def _signal_handler(*_) -> None:
    event.set()
    print("Event triggered")

signal.signal(signal.SIGINT, _signal_handler)
signal.signal(signal.SIGTERM, _signal_handler)

async def passive_wait():
    print("Pre event")
    await event.wait()
    print("Post event")

async def active_wait():
    while not event.is_set():
        await asyncio.sleep(1)

async def main():
    await asyncio.gather(
        passive_wait(),
        # Uncommenting the following line makes it stop correctly
        #active_wait(),
    )

asyncio.run(main())
# Send the signal externally

If that's the intended behavior I would love some pointers.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions