question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

aiorun hangs in wait_for_cancelled_tasks

See original GitHub issue

Thank you for your library, it really enhances the user experience when working with asyncio. But in the course of exploitation we came across the fact that sometimes the program does not stop.

The program does not stop in such a situation

import asyncio
import logging

import aiorun


async def coro_with_exception():
    await asyncio.sleep(0.1)
    raise Exception


async def unstoppable_coro():
    # For some reason one of the tasks does not want to stop
    try:
        while True:
            await asyncio.sleep(0.1)
    except asyncio.CancelledError:
        while True:
            await asyncio.sleep(0.1)


async def main():
    asyncio.create_task(unstoppable_coro())
    asyncio.create_task(coro_with_exception())


if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO)
    aiorun.run(
        main(),
        stop_on_unhandled_errors=True,
    )

We see the following logs

ERROR:aiorun:Unhandled exception; stopping loop: 'Task exception was never retrieved'
Traceback (most recent call last):
  File "C:\git\aio_run_example.py", line 9, in coro_with_exception
    raise Exception
Exception
INFO:aiorun:Entering shutdown phase.
INFO:aiorun:Cancelling pending tasks.
INFO:aiorun:Running pending tasks till complete

I propose to introduce a correction.

Replace

async def wait_for_cancelled_tasks():
    return await gather(*tasks, *do_not_cancel, return_exceptions=True)

To

async def wait_for_cancelled_tasks():
    return await asyncio.wait([*tasks, *do_not_cancel], timeout=gracefull_shutdown_timeout)

And add argument to run

def run(
    coro: "Optional[Coroutine]" = None,
    *,
    loop: Optional[AbstractEventLoop] = None,
    shutdown_handler: Optional[Callable[[AbstractEventLoop], None]] = None,
    shutdown_callback: "ShutdownCallback" = None,
    executor_workers: Optional[int] = None,
    executor: Optional[Executor] = None,
    use_uvloop: bool = False,
    stop_on_unhandled_errors: bool = False,
    gracefull_shutdown_timeout: float = 60
) -> None:
    ...

If a correction is made, the program is guaranteed to stop.

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cjrhcommented, Nov 13, 2022

Thank you for checking. I take your point about None, but I think it would be better to bail out, since that at least shouldn’t happen silently, and then users can change it to a big number if they like. I think your sugges4tion in the report of 60s is a fine default.

Thanks again!

0reactions
cjrhcommented, Nov 14, 2022

@samigullinbulat The change is including in v2022.11.1 which I’ve just deployed: https://pypi.org/project/aiorun/

Read more comments on GitHub >

github_iconTop Results From Across the Web

cancelling main starting coroutine firstly (before all ... - GitHub
Starting script using aiorun and press Ctrl+C on 2'nd message processing queue : ... [INFO] - [aiorun] - aiorun.py:262 - Cancelling pending tasks....
Read more >
How to use the aiorun.run function in aiorun - Snyk
To help you get started, we've selected a few aiorun.run examples, ... for i in range(10)) await asyncio.sleep(0.01) raise Exception("Stops the loop") with ......
Read more >
aiorun - PyPI
aiorun · Spawn all your work from a single, starting coroutine · When a shutdown signal is received, all currently-pending tasks will have ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found