Skip to content

Conversation

@alexpalyan
Copy link
Member

This PR modifies our shutdown logic to only call gRPC.GracefulStop() when an HTTP server is not enabled.

Why is this change needed?

When we run our service on Cloud Run, we primarily use the HTTP server to handle incoming requests, and gRPC traffic is served over the same port. In this mixed HTTP/gRPC setup, the HTTP server is responsible for managing the graceful shutdown of the entire application.

Calling gRPC.GracefulStop() in this scenario can interfere with the HTTP server's shutdown process, potentially causing premature connection termination or race conditions. The gRPC server should be treated as a "guest" that is shut down automatically when the main HTTP server stops listening.

This approach is based on the recommended practice for shutting down servers that handle both gRPC and other traffic on the same port. For more technical details, see this explanation: grpc/grpc-go#1384 (comment).

This change ensures a more stable and predictable graceful shutdown, especially in our Cloud Run environment.

Update Shutdown logic to only gracefully stop the gRPC server if the HTTP
server is not present. This prevents unintended shutdown behavior when both
servers are running.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a shutdown race condition issue by modifying the gRPC server shutdown logic to only call GracefulStop() when no HTTP server is present. This prevents interference between HTTP and gRPC shutdown processes in mixed server environments like Cloud Run.

Key changes:

  • Added conditional check to prevent gRPC shutdown when HTTP server exists
  • Ensures proper graceful shutdown coordination in mixed HTTP/gRPC setups

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Copy link
Contributor

@mattiasavelin mattiasavelin left a comment

Choose a reason for hiding this comment

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

👍

@alexpalyan alexpalyan merged commit 3694540 into master Aug 29, 2025
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.

3 participants