Gracefull shutdown with MJPEG streams connected
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
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.
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.
- Dominant language
- Python
- Stars
- 9
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
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.
More from labthings/labthings-fastapi
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
labthings/labthings-fastapi#405 ·
-
drop-python-version
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
labthings/labthings-fastapi#354 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
labthings/labthings-fastapi#312 · 1 comment ·
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
labthings/labthings-fastapi#404 · 2 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 63/100
labthings/labthings-fastapi#403 · 1 comment ·
All issues in labthings/labthings-fastapi
Similar issues
-
area: harness bug status: needs-triage
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Human-Agent-Society/reef#625 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 1/5 Under an hour Newbie friendliness 80/100
learningequality/kolibri#15351 · 2 comments ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
-
Name consistency Open
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
eellak/triplestore#65 · 1 comment ·