From 2e625af44df346c5c0da56ce61fb101fe389cbb5 Mon Sep 17 00:00:00 2001 From: Leo Balduf Date: Tue, 4 Apr 2017 16:05:09 +0200 Subject: [PATCH] http: fix HTTP shutdown panicking --- cmd/chihaya/main.go | 6 +++--- frontend/http/frontend.go | 7 +++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cmd/chihaya/main.go b/cmd/chihaya/main.go index 8b39f9e..6033f19 100644 --- a/cmd/chihaya/main.go +++ b/cmd/chihaya/main.go @@ -124,7 +124,7 @@ func rootCmdRun(cmd *cobra.Command, args []string) error { close(shutdown) closed = true } else { - log.Infoln(bufErr) + log.Errorln(bufErr) } bufErr = err } @@ -175,7 +175,7 @@ func startFrontends(httpConfig httpfrontend.Config, udpConfig udpfrontend.Config go func() { log.Infoln("started serving HTTP on", httpConfig.Addr) if err := httpFrontend.ListenAndServe(); err != nil { - errChan <- errors.New("failed to cleanly shutdown HTTP frontend: " + err.Error()) + errChan <- err } }() } @@ -186,7 +186,7 @@ func startFrontends(httpConfig httpfrontend.Config, udpConfig udpfrontend.Config go func() { log.Infoln("started serving UDP on", udpConfig.Addr) if err := udpFrontend.ListenAndServe(); err != nil { - errChan <- errors.New("failed to cleanly shutdown UDP frontend: " + err.Error()) + errChan <- err } }() } diff --git a/frontend/http/frontend.go b/frontend/http/frontend.go index f5d6352..86a4a62 100644 --- a/frontend/http/frontend.go +++ b/frontend/http/frontend.go @@ -5,6 +5,7 @@ package http import ( "context" "crypto/tls" + "errors" "net" "net/http" "time" @@ -144,10 +145,8 @@ func (t *Frontend) ListenAndServe() error { } // Start the HTTP server and gracefully handle any network errors. - if err := t.s.ListenAndServe(); err != nil { - if opErr, ok := err.(*net.OpError); !ok || (ok && opErr.Op != "accept") { - panic("http: failed to gracefully run HTTP server: " + err.Error()) - } + if err := t.s.ListenAndServe(); err != nil && err != http.ErrServerClosed { + return errors.New("http: failed to run HTTP server: " + err.Error()) } return nil