Shutdown process is broken in 0.15
Checklist
- [x] The bug is reproducible against the latest release and/or
master. - [x] There are no similar issues or pull requests to fix it yet.
Describe the bug
My FastAPI ASGI server cannot shutdown properly with uvicorn==0.15 while it can with 0.14
To reproduce
Setup minimal FastAPI app and add some functions with logs(prints) to shutdown event
Expected behavior
You see all logs(prints) from functions on shutdown
Actual behavior
Get ASGI 'lifespan' protocol appears unsupported. without --lifespan on
Get error trace with --lifespan on
Debugging material
uvicorn scheduler.main:app --host=0.0.0.0 --port ${WEB_PORT:-8000} --reload --lifespan on INFO: Will watch for changes in these directories: ['/home/dmytro/storage/chimplie/projects/raok-main/raok-scheduler'] INFO: Uvicorn running on http://0.0.0.0:8004 (Press CTRL+C to quit) INFO: Started reloader process [177653] using statreload INFO: Started server process [177655] INFO: Waiting for application startup. INFO: Tortoise-ORM started, {'default': <tortoise.backends.asyncpg.client.AsyncpgDBClient object at 0x7f63d4a10e50>}, {'models': {'Task': <class 'scheduler.models.task.Task'>, 'Aerich': <class 'aerich.models.Aerich'>}} INFO: Application startup complete. ^CINFO: Shutting down INFO: Finished server process [177655] ERROR: Exception in 'lifespan' protocol Traceback (most recent call last): File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 84, in main await app(scope, self.receive, self.send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 75, in call return await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/fastapi/applications.py", line 199, in call await super().call(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/applications.py", line 112, in call await self.middleware_stack(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/middleware/errors.py", line 146, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/middleware/cors.py", line 70, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/exceptions.py", line 58, in call await self.app(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/routing.py", line 569, in call await self.lifespan(scope, receive, send) File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/starlette/routing.py", line 544, in lifespan await receive() File "/home/dmytro/.local/share/virtualenvs/raok-scheduler-hpGGYNLi/lib/python3.8/site-packages/uvicorn/lifespan/on.py", line 135, in receive return await self.receive_queue.get() File "/usr/lib64/python3.8/asyncio/queues.py", line 163, in get await getter asyncio.exceptions.CancelledError INFO: Stopping reloader process [177653]

Environment
- Fedora 34 / Python 3.8 / Uvicorn version: 0.15 - bug, 0.14 - ok
- command to run:
uvicorn main:app --host=0.0.0.0 --port 8000 --reload
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(): de53c23
seems more like an issue in op lifespan event, cant reproduce here the actual app would help
Ok... It was quite fast to find it
The problem is that on reload mode, we terminate the process on the
shutdown(): de53c23
I see. I am using a script for same purpose (send SIGTERM to process group) in docker exactly to avoid dead reloader while server still working. It runs into the same problem with CTRL+C as it`s sending signal to whole group and as a result child processes receive two SIGTERMs instead of one.
In my case I decide to use a boolean that indicate if my script is propagating signals to process group or not
this simple app reloads fine and go through lifespan events fine too:
routes = [
Route("/index", index),
]
async def lifespan(app):
print(f'startup lifespan')
yield
print('shutdown lifespan')
app = Starlette(routes=routes, lifespan=lifespan)
/home/lotso/PycharmProjects/uvicorn/venv/bin/python -m uvicorn dev.app:app --reload --lifespan=on --reload-dir=dev --log-config=dev/logging_dev.yaml
INFO: Will watch for changes in these directories: ['/home/lotso/PycharmProjects/uvicorn/dev']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [407820] using watchgod
WARNING asyncio - 44 - [SFJAPI-dev]- Executing <Task pending name='Task-1' coro=<Server.serve() running at /home/lotso/PycharmProjects/uvicorn/./uvicorn/server.py:86> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7fa9758adca0>()] created at /home/lotso/.asdf/installs/python/3.9.5/lib/python3.9/asyncio/locks.py:223> cb=[run_until_complete.<locals>.done_cb()] created at /home/lotso/.asdf/installs/python/3.9.5/lib/python3.9/asyncio/runners.py:44> took 0.167 seconds
startup lifespan
INFO: Started server process [407827]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:38310 - 2021-08-19 09:28:30,450 -'GET / HTTP/1.1' 404 Not Found
INFO: 127.0.0.1:38310 - 2021-08-19 09:28:30,703 -'GET /favicon.ico HTTP/1.1' 404 Not Found
INFO: 127.0.0.1:38310 - 2021-08-19 09:28:35,012 -'GET /index HTTP/1.1' 200 OK
WARNING: WatchGodReload detected file change in '['/home/lotso/PycharmProjects/uvicorn/dev/app.py']'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Finished server process [407827]
shutdown lifespan
INFO: Started server process [409414]
INFO: Waiting for application startup.
INFO: Application startup complete.
WARNING asyncio - 44 - [SFJAPI-dev]- Executing <Task pending name='Task-1' coro=<Server.serve() running at /home/lotso/PycharmProjects/uvicorn/./uvicorn/server.py:86> wait_for=<Future pending cb=[<TaskWakeupMethWrapper object at 0x7f0b05d7ac10>()] created at /home/lotso/.asdf/installs/python/3.9.5/lib/python3.9/asyncio/locks.py:223> cb=[run_until_complete.<locals>.done_cb()] created at /home/lotso/.asdf/installs/python/3.9.5/lib/python3.9/asyncio/runners.py:44> took 0.167 seconds
startup lifespan
INFO: 127.0.0.1:38764 - 2021-08-19 09:29:01,921 -'GET /index HTTP/1.1' 200 OK
Ok... It was quite fast to find it The problem is that on reload mode, we terminate the process on the
shutdown(): de53c23seems more like an issue in op lifespan event, cant reproduce here the actual app would help
I`m using just fastapi app itself

