Do not block on stream server close.

Signed-off-by: Lantao Liu <lantaol@google.com>
This commit is contained in:
Lantao Liu
2018-02-14 08:41:29 +00:00
parent df461c0d48
commit 6d538ccbf6
3 changed files with 15 additions and 6 deletions

View File

@@ -262,8 +262,21 @@ func (c *criContainerdService) Run(startGRPC bool) error {
<-eventMonitorCloseCh
logrus.Info("Event monitor stopped")
<-streamServerCloseCh
logrus.Info("Stream server stopped")
// There is a race condition with http.Server.Serve.
// When `Close` is called at the same time with `Serve`, `Close`
// may finish first, and `Serve` may still block.
// See https://github.com/golang/go/issues/20239.
// Here we set a 2 second timeout for the stream server wait,
// if it timeout, an error log is generated.
// TODO(random-liu): Get rid of this after https://github.com/golang/go/issues/20239
// is fixed.
const streamServerStopTimeout = 2 * time.Second
select {
case <-streamServerCloseCh:
logrus.Info("Stream server stopped")
case <-time.After(streamServerStopTimeout):
logrus.Errorf("Stream server is not stopped in %q", streamServerStopTimeout)
}
if startGRPC {
// Only wait for grpc server close channel when grpc server is started.
<-grpcServerCloseCh