From 6d538ccbf69f19723364c3fa5e97b5ec68f4f2b4 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Wed, 14 Feb 2018 08:41:29 +0000 Subject: [PATCH] Do not block on stream server close. Signed-off-by: Lantao Liu --- cluster/gce/cloud-init/master.yaml | 2 -- cluster/gce/cloud-init/node.yaml | 2 -- pkg/server/service.go | 17 +++++++++++++++-- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/cluster/gce/cloud-init/master.yaml b/cluster/gce/cloud-init/master.yaml index 678c08444..d6dc1bcba 100644 --- a/cluster/gce/cloud-init/master.yaml +++ b/cluster/gce/cloud-init/master.yaml @@ -106,7 +106,6 @@ write_files: Restart=always RestartSec=10 RemainAfterExit=yes - RemainAfterExit=yes ExecStartPre=/bin/chmod 544 /home/cri-containerd/opt/cri-containerd/cluster/health-monitor.sh ExecStart=/bin/bash -c 'CRICTL=/home/cri-containerd/usr/local/bin/crictl \ /home/cri-containerd/opt/cri-containerd/cluster/health-monitor.sh' @@ -176,7 +175,6 @@ write_files: Restart=always RestartSec=10 RemainAfterExit=yes - RemainAfterExit=yes ExecStartPre=/bin/chmod 544 /home/kubernetes/bin/health-monitor.sh ExecStart=/home/kubernetes/bin/health-monitor.sh kubelet diff --git a/cluster/gce/cloud-init/node.yaml b/cluster/gce/cloud-init/node.yaml index 6eabddc41..c562279cf 100644 --- a/cluster/gce/cloud-init/node.yaml +++ b/cluster/gce/cloud-init/node.yaml @@ -106,7 +106,6 @@ write_files: Restart=always RestartSec=10 RemainAfterExit=yes - RemainAfterExit=yes ExecStartPre=/bin/chmod 544 /home/cri-containerd/opt/cri-containerd/cluster/health-monitor.sh ExecStart=/bin/bash -c 'CRICTL=/home/cri-containerd/usr/local/bin/crictl \ /home/cri-containerd/opt/cri-containerd/cluster/health-monitor.sh' @@ -175,7 +174,6 @@ write_files: Restart=always RestartSec=10 RemainAfterExit=yes - RemainAfterExit=yes ExecStartPre=/bin/chmod 544 /home/kubernetes/bin/health-monitor.sh ExecStart=/home/kubernetes/bin/health-monitor.sh kubelet diff --git a/pkg/server/service.go b/pkg/server/service.go index 3c06b69f5..f502709db 100644 --- a/pkg/server/service.go +++ b/pkg/server/service.go @@ -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