Skip to content

Commit

Permalink
Shutdown the gRPC server on interrupt.
Browse files Browse the repository at this point in the history
Fixes the case where gapis would ignore interrupt/term signals,
becuase the gRPC server would not exit when run without an idle
timeout.

Fixes #2530
  • Loading branch information
pmuetschard committed Mar 7, 2019
1 parent 9c2c8bd commit 60f12ee
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion gapis/server/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ func NewWithListener(ctx context.Context, l net.Listener, cfg Config, srvChan ch
}
if cfg.IdleTimeout != 0 {
crash.Go(func() { s.stopIfIdle(ctx, server, cfg.IdleTimeout, stop) })
} else {
crash.Go(func() { s.stopOnInterrupt(ctx, server, stop) })
}
return nil
}, grpc.UnaryInterceptor(auth.ServerInterceptor(cfg.AuthToken)))
Expand Down Expand Up @@ -117,7 +119,7 @@ func (s *grpcServer) inRPC() func() {
}

// stopIfIdle calls GracefulStop on server if there are no writes the the
// keepAlive chan within idleTimeout.
// keepAlive chan within idleTimeout or if the current process is interrupted.
// This function blocks until there's an idle timeout, or ctx is cancelled.
func (s *grpcServer) stopIfIdle(ctx context.Context, server *grpc.Server, idleTimeout time.Duration, stop func()) {
// Split the idleTimeout into N smaller chunks, and check that there was
Expand Down Expand Up @@ -158,6 +160,21 @@ func (s *grpcServer) stopIfIdle(ctx context.Context, server *grpc.Server, idleTi
}
}

// stopOnInterrupt calls GracefulStop on server if the current process is interrupted.
func (s *grpcServer) stopOnInterrupt(ctx context.Context, server *grpc.Server, stop func()) {
stoppedSignal, stopped := task.NewSignal()
defer func() {
stop()
server.GracefulStop()
stopped(ctx)
}()

// Wait for the server to stop before terminating the app.
app.AddCleanupSignal(stoppedSignal)

<-task.ShouldStop(ctx)
}

func (s *grpcServer) addInterrupter(f func()) (remove func()) {
li := s.lastInterrupter
s.lastInterrupter++
Expand Down

0 comments on commit 60f12ee

Please sign in to comment.