Skip to content

Commit

Permalink
Merge pull request #1177 from keloyang/containerd-panic
Browse files Browse the repository at this point in the history
 Fix a small probability panic for containerd.
  • Loading branch information
mlaventure authored Aug 1, 2017
2 parents f6d7c16 + 98fd0b5 commit be26baa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 30 deletions.
57 changes: 30 additions & 27 deletions containerd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,6 @@ 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
Expand All @@ -181,31 +177,11 @@ func daemon(context *cli.Context) error {
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(
stateDir,
context.String("runtime"),
context.String("shim"),
context.StringSlice("runtime-args"),
context.Duration("start-timeout"),
context.Int("retain-count"))

server, err := startServer(context, listenParts[0], listenParts[1])
if err != nil {
return err
}
types.RegisterAPIServer(server, grpcserver.NewServer(sv))
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
w := supervisor.NewWorker(sv, wg)
go w.Start()
}
if err := sv.Start(); err != nil {
return err
}
for ss := range s {
switch ss {
case syscall.SIGPIPE:
Expand All @@ -219,7 +195,12 @@ func daemon(context *cli.Context) error {
return nil
}

func startServer(protocol, address string) (*grpc.Server, error) {
func startServer(context *cli.Context, protocol, address string) (*grpc.Server, error) {
stateDir := context.String("state-dir")
if err := os.MkdirAll(stateDir, 0755); err != nil {
return nil, err
}

// TODO: We should use TLS.
// TODO: Add an option for the SocketGroup.
sockets, err := listeners.Init(protocol, address, "", nil)
Expand All @@ -234,12 +215,34 @@ func startServer(protocol, address string) (*grpc.Server, error) {
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(s, healthServer)

sv, err := supervisor.New(
stateDir,
context.String("runtime"),
context.String("shim"),
context.StringSlice("runtime-args"),
context.Duration("start-timeout"),
context.Int("retain-count"))
if err != nil {
return nil, err
}
types.RegisterAPIServer(s, grpcserver.NewServer(sv))
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
w := supervisor.NewWorker(sv, wg)
go w.Start()
}

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")
}
}()

if err := sv.Start(); err != nil {
return nil, err
}
return s, nil
}

Expand Down
6 changes: 3 additions & 3 deletions supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ func New(stateDir string, runtimeName, shimName string, runtimeArgs []string, ti
}
go s.exitHandler()
go s.oomHandler()
if err := s.restore(); err != nil {
return nil, err
}
return s, nil
}

Expand Down Expand Up @@ -268,6 +265,9 @@ func (s *Supervisor) notifySubscribers(e Event) {
// therefore it is save to do operations in the handlers that modify state of the system or
// state of the Supervisor
func (s *Supervisor) Start() error {
if err := s.restore(); err != nil {
return err
}
logrus.WithFields(logrus.Fields{
"stateDir": s.stateDir,
"runtime": s.runtime,
Expand Down

0 comments on commit be26baa

Please sign in to comment.