From 8b64b70fe4a5aa2877c95aa12c6798b12d3ff7ec Mon Sep 17 00:00:00 2001 From: Mitsuo Heijo Date: Wed, 2 Dec 2020 17:39:54 +0900 Subject: [PATCH] Guard closing quitCh with sync.Once to prevent double close (#8242) * Guard closing quitCh with sync.Once to prevent double close Signed-off-by: Mitsuo Heijo --- web/web.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/web/web.go b/web/web.go index 3602150d4..8c3155983 100644 --- a/web/web.go +++ b/web/web.go @@ -186,6 +186,7 @@ type Handler struct { router *route.Router quitCh chan struct{} + quitOnce sync.Once reloadCh chan chan error options *Options config *config.Config @@ -918,12 +919,14 @@ func (h *Handler) version(w http.ResponseWriter, r *http.Request) { } func (h *Handler) quit(w http.ResponseWriter, r *http.Request) { - select { - case <-h.quitCh: - fmt.Fprintf(w, "Termination already in progress.") - default: - fmt.Fprintf(w, "Requesting termination... Goodbye!") + var closed bool + h.quitOnce.Do(func() { + closed = true close(h.quitCh) + fmt.Fprintf(w, "Requesting termination... Goodbye!") + }) + if !closed { + fmt.Fprintf(w, "Termination already in progress.") } }