QoL: Kill server with a single keyboard interrupt#8919
Open
lstein wants to merge 7 commits intoinvoke-ai:mainfrom
Open
QoL: Kill server with a single keyboard interrupt#8919lstein wants to merge 7 commits intoinvoke-ai:mainfrom
lstein wants to merge 7 commits intoinvoke-ai:mainfrom
Conversation
Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
* Initial plan * Handle KeyboardInterrupt in run_app to allow single Ctrl+C shutdown Co-authored-by: lstein <111189+lstein@users.noreply.github.com> * Force os._exit(0) on KeyboardInterrupt to avoid hanging on background threads Co-authored-by: lstein <111189+lstein@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
JPPhoto
requested changes
Feb 27, 2026
Collaborator
JPPhoto
left a comment
There was a problem hiding this comment.
I am a bit concerned about the brute-force exit here. What happens to all of the existing threads?
invokeai/app/run_app.py
Outdated
| except KeyboardInterrupt: | ||
| logger.info("InvokeAI shutting down...") | ||
| # Force exit to avoid hanging on non-daemon background threads (e.g. model install service). | ||
| os._exit(0) |
Collaborator
There was a problem hiding this comment.
Shouldn't we make sure threads clean up instead of forcing an exit?
Collaborator
Author
There was a problem hiding this comment.
Good point! Fixed in commit 36150df
When no model install or download is active:
[2026-02-28 10:47:05,987]::[InvokeAI]::INFO --> Invoke running on http://127.0.0.1:9090 (Press CTRL+C to quit)
^C[2026-02-28 10:47:10,681]::[ModelInstallService]::INFO --> Installer thread 127430597535424 exiting
[2026-02-28 10:47:10,700]::[InvokeAI]::INFO --> InvokeAI shutting down...
When a download or install is active:
[2026-02-28 10:44:52,983]::[DownloadQueueService]::INFO --> File download started: https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev/resolve/main/text_encoder_2/model-00001-of-00002.safetensors
^C[2026-02-28 10:45:03,826]::[ModelInstallService]::WARNING --> Pausing job 0
[2026-02-28 10:45:03,826]::[ModelInstallService]::WARNING --> Pausing black-forest-labs/FLUX.1-Kontext-dev
[2026-02-28 10:45:03,827]::[DownloadQueueService]::WARNING --> Download paused: https://huggingface.co/black-forest-labs/FLUX.1-Kontext-dev/resolve/main/text_encoder_2/model-00001-of-00002.safetensors
[2026-02-28 10:45:04,821]::[ModelInstallService]::INFO --> Installer thread 136404375795392 exiting
[2026-02-28 10:45:05,532]::[InvokeAI]::INFO --> InvokeAI shutting down...
Collaborator
There was a problem hiding this comment.
Have you tried after generating? I cannot get Invoke to exit gracefully; it hangs on "InvokeAI shutting down...".
[2026-02-28 10:38:22,186]::[InvokeAI]::INFO --> InvokeAI shutting down...
^CException ignored in: <module 'threading' from '/home/jsp/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/threading.py'>
Traceback (most recent call last):
File "/home/jsp/.local/share/uv/python/cpython-3.12.11-linux-x86_64-gnu/lib/python3.12/threading.py", line 1624, in _shutdown
lock.acquire()
KeyboardInterrupt:
Something is holding onto a lock.
* Initial plan * Replace os._exit(0) with ApiDependencies.shutdown() on KeyboardInterrupt 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> * Make stop() idempotent in download and model install services 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> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR cleans up what happens when you ^C the running invokeai-web server.
Before
Two ^C needed, and terminal looks lilke this:
After:
Only one ^C needed, and terminal shows this:
Related Issues / Discussions
QA Instructions
Merge Plan
Simple merge.
Checklist
What's Newcopy (if doing a release after this PR)