Skip to content

server/grpc: improve graceful stop behavior#2892

Open
jejefferson wants to merge 3 commits intomicro:masterfrom
jejefferson:fix/grpc-graceful-stop-timeout
Open

server/grpc: improve graceful stop behavior#2892
jejefferson wants to merge 3 commits intomicro:masterfrom
jejefferson:fix/grpc-graceful-stop-timeout

Conversation

@jejefferson
Copy link
Copy Markdown

Summary

This changes the gRPC server shutdown path to behave more like a real graceful stop for long-lived RPCs and streams.

Current behavior in server/grpc/grpc.go is:

  1. wait on internal wg
  2. only then call grpc.Server.GracefulStop()
  3. force Stop() after 1s

That makes graceful shutdown difficult for services with long-running gRPC calls.

What this patch changes

  • starts grpc.Server.GracefulStop() immediately when shutdown begins
  • adds a configurable graceful timeout via GracefulStopTimeout(time.Duration)
  • only force-calls Stop() after that timeout
  • waits on the internal waitgroup after transport shutdown has started, not before

Why

For gRPC graceful shutdown, the expected behavior is:

  • stop accepting new RPCs as soon as shutdown begins
  • allow already-running RPCs/streams to continue
  • only force-stop after an explicit timeout

With the previous order, GracefulStop() starts too late and the hardcoded 1s fallback is too aggressive for long-lived calls.

Compatibility

  • default behavior is preserved if GracefulStopTimeout(...) is not configured
  • this is a minimal transport-level change; it does not change service lifecycle APIs

Validation

I verified the package still compiles with:

go test ./server/grpc -run '^$'

@jejefferson jejefferson force-pushed the fix/grpc-graceful-stop-timeout branch from 6061c68 to 2d6555c Compare March 31, 2026 22:27
@jejefferson jejefferson force-pushed the fix/grpc-graceful-stop-timeout branch 2 times, most recently from 0d1510a to 7d79cca Compare March 31, 2026 22:38
@jejefferson jejefferson force-pushed the fix/grpc-graceful-stop-timeout branch from 7d79cca to 4b4ce50 Compare March 31, 2026 22:39
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.

1 participant