Skip to content

Commit b4af9f4

Browse files
committed
fix: cancel scheduler tasks on shutdown
Otherwise the currently running tasks will never exit (before they actually complete), which means the process can't be properly shut down (only with SIGKILL). Ideally, we let tasks know that they are about to shutdown and give them some time to do so; but in the lack of the mechanism, it's better to cancel than linger forever. Signed-off-by: Ihar Hrachyshka <[email protected]>
1 parent 473a07f commit b4af9f4

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

llama_stack/providers/utils/scheduler.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,14 @@ def _run_loop(self) -> None:
157157
asyncio.set_event_loop(self._loop)
158158
self._loop.run_forever()
159159

160-
# When stopping the loop, give tasks a chance to finish
160+
# TODO: When stopping the loop, give tasks a chance to finish
161161
# TODO: should we explicitly inform jobs of pending stoppage?
162+
163+
# cancel all tasks
162164
for task in asyncio.all_tasks(self._loop):
163-
self._loop.run_until_complete(task)
165+
if not task.done():
166+
task.cancel()
167+
164168
self._loop.close()
165169

166170
async def shutdown(self) -> None:

tests/unit/providers/utils/test_scheduler.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ async def job_handler(on_log, on_status, on_artifact):
5252
assert sched.get_jobs("unknown") == []
5353
assert sched.get_jobs(job_type) == [sched.get_job(job_id)]
5454

55+
# give the job handler a chance to run
56+
await asyncio.sleep(0)
57+
5558
# now shut the scheduler down and make sure the job ran
5659
await sched.shutdown()
5760

@@ -112,6 +115,9 @@ async def successful_job_handler(on_log, on_status, on_artifact):
112115
job_id = "test_job_id2"
113116
sched.schedule(job_type, job_id, successful_job_handler)
114117

118+
# give the job handler a chance to run
119+
await asyncio.sleep(0)
120+
115121
await sched.shutdown()
116122

117123
assert called

0 commit comments

Comments
 (0)