-
Notifications
You must be signed in to change notification settings - Fork 1.6k
feat: Backend accept new requests during shutdown grace period #6093
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
0d9eb99
feat: implement a more robust graceful shutdown mechanism
jh-nv 17d26dc
update
jh-nv af969ca
Merge remote-tracking branch 'origin/main' into jihao/graceful_shutdo…
jh-nv e88de26
update
jh-nv 25d3327
update
jh-nv a4e1ac9
Merge remote-tracking branch 'origin/main' into jihao/graceful_shutdo…
jh-nv efb5ff3
update
jh-nv f6288d1
revert: tests/fault_tolerance/migration changes
kthui bc62eae
test: Disable backend shutdown grace period for all migration tests
kthui fe5ac93
Merge remote-tracking branch 'origin/main' into jihao/graceful_shutdo…
kthui 3fac793
refactor: Update default grace period to 5 seconds
kthui bd242d8
Merge branch 'main' into jihao/graceful_shutdown_DIS-1233
kthui 9c515e7
Merge branch 'main' into jihao/graceful_shutdown_DIS-1233
kthui 800af0f
fix the lora endpoints tracking, add multimodal worker endpoints to s…
jh-nv a2286aa
Merge remote-tracking branch 'origin/main' into jihao/graceful_shutdo…
jh-nv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
components/src/dynamo/common/utils/graceful_shutdown.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import asyncio | ||
| import logging | ||
| import os | ||
| import signal | ||
| from typing import Iterable, Optional | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # TODO: make this using cli flag | ||
| _DEFAULT_GRACE_PERIOD_SECS = 5.0 | ||
| _GRACE_PERIOD_ENV = "DYN_GRACEFUL_SHUTDOWN_GRACE_PERIOD_SECS" | ||
| _shutdown_started = asyncio.Event() | ||
|
|
||
|
|
||
| def get_grace_period_seconds() -> float: | ||
| value = os.getenv(_GRACE_PERIOD_ENV) | ||
| if value is None or value == "": | ||
| return _DEFAULT_GRACE_PERIOD_SECS | ||
| try: | ||
| parsed = float(value) | ||
| except ValueError: | ||
| logger.warning( | ||
| "Invalid %s=%r; using default %s", | ||
| _GRACE_PERIOD_ENV, | ||
| value, | ||
| _DEFAULT_GRACE_PERIOD_SECS, | ||
| ) | ||
| return _DEFAULT_GRACE_PERIOD_SECS | ||
| if parsed < 0: | ||
| logger.warning( | ||
| "Negative %s=%r; using 0", | ||
| _GRACE_PERIOD_ENV, | ||
| value, | ||
| ) | ||
| return 0.0 | ||
| return parsed | ||
|
|
||
|
|
||
| async def _unregister_endpoints(endpoints: Iterable) -> None: | ||
| seen = set() | ||
| tasks = [] | ||
| for endpoint in endpoints: | ||
| endpoint_id = id(endpoint) | ||
| if endpoint_id in seen: | ||
| continue | ||
| seen.add(endpoint_id) | ||
| tasks.append(endpoint.unregister_endpoint_instance()) | ||
|
|
||
| if not tasks: | ||
| return | ||
|
|
||
| results = await asyncio.gather(*tasks, return_exceptions=True) | ||
| for result in results: | ||
| if isinstance(result, Exception): | ||
| logger.warning( | ||
| "Failed to unregister endpoint instance from discovery: %s", | ||
| result, | ||
| ) | ||
|
|
||
|
|
||
| async def graceful_shutdown_with_discovery( | ||
| runtime, | ||
| endpoints: Iterable, | ||
| shutdown_event: Optional[asyncio.Event] = None, | ||
| grace_period_s: Optional[float] = None, | ||
| ) -> None: | ||
| if _shutdown_started.is_set(): | ||
| return | ||
| _shutdown_started.set() | ||
|
|
||
| if grace_period_s is None: | ||
| grace_period_s = get_grace_period_seconds() | ||
|
|
||
| logger.info("Received shutdown signal; unregistering endpoints from discovery") | ||
| await _unregister_endpoints(list(endpoints)) | ||
|
|
||
| if grace_period_s > 0: | ||
| logger.info("Grace period %.2fs before stopping endpoints", grace_period_s) | ||
| await asyncio.sleep(grace_period_s) | ||
|
|
||
| if shutdown_event is not None: | ||
| shutdown_event.set() | ||
|
|
||
| logger.info("Initiating runtime shutdown") | ||
| runtime.shutdown() | ||
|
|
||
|
|
||
| def install_signal_handlers( | ||
| loop: asyncio.AbstractEventLoop, | ||
| runtime, | ||
| endpoints: Iterable, | ||
| shutdown_event: Optional[asyncio.Event] = None, | ||
| grace_period_s: Optional[float] = None, | ||
| ) -> None: | ||
| shutdown_task: Optional[asyncio.Task[None]] = None | ||
|
|
||
| def _on_shutdown_done(task: asyncio.Task[None]) -> None: | ||
| nonlocal shutdown_task | ||
| try: | ||
| task.result() | ||
| except asyncio.CancelledError: | ||
| logger.info("Graceful shutdown task cancelled") | ||
| except Exception: | ||
| logger.exception("Graceful shutdown task failed") | ||
| finally: | ||
| if shutdown_task is task: | ||
| shutdown_task = None | ||
|
|
||
| def signal_handler() -> None: | ||
| nonlocal shutdown_task | ||
| if shutdown_task is not None and not shutdown_task.done(): | ||
| logger.debug("Shutdown already in progress; ignoring duplicate signal") | ||
| return | ||
|
|
||
| shutdown_task = asyncio.create_task( | ||
| graceful_shutdown_with_discovery( | ||
| runtime, | ||
| endpoints, | ||
| shutdown_event=shutdown_event, | ||
| grace_period_s=grace_period_s, | ||
| ) | ||
| ) | ||
| shutdown_task.add_done_callback(_on_shutdown_done) | ||
|
|
||
| for sig in (signal.SIGTERM, signal.SIGINT): | ||
| loop.add_signal_handler(sig, signal_handler) | ||
|
|
||
| logger.info( | ||
| "Signal handlers set up for graceful shutdown " | ||
| "(discovery unregister + grace period)" | ||
| ) | ||
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.