Skip to content

Add signal handling for graceful shutdown in server and improve clean…#597

Merged
helloyongyang merged 1 commit intomainfrom
gp/api
Dec 11, 2025
Merged

Add signal handling for graceful shutdown in server and improve clean…#597
helloyongyang merged 1 commit intomainfrom
gp/api

Conversation

@GACLove
Copy link
Contributor

@GACLove GACLove commented Dec 10, 2025

…up process in DistributedManager

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @GACLove, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the stability and operational reliability of the LightX2V server, particularly in distributed setups. It introduces mechanisms for graceful shutdown upon receiving termination signals and refines the cleanup process for distributed resources, preventing potential resource leaks or unresponsive processes. Additionally, worker processes are made more resilient by intelligently handling master disconnection events, ensuring they can exit cleanly when the orchestrator is no longer available.

Highlights

  • Graceful Server Shutdown: Implemented signal handling for SIGINT and SIGTERM in main.py to ensure the server can shut down gracefully. This includes calling stop_distributed_inference and using os._exit(0) to prevent deadlocks during termination.
  • Robust Distributed Cleanup: Enhanced the DistributedManager.cleanup method in distributed_utils.py by introducing a _shutting_down flag and using a timed threading.Thread to destroy the process group. This prevents cleanup operations from hanging indefinitely and ensures proper resource release.
  • Improved Worker Resilience: Modified the worker loop's error handling in worker.py to specifically detect and gracefully exit when the master process disconnects (e.g., 'Connection closed by peer'). It also now logs barrier failures as warnings and exits the loop to prevent further issues.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces signal handling for graceful shutdown in the server and refines the cleanup process within the DistributedManager. Key improvements include the integration of the signal module, the use of a global _shutdown_requested flag, and a more robust cleanup method in DistributedManager that employs a timed thread for dist.destroy_process_group(). The broadcast_task_data and barrier methods now incorporate checks for the distributed environment's initialization and shutdown status, which significantly enhances system stability. Furthermore, the worker loop has been updated to specifically handle connection-related errors during shutdown, facilitating a more controlled and graceful exit.

logger.info(f"Server rank {rank} received shutdown signal")
if inference_service:
inference_service.stop_distributed_inference()
os._exit(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The use of os._exit(0) in the signal handler is problematic for graceful shutdown. It bypasses normal Python interpreter shutdown procedures, including finally blocks and atexit handlers. This means that any cleanup logic in the finally block of run_server (e.g., inference_service.stop_distributed_inference()) will not be executed if a signal is received.

A more robust approach would be to set a flag in the signal handler and allow the main application loop to detect this flag and exit gracefully, ensuring all finally blocks and cleanup routines are executed. For example, the run_worker_loop could be designed to periodically check _shutdown_requested.

if not dist.is_initialized():
return

import threading
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's generally considered best practice to place all import statements at the top of the file, rather than inside a function. This improves readability and ensures that modules are imported only once.

@helloyongyang helloyongyang merged commit 9df4f0a into main Dec 11, 2025
2 checks passed
@helloyongyang helloyongyang deleted the gp/api branch January 7, 2026 10:09
helloyongyang pushed a commit that referenced this pull request Mar 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants