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.

asyncio.exceptions.CancelledError on scheduler.shutdown()

See original GitHub issue

Things to check first

  • I have checked that my issue does not already have a solution in the FAQ

  • I have searched the existing issues and didn’t find my bug already reported there

  • I have checked that my bug is still present in the latest release

Version

3.9.1

What happened?

Hi again,

So I downgraded to APScheduler v3.9.1 (from the latest alpha2) and set up a job using AsyncIOScheduler() in the Starlite API framework. This was the easiest path forward for me (RE #673)

I am able to successfully start and stop the scheduler with the Starlite on_startup and on_shutdown events. But on shutdown, I am getting exceptions thrown after the shutdown event.

Is this something I need to worry about, or is there anything I can do to handle the errors? The tasks query an API and write to a DB.

STACKTRACE:

INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO - 2022-10-27 16:13:33,785 - main - main - 🛑 Stopping task scheduler
INFO - 2022-10-27 16:13:33,786 - apscheduler.scheduler - base - Scheduler has been shut down
ERROR - 2022-10-27 16:13:33,791 - apscheduler.executors.default - base_py3 - Job "main (trigger: interval[0:01:00], next run at: 2022-10-27 16:14:08 EAT)" raised an exception
Traceback (most recent call last):
  File "/miniforge3/envs/py311/lib/python3.11/site-packages/apscheduler/executors/base_py3.py", line 30, in run_coroutine_job
    retval = await job.func(*job.args, **job.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Documents/GitHub/data-wrangler/src/job.py", line 332, in main
    async with rate_limiter:
  File "/miniforge3/envs/py311/lib/python3.11/site-packages/aiolimiter/leakybucket.py", line 110, in __aenter__
    await self.acquire()
  File "/miniforge3/envs/py311/lib/python3.11/site-packages/aiolimiter/leakybucket.py", line 97, in acquire
    await wait_for(
  File "/miniforge3/envs/py311/lib/python3.11/asyncio/tasks.py", line 466, in wait_for
    await waiter
asyncio.exceptions.CancelledError
INFO:     Application shutdown complete.
INFO:     Finished server process [78813]
INFO:     Stopping reloader process [78810]

How can we reproduce the bug?

Here are the functions I’m using to schedule the jobs. I tried added scheduler.remove_all_jobs() before shutdown, but that created even more exceptions.

async def start_scheduler(state: State):
    scheduler = AsyncIOScheduler()
    scheduler.add_job(
        main, "interval", seconds=60, max_instances=1, id="data_job_1"
    )
    scheduler.start()
    state.scheduler = scheduler
    logger.info("🚦 Starting the task scheduler")


async def stop_scheduler(state: State):
    scheduler = state.scheduler
    scheduler.shutdown(wait=True)
    logger.info("🛑 Stopping task scheduler")

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
agronholmcommented, Oct 27, 2022

Asyncio support was added to the 3.x series as an afterthought. It never worked really well, so in v4.0 I redesigned the library as async-first. I should mention that, among other things, wait=True does not work with the asyncio scheduler, as there is no way to wait for the shutdown in a synchronous function.

0reactions
agronholmcommented, Oct 28, 2022

I was hoping to see what you tried to do with AsyncExitStack, based on my advice.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[ERROR] asyncio.exceptions.CancelledError #1532 - GitHub
First thing to note is that asyncio.exceptions.CancelledError ... shutdown() closes down the scheduler that the Client is attached to first, ...
Read more >
python3.9.2 asyncio.exceptions.CancelledError with aysncio ...
The event loop of asyncio.run only. runs the passed coroutine. before attempting to clean up all other outstanding tasks and coroutines.
Read more >
Exceptions — Python 3.11.1 documentation
exception asyncio.CancelledError¶. The operation has been cancelled. This exception can be caught to perform custom operations when asyncio Tasks are ...
Read more >
How to ensure asyncio task exceptions always get logged
When we run asyncio.create_task(my_coroutine()), what happens when my_coroutine() raises an exception? Consider this example [2]: import asyncio ...
Read more >
Using Asyncio in Python
raise asyncio.CancelledError inside your coroutine (Example 3-8). Example 3-8. Using coro.throw() to inject exceptions into a coroutine. >>> coro = f(). > ...
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