The Fix
Upgrade to version 0.16.0 or later.
Based on closed Kludex/uvicorn issue #1160 · PR/commit linked
Production note: Most teams hit this during upgrades or environment changes. Roll out with a canary and smoke critical endpoints (health, OpenAPI/docs) before 100%.
@@ -310,7 +310,7 @@ def install_signal_handlers(self) -> None:
def handle_exit(self, sig: signal.Signals, frame: FrameType) -> None:
- if self.should_exit:
+ if self.should_exit and sig == signal.SIGINT:
self.force_exit = True
async def _lifespan(app):
import asyncio
try:
yield
except asyncio.exceptions.CancelledError:
pass
server_app = Starlette(routes=routes, lifespan=_lifespan)
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Option A — Upgrade to fixed release\nUpgrade to version 0.16.0 or later.\nWhen NOT to use: Do not apply this fix if your application relies on the previous shutdown behavior.\n\n
Why This Fix Works in Production
- Trigger: [2021-08-19 11:01:19,614][25024] INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
- Mechanism: The shutdown process fails due to improper handling of SIGINT signals in the server's exit logic
- Why the fix works: Fixes the shutdown process issue in Uvicorn by ensuring that the server can gracefully handle SIGINT signals. (first fixed release: 0.16.0).
- If left unfixed, the same config can fail only in production (env differences), causing startup failures or partial feature outages.
Why This Breaks in Prod
- Triggered by an upgrade/regression window: 0.15 breaks; 0.16.0 is the first fixed release.
- Shows up under Python 3.8 in real deployments (not just unit tests).
- The shutdown process fails due to improper handling of SIGINT signals in the server's exit logic
- Surfaces as: [2021-08-19 11:01:19,614][25024] INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
Proof / Evidence
- GitHub issue: #1160
- Fix PR: https://github.com/kludex/uvicorn/pull/1269
- First fixed release: 0.16.0
- Affected versions: 0.15
- Reproduced locally: No (not executed)
- Last verified: 2026-02-09
- Confidence: 0.75
- Did this fix it?: Yes (upstream fix exists)
- Own content ratio: 0.31
Discussion
High-signal excerpts from the issue thread (symptoms, repros, edge-cases).
“I'm having the exactly same issue!! Here's the code https://github.com/igormcsouza/kitty-api/blob/master/api/__init__.py I downgrade to 0.14 just to avoid error”
“When I press CTRL + C it sends two signals to handle_exit(), first a SIGNIT followed by a SIGTERM. I'm debugging it.”
“Ok... It was quite fast to find it :sweat_smile: The problem is that on reload mode, we terminate the process on the shutdown(): https://github.com/encode/uvicorn/commit/de53c2366f1df9f55ac88d8bde67cfd314d2d98e”
“> Ok... It was quite fast to find it > > The problem is that on reload mode, we terminate the process on the shutdown():…”
Failure Signature (Search String)
- [2021-08-19 11:01:19,614][25024] INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
Error Message
Stack trace
Error Message
-------------
[2021-08-19 11:01:19,614][25024] INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
[2021-08-19 11:01:23,189][25024] ERROR: Traceback (most recent call last):
File "c:\Users\...\.venv\lib\site-packages\starlette\routing.py", line 624, in lifespan
await receive()
File "c:\Users\...\.venv\lib\site-packages\uvicorn\lifespan\on.py", line 135, in receive
return await self.receive_queue.get()
File "C:\Users\...\python\current\lib\asyncio\queues.py", line 166, in get
await getter
asyncio.exceptions.CancelledError
Stack trace
Error Message
-------------
0:00:00.901396 INFO 13 show_systeminfo Processor: Intel64 Family 6 Model 165 Stepping 2, GenuineIntel
0:00:00.902397 INFO 14 show_systeminfo Machine : AMD64
0:00:00.903399 INFO 15 show_systeminfo Node : DESKTOP-VF4OT9Q
0:00:00.904396 INFO 16 show_systeminfo System : Windows
0:00:00.910397 INFO 17 show_systeminfo Platform : Windows-10-10.0.19044-SP0
0:00:00.911397 INFO 26 startup_event Starting: Tasks
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:53259 (Press CTRL+C to quit)
INFO: Shutting down
INFO: Finished server process [15628]
0:00:01.801396 INFO 30 shutdown_event Shutting down...
0:00:01.802398 INFO 32 shutdown_event Shutting down: Tasks
ERROR: Traceback (most recent call last):
File "C:\Users\darko\AppData\Local\Temp\ONC4D5~1\starlette\routing.py", line 624, in lifespan
File "C:\Users\darko\AppData\Local\Temp\ONC4D5~1\uvicorn\lifespan\on.py", line 135, in receive
File "C:\Users\darko\AppData\Local\Temp\ONC4D5~1\asyncio\queues.py", line 166, in get
asyncio.exceptions.CancelledError
Stack trace
Error Message
-------------
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started parent process [10954]
INFO: Started server process [10958]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [10959]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [10961]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [10960]
INFO: Waiting for application startup.
INFO: Application startup complete.
^CINFO: Shutting down
INFO: Shutting down
INFO: Shutting down
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [10959]
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [10961]
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [10960]
INFO: Finished server process [10958]
ERROR: Traceback (most recent call last):
File "/home/user/code/project/.venv/lib/python3.8/site-packages/starlette/routing.py", line 638, in lifespan
await receive()
File "/home/user/code/project/.venv/lib/python3.8/site-packages/uvicorn/lifespan/on.py", l
... (truncated) ...
Minimal Reproduction
async def _lifespan(app):
import asyncio
try:
yield
except asyncio.exceptions.CancelledError:
pass
server_app = Starlette(routes=routes, lifespan=_lifespan)
Environment
- Python: 3.8
What Broke
The server does not shut down gracefully, leading to potential data loss and unhandled exceptions.
Why It Broke
The shutdown process fails due to improper handling of SIGINT signals in the server's exit logic
Fix Options (Details)
Option A — Upgrade to fixed release Safe default (recommended)
Upgrade to version 0.16.0 or later.
Use when you can deploy the upstream fix. It is usually lower-risk than long-lived workarounds.
Option D — Guard side-effects with OnceOnly Guardrail for side-effects
Mitigate duplicate external side-effects under retries/timeouts/agent loops by gating the operation before calling external systems.
- Place OnceOnly between your code/agent and real side-effects (Stripe, emails, CRM, APIs).
- Use a stable key per side-effect (e.g., customer_id + action + idempotency_key).
- Fail-safe: configure fail-open vs fail-closed based on blast radius and spend risk.
Show example snippet (optional)
from onceonly import OnceOnly
import os
once = OnceOnly(api_key=os.environ["ONCEONLY_API_KEY"], fail_open=True)
# Stable idempotency key per real side-effect.
# Use a request id / job id / webhook delivery id / Stripe event id, etc.
event_id = "evt_..." # replace
key = f"stripe:webhook:{event_id}"
res = once.check_lock(key=key, ttl=3600)
if res.duplicate:
return {"status": "already_processed"}
# Safe to execute the side-effect exactly once.
handle_event(event_id)
Fix reference: https://github.com/kludex/uvicorn/pull/1269
First fixed release: 0.16.0
Last verified: 2026-02-09. Validate in your environment.
When NOT to Use This Fix
- Do not apply this fix if your application relies on the previous shutdown behavior.
- Do not use this to hide logic bugs or data corruption. Use it to block duplicate external side-effects and enforce tool permissions/spend caps.
Verify Fix
Re-run the minimal reproduction on your broken version, then apply the fix and re-run.
Did This Fix Work in Your Case?
Quick signal helps us prioritize which fixes to verify and improve.
Prevention
- Add a stress test that runs high-concurrency workloads and fails on thread dumps / blocked locks.
- Enable watchdog dumps in prod (faulthandler, thread dump endpoint) to capture deadlocks quickly.
- Make timeouts explicit and test them (unit + integration) to avoid silent behavior changes.
- Instrument retries (attempt count + reason) and alert on spikes to catch dependency slowdowns.
Version Compatibility Table
| Version | Status |
|---|---|
| 0.15 | Broken |
| 0.16.0 | Fixed |
Related Issues
No related fixes found.
Sources
We don’t republish the full GitHub discussion text. Use the links above for context.