diff --git a/api/server/server.go b/api/server/server.go index 65d567c1a0..a4b9fb4c42 100644 --- a/api/server/server.go +++ b/api/server/server.go @@ -76,11 +76,10 @@ func (s *Server) Close() { } // ServeAPI loops through all initialized servers and spawns goroutine -// with Server method for each. It sets CreateMux() as Handler also. +// with Serve() method for each. func (s *Server) ServeAPI() error { var chErrors = make(chan error, len(s.servers)) for _, srv := range s.servers { - srv.srv.Handler = s.CreateMux() go func(srv *HTTPServer) { var err error logrus.Infof("API listen on %s", srv.l.Addr()) @@ -162,9 +161,13 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc { } // InitRouters initializes a list of routers for the server. +// Sets those routers as Handler for each server. func (s *Server) InitRouters(d *daemon.Daemon) { s.addRouter(local.NewRouter(d)) s.addRouter(network.NewRouter(d)) + for _, srv := range s.servers { + srv.srv.Handler = s.CreateMux() + } } // addRouter adds a new router to the server. diff --git a/docker/daemon.go b/docker/daemon.go index 1d8bd0f11c..6a48a90e88 100644 --- a/docker/daemon.go +++ b/docker/daemon.go @@ -235,6 +235,21 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error { logrus.Fatal(err) } + // The serve API routine never exits unless an error occurs + // We need to start it as a goroutine and wait on it so + // daemon doesn't exit + // All servers must be protected with some mechanism (systemd socket, listenbuffer) + // which prevents real handling of request until routes will be set. + serveAPIWait := make(chan error) + go func() { + if err := api.ServeAPI(); err != nil { + logrus.Errorf("ServeAPI error: %v", err) + serveAPIWait <- err + return + } + serveAPIWait <- nil + }() + if err := migrateKey(); err != nil { logrus.Fatal(err) } @@ -262,19 +277,6 @@ func (cli *DaemonCli) CmdDaemon(args ...string) error { api.InitRouters(d) - // The serve API routine never exits unless an error occurs - // We need to start it as a goroutine and wait on it so - // daemon doesn't exit - serveAPIWait := make(chan error) - go func() { - if err := api.ServeAPI(); err != nil { - logrus.Errorf("ServeAPI error: %v", err) - serveAPIWait <- err - return - } - serveAPIWait <- nil - }() - signal.Trap(func() { api.Close() <-serveAPIWait