Fix: Kill the server with one keyboard interrupt#94
Conversation
Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
lstein
left a comment
There was a problem hiding this comment.
Almost, but not quite. After running for a while I still have to press ^C twice. Here's the first time:
^C[2026-02-26 19:17:48,392]::[ModelInstallService]::INFO --> Installer thread 140553368897216 exiting
[2026-02-26 19:17:49,395]::[InvokeAI]::INFO --> InvokeAI shutting down...
I waited for about 30s and then ^C again:
^CException ignored in: <module 'threading' from '/home/lstein/.local/share/uv/python/cpython-3.12.12-linux-x86_64-gnu/lib/python3.12/threading.py'>
Traceback (most recent call last):
File "/home/lstein/.local/share/uv/python/cpython-3.12.12-linux-x86_64-gnu/lib/python3.12/threading.py", line 1624, in _shutdown
lock.acquire()
KeyboardInterrupt:
… threads Co-authored-by: lstein <111189+lstein@users.noreply.github.com>
The root cause was that after catching Fixed in commit |
Summary
Pressing Ctrl+C once printed a uvicorn traceback and required a second Ctrl+C to fully exit. There were two separate causes:
Traceback on first Ctrl+C: uvicorn's
capture_signals()re-raises the captured signal viasignal.raise_signal()after graceful shutdown, causing an unhandledKeyboardInterruptto bubble up throughloop.run_until_complete(server.serve()).Process hanging after first Ctrl+C: Even after catching the
KeyboardInterrupt, the process would hang indefinitely because Python's interpreter tried to join non-daemon background threads (e.g. the model install service thread) during shutdown.Fix: wrap the serve call in
run_app.pywith atry/except KeyboardInterruptthat logs a clean shutdown message and then callsos._exit(0)to force an immediate process exit, bypassing Python's thread-joining phase:Uvicorn's graceful shutdown still runs normally before the
KeyboardInterruptis re-raised. Normal server exits (e.g. via API shutdown endpoint) are unaffected.Related Issues / Discussions
QA Instructions
Start
invokeai-weband press Ctrl+C once. The server should exit immediately and cleanly with anInvokeAI shutting down...log line, no traceback, and no need to press Ctrl+C a second time.Merge Plan
Checklist
What's Newcopy (if doing a release after this PR)Original prompt
This section details on the original issue you should resolve
<issue_title>[enhancement]: Kill the server with one keyboard interrupt</issue_title>
<issue_description>### Is there an existing issue for this?
Contact Details
No response
What should this feature add?
Currently to kill
invokeai-webfrom the command line, you must press ^C twice. The first ^C gives this:The second ^C gives these messages and it does finally shut down: