Quansight / ragna

RAG orchestration framework ⛵️
https://ragna.chat
BSD 3-Clause "New" or "Revised" License
181 stars 22 forks source link

gracefully stop API when started by ragna ui #496

Closed pmeier closed 3 months ago

pmeier commented 3 months ago

By default Python does not handle the SIGTERM signal. Thus, currently, when we send a SIGTERM to the process that was started by ragna ui --start-api, the finally branch that is supposed to stop the API is never run:

https://github.com/Quansight/ragna/blob/2066dcdeb949060873a8552449b848b245e0e985/ragna/deploy/_cli/core.py#L170-L173

Meaning, the Ragna API stays up:

❯ ragna ui --start-api &; sleep 10 && kill $! && sleep 10 && ps aux | grep ragna
[1] 22747
INFO:   RagnaDemoAuthentication: You can log in with any username and a matching password.
INFO:     Started server process [22773]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:31476 (Press CTRL+C to quit)
INFO:     127.0.0.1:54172 - "GET / HTTP/1.1" 200 OK
Launching server at http://127.0.0.1:31477
INFO:     127.0.0.1:58566 - "GET / HTTP/1.1" 200 OK
[1]  + 22747 terminated  ragna ui
philip     22773 22.8  0.5 1630504 358968 pts/0  SNl  11:01   0:04 /home/philip/.conda/envs/ragna-deploy-dev/bin/python3.9 -m ragna api --config ./ragna.toml --no-ignore-unavailable-components

This pronounced in our test suite where each UI test spawns the Ragna API, but it never gets cleaned up

❯ pytest tests/deploy/ui --quiet && ps aux | grep ragna
..                                                                                                 [100%]
2 passed in 11.99s
philip     25447 35.7  0.2 1139644 195568 pts/0  Sl   11:11   0:03 /home/philip/.conda/envs/ragna-deploy-dev/bin/python -m ragna api --config /tmp/pytest-of-philip/pytest-26/test_health_chromium_0/ragna.toml --ignore-unavailable-components
philip     25521 68.2  0.3 1290196 199060 pts/0  Sl   11:11   0:03 /home/philip/.conda/envs/ragna-deploy-dev/bin/python -m ragna api --config /tmp/pytest-of-philip/pytest-26/test_start_chat_chromium_0/ragna.toml --ignore-unavailable-components

This PR turns the SIGTERM signal in a regular system exit for ragna ui and thus enables graceful shutdown.

❯ ragna ui --start-api &; sleep 10 && kill $! && sleep 10 && ps aux | grep ragna
[1] 25853
INFO:   RagnaDemoAuthentication: You can log in with any username and a matching password.
INFO:     Started server process [25879]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://127.0.0.1:31476 (Press CTRL+C to quit)
INFO:     127.0.0.1:49790 - "GET / HTTP/1.1" 200 OK
Launching server at http://127.0.0.1:31477
INFO:     Shutting down
INFO:     Waiting for application shutdown.
INFO:     Application shutdown complete.
INFO:     Finished server process [25879]
[1]  + 25853 done       ragna ui --start-api