-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Handle server shutdown gracefully to prevent traceback spam #408
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
Open
Vasuk12
wants to merge
7
commits into
microsoft:main
Choose a base branch
from
Vasuk12:cleaner-termination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2fdd43e
Handle server shutdown gracefully to prevent traceback spam
Vasuk12 1156651
Address Copilot suggestions: thread safety, flag reset, and string fo…
Vasuk12 fb928f6
Add CancelledError handling for graceful server shutdown
Vasuk12 41948fa
Signed-off-by: Vasu <[email protected]>
Vasuk12 397a34c
Signed-off-by: Vasu <[email protected]>
Vasuk12 ec5b0e4
Handle server shutdown errors gracefully using shared status flag
Vasuk12 ca835ca
Merge branch 'main' into cleaner-termination
Vasuk12 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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 |
|---|---|---|
|
|
@@ -75,6 +75,15 @@ | |
| T_model = TypeVar("T_model", bound=BaseModel) | ||
|
|
||
|
|
||
| class ServerShutdownError(Exception): | ||
Vasuk12 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """Raised when the server is shutting down and requests cannot be completed. | ||
|
|
||
| This exception is raised instead of ServerDisconnectedError when we detect | ||
| that the server is permanently unavailable (e.g., during graceful shutdown). | ||
| Callers should handle this gracefully without dumping full tracebacks. | ||
| """ | ||
|
|
||
|
|
||
| class RolloutRequest(BaseModel): | ||
| input: TaskInput | ||
| mode: Optional[Literal["train", "val", "test"]] = None | ||
|
|
@@ -1238,6 +1247,9 @@ def __init__( | |
| self._dequeue_was_successful: bool = False | ||
| self._dequeue_first_unsuccessful: bool = True | ||
|
|
||
| # Track server shutdown state to handle errors gracefully | ||
| self._server_shutting_down: bool = False | ||
|
||
|
|
||
| @property | ||
| def capabilities(self) -> LightningStoreCapabilities: | ||
| """Return the capabilities of the store.""" | ||
|
|
@@ -1287,6 +1299,7 @@ def __setstate__(self, state: Dict[str, Any]): | |
| self._connection_timeout = state["_connection_timeout"] | ||
| self._dequeue_was_successful = False | ||
| self._dequeue_first_unsuccessful = True | ||
| self._server_shutting_down = False | ||
|
|
||
| async def _get_session(self) -> aiohttp.ClientSession: | ||
| # In the proxy process, FastAPI middleware calls | ||
|
|
@@ -1324,6 +1337,7 @@ async def _wait_until_healthy(self, session: aiohttp.ClientSession) -> bool: | |
| """ | ||
| Probe the server's /health until it responds 200 or retries are exhausted. | ||
| Returns True if healthy, False otherwise. | ||
| When this returns False, it indicates the server is shutting down or permanently unavailable. | ||
| """ | ||
| if not self._health_retry_delays: | ||
| client_logger.info("No health retry delays configured; skipping health checks.") | ||
|
|
@@ -1342,9 +1356,12 @@ async def _wait_until_healthy(self, session: aiohttp.ClientSession) -> bool: | |
| client_logger.warning(f"Server is not healthy yet. Retrying in {delay} seconds.") | ||
| if delay > 0.0: | ||
| await asyncio.sleep(delay) | ||
| client_logger.error( | ||
| f"Server is not healthy at {self.server_address}/health after {len(self._health_retry_delays)} retry attempts" | ||
| client_logger.warning( | ||
| f"Server is not healthy at {self.server_address}/health after {len(self._health_retry_delays)} retry attempts. " | ||
| "Server appears to be shutting down." | ||
| ) | ||
| # Mark server as shutting down to handle subsequent errors gracefully | ||
| self._server_shutting_down = True | ||
Vasuk12 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
Vasuk12 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return False | ||
|
|
||
| async def _request_json( | ||
|
|
@@ -1405,6 +1422,15 @@ async def _request_json( | |
| last_exc = net_exc | ||
| client_logger.info(f"Network/session issue will be retried. Retrying the request {method}: {path}") | ||
| if not await self._wait_until_healthy(session): | ||
| # Server is shutting down - handle ServerDisconnectedError gracefully | ||
| if isinstance(net_exc, aiohttp.ServerDisconnectedError) and self._server_shutting_down: | ||
| client_logger.debug( | ||
| f"Server is shutting down. Suppressing ServerDisconnectedError for {method}: {path}" | ||
| ) | ||
| # Raise a specific exception that callers can catch and handle gracefully | ||
| raise ServerShutdownError( | ||
| f"Server is shutting down. Request {method}: {path} cannot be completed." | ||
| ) from net_exc | ||
Vasuk12 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| break # server is not healthy, do not retry | ||
|
|
||
| # exhausted retries | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this isn't the only place with such issue. We'd better handle it in store client.