asyncio.exceptions.CancelledError on scheduler.shutdown()
See original GitHub issueThings 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:
- Created a year ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
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.I was hoping to see what you tried to do with
AsyncExitStack
, based on my advice.