And it shutdown properly without --reload flag, and failed with it
@euri10 I'm reproducing your code, and it does raise CancelledError here (it doesn't handle the lifespan shutdown event).
EDIT: On reload is fine. It's just when you CTRL + C there.
ok can get the error on ctrl+c, thought it was the same as kill -2 which works fine, as well as kill -15 I wouldn't worry too much
That's because kill sends signal to single process, but CTRL+C sends signal to process group. That's why with reloaded up(which send additional signal to worker), web worker would receive two signals instead of one.
I get the same error with Starlette 0.16.0 when I send a SIGINT:
[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
I never had to create a lifespan function in previous versions, but I found that the error disappears if I bootstrap Starlette like so:
async def _lifespan(app):
import asyncio
try:
yield
except asyncio.exceptions.CancelledError:
pass
server_app = Starlette(routes=routes, lifespan=_lifespan)
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
Downgrade is not recommended.
Reload works normally, it's just when you CTRL+C, it will force exit, but network resources are closed anyway.
Even though the problem persist! I tried once more yesterday! That error at the end causes issues on my routine! I will keep on 0.14 until it is fixed!
For passers-by: I encountered a similar issue when setting spaCy's Language.pipe n_process parameter to > 1.
Hello, we're having this issue when we Nuitka compile our app. Here is the output
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
Hello all,
I got this only under a container, but if not running inside a container it works fine.
Hope this helps a little.
I am running uvicorn programatically with workers and getting this issue when I Ctrl-C the main process.
It only happens with workers, I think one of the worker processes is generating the exception since catching the asyncio exception on my uvicorn.run call does not stop the stack trace.
Also, it does not happen every time I shutdown, only randomly/sporadically.
Since I am starting uvicorn from within python, is there some way to prevent the default Ctrl-C behavior and shutdown uvicorn programatically?
Referring to: https://twitter.com/graingert/status/1539697480631197703
How about running your own lifespan task and sending a "lifespan.shutdown.complete" event?
Is this applicable here?
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", line 137, in receive
return await self.receive_queue.get()
File "/home/user/.asdf/installs/python/3.8.13/lib/python3.8/asyncio/queues.py", line 163, in get
await getter
asyncio.exceptions.CancelledError
INFO: Stopping parent process [10954]
I still have this issue on the latest version
please the open a discussion in potential issues with as many details as possible
I have made a new repo with this exact problem, but the problem occurs (for me) only when using Docker. If it makes any difference: I use m1 Mac, so my docker image is linux/arm64.
You can play with the repo:
https://github.com/rafrafek/playground
