Skip to content

Commit

Permalink
agent: Properly stop the gRPC server
Browse files Browse the repository at this point in the history
This commit attempts to close cleanly the gRPC server so that tracing
will be ended properly.

Fixes kata-containers#445

Signed-off-by: Sebastien Boeuf <sebastien.boeuf@intel.com>
  • Loading branch information
Sebastien Boeuf committed Jan 29, 2019
1 parent a581aeb commit 569f5b8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
58 changes: 56 additions & 2 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type sandbox struct {
enableGrpcTrace bool
sandboxPidNs bool
storages map[string]*sandboxStorage
stopServer chan struct{}
}

var agentFields = logrus.Fields{
Expand Down Expand Up @@ -524,6 +525,44 @@ func (s *sandbox) teardownSharedPidNs() error {
return nil
}

func (s *sandbox) waitForStopServer() {
fieldLogger := agentLog.WithField("subsystem", "stopserverwatcher")

fieldLogger.Info("Waiting for stopServer signal...")

// Wait for DestroySandbox() to signal this thread about the need to
// stop the server.
<-s.stopServer

fieldLogger.Info("stopServer signal received")

if s.server == nil {
fieldLogger.Info("No server initialized, nothing to stop")
return
}

defer fieldLogger.Info("gRPC server stopped")

// Try to gracefully stop the server for a minute
timeout := time.Minute
done := make(chan struct{})
go func() {
s.server.GracefulStop()
close(done)
}()

select {
case <-done:
s.server = nil
return
case <-time.After(timeout):
fieldLogger.WithField("timeout", timeout).Warn("Could not gracefully stop the server")
}

fieldLogger.Info("Force stopping the server now")
s.stopGRPC()
}

func (s *sandbox) listenToUdevEvents() {
fieldLogger := agentLog.WithField("subsystem", "udevlistener")

Expand Down Expand Up @@ -810,6 +849,7 @@ func (s *sandbox) startGRPC() {
defer s.wg.Done()

var err error
var servErr error
for {
agentLog.Info("agent grpc server starts")

Expand All @@ -833,15 +873,26 @@ func (s *sandbox) startGRPC() {
}

// l is closed when Serve() returns
err = grpcServer.Serve(l)
if err != nil {
servErr = grpcServer.Serve(l)
if servErr != nil {
agentLog.WithError(err).Warn("agent grpc server quits")
}

err = s.channel.teardown()
if err != nil {
agentLog.WithError(err).Warn("agent grpc channel teardown failed")
}

// Based on the definition of grpc.Serve(), the function
// returns nil in case of a proper stop triggered by either
// grpc.GracefulStop() or grpc.Stop(). Those calls can only
// be issued by the chain of events coming from DestroySandbox
// and explicitly means the server should not try to listen
// again, as the sandbox is being completely removed.
if servErr == nil {
agentLog.Info("agent grpc server has been explicitly stopped")
return
}
}
}()
}
Expand Down Expand Up @@ -1019,6 +1070,7 @@ func realMain() {
pciDeviceMap: make(map[string]string),
deviceWatchers: make(map[string](chan string)),
storages: make(map[string]*sandboxStorage),
stopServer: make(chan struct{}),
}

if err = s.initLogger(); err != nil {
Expand Down Expand Up @@ -1046,6 +1098,8 @@ func realMain() {
// Start gRPC server.
s.startGRPC()

go s.waitForStopServer()

go s.listenToUdevEvents()

s.wg.Wait()
Expand Down
4 changes: 4 additions & 0 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,10 @@ func (a *agentGRPC) DestroySandbox(ctx context.Context, req *pb.DestroySandboxRe
return emptyResp, err
}

// Close stopServer channel to signal the main agent code to stop
// the server when all gRPC calls will be completed.
close(a.sandbox.stopServer)

a.sandbox.hostname = ""
a.sandbox.id = ""
a.sandbox.containers = make(map[string]*container)
Expand Down

0 comments on commit 569f5b8

Please sign in to comment.