Skip to content

Commit

Permalink
Handle term signals gracefully
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <[email protected]>
  • Loading branch information
crosbymichael committed Mar 21, 2016
1 parent 577f9d7 commit c28a87c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 48 deletions.
41 changes: 36 additions & 5 deletions containerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"net"
"os"
"os/signal"
"sync"
"syscall"
"time"

"google.golang.org/grpc"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/docker/containerd"
"github.com/docker/containerd/api/grpc/server"
"github.com/docker/containerd/api/grpc/types"
"github.com/docker/containerd/osutils"
"github.com/docker/containerd/supervisor"
)

Expand Down Expand Up @@ -80,7 +83,11 @@ func main() {
func daemon(address, stateDir string, concurrency int, runtimeName string) error {
// setup a standard reaper so that we don't leave any zombies if we are still alive
// this is just good practice because we are spawning new processes
go reapProcesses()
s := make(chan os.Signal, 2048)
signal.Notify(s, syscall.SIGCHLD, syscall.SIGTERM, syscall.SIGINT)
if err := osutils.SetSubreaper(1); err != nil {
logrus.WithField("error", err).Error("containerd: set subpreaper")
}
sv, err := supervisor.New(stateDir, runtimeName)
if err != nil {
return err
Expand All @@ -94,17 +101,41 @@ func daemon(address, stateDir string, concurrency int, runtimeName string) error
if err := sv.Start(); err != nil {
return err
}
if err := os.RemoveAll(address); err != nil {
server, err := startServer(address, sv)
if err != nil {
return err
}
for ss := range s {
switch ss {
case syscall.SIGCHLD:
if _, err := osutils.Reap(); err != nil {
logrus.WithField("error", err).Warn("containerd: reap child processes")
}
default:
server.Stop()
os.Exit(0)
}
}
return nil
}

func startServer(address string, sv *supervisor.Supervisor) (*grpc.Server, error) {
if err := os.RemoveAll(address); err != nil {
return nil, err
}
l, err := net.Listen(defaultListenType, address)
if err != nil {
return err
return nil, err
}
s := grpc.NewServer()
types.RegisterAPIServer(s, server.NewServer(sv))
logrus.Debugf("containerd: grpc api on %s", address)
return s.Serve(l)
go func() {
logrus.Debugf("containerd: grpc api on %s", address)
if err := s.Serve(l); err != nil {
logrus.WithField("error", err).Fatal("containerd: serve grpc")
}
}()
return s, nil
}

// getDefaultID returns the hostname for the instance host
Expand Down
14 changes: 0 additions & 14 deletions containerd/main_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"log"
"net"
"os"
"os/signal"
"runtime"
"syscall"
"time"
Expand Down Expand Up @@ -62,19 +61,6 @@ func checkLimits() error {
return nil
}

func reapProcesses() {
s := make(chan os.Signal, 2048)
signal.Notify(s, syscall.SIGCHLD)
if err := osutils.SetSubreaper(1); err != nil {
logrus.WithField("error", err).Error("containerd: set subpreaper")
}
for range s {
if _, err := osutils.Reap(); err != nil {
logrus.WithField("error", err).Error("containerd: reap child processes")
}
}
}

func processMetrics() {
var (
g = metrics.NewGauge()
Expand Down
29 changes: 0 additions & 29 deletions containerd/main_windows.go

This file was deleted.

0 comments on commit c28a87c

Please sign in to comment.