Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion module/grpcserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,4 @@ func (g *GrpcServer) shutdownWorker(ctx irrecoverable.SignalerContext, ready com
Msg("graceful stop timed out; force-stopping gRPC server")
g.server.Stop()
}
<-gracefulDone
}
27 changes: 24 additions & 3 deletions module/grpcserver/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,21 @@ var blockingStreamServiceDesc = grpc.ServiceDesc{
type blockingStreamServer struct {
// started is closed when the stream handler has been entered.
started chan struct{}
// blockDuration will cause `Stream` to block for the set duration
blockDuration time.Duration
}

var _ blockingStreamService = (*blockingStreamServer)(nil)

func (s *blockingStreamServer) Stream(stream grpc.ServerStream) error {
close(s.started)
if s.blockDuration > 0 {
// this is to simulate the case that after `grpcServer.Stop()` is called,
// `<-gracefulDone` channel is still blocking, so that we can verify
// the caller is not waiting for `<-gracefulDone` return before shutdown,
// otherwise, the waiting might be still blocking for longer or indefinitely.
time.Sleep(s.blockDuration)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Besides blocking on the done channel readiness, also add some artificial blocking with time.Sleep, whenever configured.

Comment thread
m-Peter marked this conversation as resolved.
}
<-stream.Context().Done()
return nil
}
Expand All @@ -60,7 +69,10 @@ func TestGrpcServerShutdown_WithActiveStream(t *testing.T) {
gracefulStopTimeout := 200 * time.Millisecond

rawServer := grpc.NewServer()
handler := &blockingStreamServer{started: make(chan struct{})}
handler := &blockingStreamServer{

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

By adding <-gracefulDone as the final statement on shutdownWorker, this test (TestGrpcServerShutdown_WithActiveStream), fails with:

could not close done channel on time: failed to shutdown all components on time

showcasing the blocking that was still possible.

started: make(chan struct{}),
blockDuration: gracefulStopTimeout * 10,
}
rawServer.RegisterService(&blockingStreamServiceDesc, handler)

signalerCtx := atomic.NewPointer[irrecoverable.SignalerContext](nil)
Expand Down Expand Up @@ -114,7 +126,10 @@ func TestGrpcServerShutdown_ShutdownStreamInterceptor(t *testing.T) {
rawServer := grpc.NewServer(
grpc.ChainStreamInterceptor(grpcserver.ShutdownStreamInterceptor(signalerCtx)),
)
handler := &blockingStreamServer{started: make(chan struct{})}
handler := &blockingStreamServer{
started: make(chan struct{}),
blockDuration: time.Second,
}
rawServer.RegisterService(&blockingStreamServiceDesc, handler)

server := grpcserver.NewGrpcServer(
Expand Down Expand Up @@ -159,7 +174,13 @@ func TestGrpcServerShutdown_NoActiveStreams(t *testing.T) {
gracefulStopTimeout := 5 * time.Second

rawServer := grpc.NewServer()
rawServer.RegisterService(&blockingStreamServiceDesc, &blockingStreamServer{started: make(chan struct{})})
rawServer.RegisterService(
&blockingStreamServiceDesc,
&blockingStreamServer{
started: make(chan struct{}),
blockDuration: gracefulStopTimeout * 10,
},
)

signalerCtx := atomic.NewPointer[irrecoverable.SignalerContext](nil)
server := grpcserver.NewGrpcServer(
Expand Down
Loading