From 865d9b10452e6f026b5658fb4eb1c486717405f1 Mon Sep 17 00:00:00 2001 From: Antonio Murdaca Date: Fri, 19 Jan 2018 16:25:30 +0100 Subject: [PATCH] BACKPORT: Fix BZ#1529300 https://github.com/moby/moby/pull/31662 https://github.com/moby/moby/pull/31852 Signed-off-by: Antonio Murdaca --- containerd/main.go | 33 +++++++++++++++++++-------------- supervisor/supervisor.go | 7 +++---- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/containerd/main.go b/containerd/main.go index 9bf8f7ec9..c0beba789 100644 --- a/containerd/main.go +++ b/containerd/main.go @@ -20,7 +20,7 @@ import ( "github.com/codegangsta/cli" "github.com/cyberdelia/go-metrics-graphite" "github.com/docker/containerd" - "github.com/docker/containerd/api/grpc/server" + grpcserver "github.com/docker/containerd/api/grpc/server" "github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/api/http/pprof" "github.com/docker/containerd/supervisor" @@ -157,10 +157,25 @@ func main() { } func daemon(context *cli.Context) error { + stateDir := context.String("state-dir") + if err := os.MkdirAll(stateDir, 0755); err != nil { + return err + } s := make(chan os.Signal, 2048) signal.Notify(s, syscall.SIGTERM, syscall.SIGINT, syscall.SIGPIPE) + // Split the listen string of the form proto://addr + listenSpec := context.String("listen") + listenParts := strings.SplitN(listenSpec, "://", 2) + if len(listenParts) != 2 { + return fmt.Errorf("bad listen address format %s, expected proto://address", listenSpec) + } + // Register server early to allow healthcheck to be done + server, err := startServer(listenParts[0], listenParts[1]) + if err != nil { + return err + } sv, err := supervisor.New( - context.String("state-dir"), + stateDir, context.String("runtime"), context.String("shim"), context.StringSlice("runtime-args"), @@ -169,6 +184,7 @@ func daemon(context *cli.Context) error { if err != nil { return err } + types.RegisterAPIServer(server, grpcserver.NewServer(sv)) wg := &sync.WaitGroup{} for i := 0; i < 10; i++ { wg.Add(1) @@ -178,16 +194,6 @@ func daemon(context *cli.Context) error { if err := sv.Start(); err != nil { return err } - // Split the listen string of the form proto://addr - listenSpec := context.String("listen") - listenParts := strings.SplitN(listenSpec, "://", 2) - if len(listenParts) != 2 { - return fmt.Errorf("bad listen address format %s, expected proto://address", listenSpec) - } - server, err := startServer(listenParts[0], listenParts[1], sv) - if err != nil { - return err - } for ss := range s { switch ss { case syscall.SIGPIPE: @@ -201,7 +207,7 @@ func daemon(context *cli.Context) error { return nil } -func startServer(protocol, address string, sv *supervisor.Supervisor) (*grpc.Server, error) { +func startServer(protocol, address string) (*grpc.Server, error) { // TODO: We should use TLS. // TODO: Add an option for the SocketGroup. sockets, err := listeners.Init(protocol, address, "", nil) @@ -213,7 +219,6 @@ func startServer(protocol, address string, sv *supervisor.Supervisor) (*grpc.Ser } l := sockets[0] s := grpc.NewServer() - types.RegisterAPIServer(s, server.NewServer(sv)) healthServer := health.NewHealthServer() grpc_health_v1.RegisterHealthServer(s, healthServer) diff --git a/supervisor/supervisor.go b/supervisor/supervisor.go index f8561897e..c4d42254a 100644 --- a/supervisor/supervisor.go +++ b/supervisor/supervisor.go @@ -21,9 +21,6 @@ const ( // New returns an initialized Process supervisor. func New(stateDir string, runtimeName, shimName string, runtimeArgs []string, timeout time.Duration, retainCount int) (*Supervisor, error) { startTasks := make(chan *startTask, 10) - if err := os.MkdirAll(stateDir, 0755); err != nil { - return nil, err - } machine, err := CollectMachineInformation() if err != nil { return nil, err @@ -334,7 +331,9 @@ func (s *Supervisor) restore() error { id := d.Name() container, err := runtime.Load(s.stateDir, id, s.shim, s.timeout) if err != nil { - return err + logrus.WithFields(logrus.Fields{"error": err, "id": id}).Warnf("containerd: failed to load container,removing state directory.") + os.RemoveAll(filepath.Join(s.stateDir, id)) + continue } processes, err := container.Processes() if err != nil {