feat: add graceful shutdown handling - #77
Open
kocaemre wants to merge 1 commit into
Open
Conversation
Signed-off-by: Emre K <110906681+kocaemre@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds an opt-in (default enabled) “graceful shutdown” feature that is intended to coordinate bounded cleanup on SIGINT/SIGTERM, backed by new settings and documentation updates.
Changes:
- Introduces
GracefulShutdownManagerplusregister_graceful_shutdown()for signal-driven shutdown coordination with timeout protection. - Adds
ENABLE_GRACEFUL_SHUTDOWNandSHUTDOWN_TIMEOUT_SECONDSto settings and documents them in.env.exampleandREADME.md. - Adds unit tests covering manager behavior, timeout enforcement, and signal handler registration.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/test_graceful_shutdown.py | Adds unit tests for the shutdown manager and signal registration behavior. |
| template_mcp_server/utils/graceful_shutdown.py | Implements the shutdown manager and signal handler registration helper. |
| template_mcp_server/src/settings.py | Adds new env-backed settings for enabling graceful shutdown and configuring timeout. |
| template_mcp_server/src/main.py | Instantiates/registers the shutdown manager during server startup. |
| README.md | Documents the new configuration flags in the environment variable table. |
| .env.example | Adds example values for the new shutdown configuration flags. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+104
to
+108
| register_graceful_shutdown( | ||
| app, | ||
| shutdown_manager, | ||
| enabled=settings.ENABLE_GRACEFUL_SHUTDOWN, | ||
| ) |
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.
Summary
ENABLE_GRACEFUL_SHUTDOWNandSHUTDOWN_TIMEOUT_SECONDSsettings.GracefulShutdownManagerthat runs registered cleanup callbacks with timeout protection..env.exampleandREADME.md.Why
Production deployments need bounded cleanup during SIGINT/SIGTERM so resource shutdown can be coordinated without noisy already-closed-resource logs.
Closes #44.
Test plan
ModuleNotFoundError: No module named 'template_mcp_server.utils.graceful_shutdown').uv run --python 3.12 python -m pytest tests/test_graceful_shutdown.py -q— 6 passeduv run --python 3.12 python -m pytest tests/test_main.py tests/test_settings.py tests/test_graceful_shutdown.py -q— 44 passed, 1 existing warninguv run --python 3.12 python -m pytest tests -q— 311 passed, 1 skipped, 1 existing warninguv run --python 3.12 ruff check template_mcp_server tests/test_graceful_shutdown.py tests/test_main.py tests/test_settings.py— passeduv run --python 3.12 python -m py_compile template_mcp_server/utils/graceful_shutdown.py template_mcp_server/src/main.py template_mcp_server/src/settings.py tests/test_graceful_shutdown.pygit diff --check