labthings / labthings/labthings-fastapi
Gracefull shutdown with MJPEG streams connected
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 9
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
If an MJPEG stream is connected then the server will not graefully shutdown.
Partial fix
Add timeout_graceful_shutdown=2, to the uvicorn.run line to ensure that it shuts down within 2 seconds even if the server cannot shut down the streams.
Full fix
In the OpenFlexure server each camera has a kill_mjpeg_streams() method which sets the _streaming attribute of the MJPEG stream to False.
When creating the server we define this function:
def shutdown_call() -> None:
try:
camera_thing = server.things["camera"]
if not isinstance(camera_thing, BaseCamera):
raise RuntimeError("Camera thing is not a BaseCamera")
camera_thing.kill_mjpeg_streams()
except BaseException as e:
# Catch anything and log as it is essential that this
# function cannot raise an unhandled exception or Uvicorn
# will never get a shutdown signal.
LOGGER.error(e, exc_info=True)
And run it with set_shutdown_function(shutdown_call) where:
from uvicorn.main import Server
def set_shutdown_function(shutdown_function: Callable[[], None]) -> None:
"""Ensure a function is called before the shutdown.
This monkey patches the Uvicorn Server's handle_exit. This is needed because
the uvicorn ``lifecycle`` events and FastAPI ``shutdown`` events only fire once
background tasks have completed.
Without this the system exits cleanly only if no client is receiving a
StreamingResponse. This patch is used to stop the async generators that
send streaming responses.
:param shutdown_function: A callable with no arguments or outputs. This
should stop any async generators that may be sending to streaming responses.
"""
original_handler = Server.handle_exit
@wraps(Server.handle_exit)
def handle_exit(*args: Any, **kwargs: Any) -> None:
shutdown_function()
original_handler(*args, **kwargs)
# Ignore the MyPy doesn't want us monkey patching. We have to unless the
# FastAPI lifecycle is fixed.
Server.handle_exit = handle_exit # type: ignore[method-assign]
LathThings FastAPI could keep a list of MJPEG streams on the thing server itself. So then ThingServer.kill_streams() method could kill all registered streams. The Uivcorn.Server would still need to be monkey patched.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the server creation code that calls uvicorn.run and inspect how the camera is exposed through server.things["camera"]. Read the BaseCamera.kill_mjpeg_streams method and Uvicorn Server.handle_exit integration described in the issue. Done means connected MJPEG streams stop during shutdown and the server exits cleanly without requiring the timeout workaround.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- fastapi, python
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100