From dc9fc66f613eb83a073b7896a2bcf443d53206df Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Mon, 28 Oct 2019 03:27:39 -0700 Subject: [PATCH] http: shutdown the actual http server PR #1680 introduced graceful handling for the HTTP server in Thanos, but the graceful `Shutdown` call was being performed on an `http.Server` instance that was *not* running at all. The actual server that was listening for requests was started through `http.Serve`, so there was no reference to the server struct that we could use to shut it down. This was causing all of Thanos to freeze after receiving an exit signal, because the run-group for the HTTP server would never finalize. This seems like an oversight because the `(*Server).srv` field was being properly initialized with an HTTP server. Fix this by calling `ListenAndServe` on our initialized server. Signed-off-by: Vicent Marti --- pkg/server/http.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/pkg/server/http.go b/pkg/server/http.go index 344ebac78f..d7b17a5a77 100644 --- a/pkg/server/http.go +++ b/pkg/server/http.go @@ -2,7 +2,6 @@ package server import ( "context" - "net" "net/http" "net/http/pprof" "time" @@ -22,9 +21,8 @@ type Server struct { comp component.Component prober *prober.Prober - mux *http.ServeMux - srv *http.Server - listener net.Listener + mux *http.ServeMux + srv *http.Server opts options } @@ -55,15 +53,9 @@ func NewHTTP(logger log.Logger, reg *prometheus.Registry, comp component.Compone } func (s *Server) ListenAndServe() error { - l, err := net.Listen("tcp", s.opts.listen) - if err != nil { - return errors.Wrap(err, "listen metrics address") - } - s.listener = l s.prober.SetHealthy() level.Info(s.logger).Log("msg", "listening for requests and metrics", "component", s.comp.String(), "address", s.opts.listen) - - return errors.Wrapf(http.Serve(l, s.mux), "serve %s and metrics", s.comp.String()) + return errors.Wrapf(s.srv.ListenAndServe(), "serve %s and metrics", s.comp.String()) } func (s *Server) Shutdown(err error) {