Repository navigation
Conversation
This replicates the current behaviour of the OFM server, by closing connections when the server is asked to shut down (instead of waiting for them to close). I've subclassed `uvicorn.Server` rather than monkeypatching it as that felt cleaner. As a side-effect it's now easier to catch exceptions raised at server start-up, though I've not made use of this. `uvicorn.run` swallows a lot of stuff with its `system.exit(3)`, but that's no longer a problem.
This starts a server in a subprocess and terminates it while holding the stream open. I've manually verified that this test does not pass if I don't set the shutdown function. Testing that the server hangs if a stream is left open when it's terminated would be slow and painful. I don't think it's necessary.
This is necessary for a clean shutdown at the command line.
rwb27
marked this pull request as draft
November 26, 2026 20:47
We now run the graceful shutdown test again, with `MessageBroker.close_streams` mocked. This ensures that we know the the graceful shutdown code is working, and the test is not broken. This may be an overly paranoid use of an extra second of test suite time, but I think it's worthwhile to validate the test. I did do this manually, but I decided I'd rather have it test that every time, just in case the test gets broken in the future.
rwb27
marked this pull request as ready for review
November 26, 2026 20:56
This uses a `finally:` block to make sure the server shuts down correctly. I've checked manually that a real server does shut down OK, even if the shutdown callback raises an exception. The test does not spin up a server, but it does verify the correct behaviour when the callback raises an exception. The stack trace gets dumped to the terminal, and does point to where the error originated. I think this is better than logging and swallowing the exception. The OFM code this replaces used to log and swallow the exception, because otherwise the server would not shut down. That's because it ran in the shutdown handler responsible for setting a flag to stop the event loop. We are hooking in later in the shutdown process once the flag is already set. That problem doesn't apply so a finally block is all we need.
rwb27
force-pushed
the
mjpeg-graceful-shutdown
branch
from
November 26, 2026 22:57
a872260 to
b252fc1
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR builds on #409 to shut down the MJPEG streams cleanly via the
MessageBrokerwhen the server is terminated.Closes #313
OFM-Feature-Branch: v3-mjpeg-stream-refactor
Skip-OFM-v3-Test
This MR contains the following
uvicorn.runforuvicorn.Server.runand subclassesuvicorn.Serverto customise shutdown behaviour.MessageBroker.close_streams()when the server is instructed to shut down. This closes all active MJPEG streams (and should do the same for websockets in due course).test_mjpeg_stream.py.Writing the test for this prompted me to learn some new bits of
anyioand has resulted in a test that is both realistic and performant (it costs about a second to start the server and I doubt we'll do better). I think it's important to have a few tests that actually start a real HTTP server, bound to a real port, and check it works correctly. Usinganyio.open_process, I've achieved this with much less gnarly code than my previous versions using the (custom)MonitoredProcesshelper, and it does not seem to waste any time. This test could be generalised and used for some other things in due course.I have manually checked that, if I comment out the line setting the shutdown callback, the new test fails with a timeout after one second. I initially didn't want to include this in the tests for speed, but debugging this stuff is tricky enough that I think it's worth the extra second to make sure that the test does fail if we deliberately break the graceful shutdown so it's included in this PR.
Before merge:
close_streamsraises an exception.uvicornshutdown function is always called.main. This branch is based on Mjpeg stream refactor #409 and includes all its commits.AI Contributions
AI was not used as part of this pull request.
Merge checklist:
Any changes to the public API have been updated indocs/src/public_api.rst.New or changed features have been added to (or updated in) the conceptual documentation.Any new or updated dependencies have been added to the project configuration (e.g.,pyproject.toml).Any decrease in test coverage has been justified.test-against-ofm-v3job passes, ortest-against-ofm-feature-branchpasses.test-against-ofm-feature-branch.This code has been tested manually against simulated hardware (detail tests in "Before merge").This code has been tested against real hardware (detail tests in "Before merge").