Fix graceful shutdown to wait for download/install worker threads#102
Conversation
Instead of immediately force-exiting the process on CTRL+C, call ApiDependencies.shutdown() to gracefully stop the download and install manager services, allowing active work to complete or cancel cleanly before the process exits. Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
|
@copilot After ^C I am now seeing the error message The same message appears if a model is being downloaded and installed. |
When CTRL+C is pressed, uvicorn's graceful shutdown triggers the FastAPI lifespan which calls ApiDependencies.shutdown(), then a KeyboardInterrupt propagates from run_until_complete() hitting the except block which tries to call ApiDependencies.shutdown() a second time. Change both stop() methods to return silently (instead of raising) when the service is not running. This handles: - Double-shutdown: lifespan already stopped the services - Early interrupt: services were never fully started Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
The error was caused by a double-shutdown: uvicorn's graceful shutdown triggers the FastAPI Fixed in commit |
04a83cf to
4b70d39
Compare
Summary
On
KeyboardInterrupt, the shutdown path calledos._exit(0)unconditionally, killing the process before download or model install threads could complete or cancel their in-flight work.Replace
os._exit(0)withApiDependencies.shutdown(), which propagates throughinvoker.stop()→ each service'sstop():The process then exits naturally; all worker threads are
daemon=Trueso they won't block exit if shutdown is interrupted again.To prevent a double-shutdown error, both
stop()methods are now idempotent — they return silently if the service is not running. This is necessary because uvicorn's graceful shutdown triggers the FastAPIlifespanwhich callsApiDependencies.shutdown(), and then aKeyboardInterruptcan still propagate fromrun_until_complete()into theexceptblock, causing a secondApiDependencies.shutdown()call. Makingstop()a no-op when already stopped (or never started) handles both this double-shutdown case and early interrupts where services were never fully initialized.Related Issues / Discussions
QA Instructions
Start a model download, then hit
CTRL+C. Confirm:.downloadingfile left on disk, no corrupted state) rather than being silently killed mid-write.Exception: Attempt to stop the install service before it was startederror appears.Merge Plan
Checklist
What's Newcopy (if doing a release after this PR)Original prompt
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.