From 0770aced4f7ca3bc002d8b94a43f9970a72b6889 Mon Sep 17 00:00:00 2001 From: Tommy Schaefer Date: Tue, 7 Apr 2015 15:03:20 -0500 Subject: [PATCH] cmd/tipgodoc: Kill godoc process if http.ListenAndServe fails Currently, if for some reason http.ListenAndServe fails, any running running godoc processes don't get killed. I don't think this would ever actually happen because, with godoc being set up in a separate go routine, http.ListenAndServe would always(?) fail before the godoc server started. This change ensures that, if a Proxy has a cmd, it is closed when http.ListenAndServe fails. Change-Id: I0d3bfae0c16bc583248c2052a4d7a84c95127e76 Reviewed-on: https://go-review.googlesource.com/8570 Reviewed-by: Brad Fitzpatrick --- cmd/tipgodoc/tip.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/tipgodoc/tip.go b/cmd/tipgodoc/tip.go index e60bd644..2dd3a7b3 100644 --- a/cmd/tipgodoc/tip.go +++ b/cmd/tipgodoc/tip.go @@ -36,7 +36,11 @@ func main() { p := new(Proxy) go p.run() http.Handle("/", p) - log.Fatal(http.ListenAndServe(":8080", nil)) + + if err := http.ListenAndServe(":8080", nil); err != nil { + p.stop() + log.Fatal(err) + } } // Proxy implements the tip.golang.org server: a reverse-proxy @@ -85,6 +89,14 @@ func (p *Proxy) run() { } } +func (p *Proxy) stop() { + p.mu.Lock() + defer p.mu.Unlock() + if p.cmd != nil { + p.cmd.Process.Kill() + } +} + // poll runs from the run loop goroutine. func (p *Proxy) poll() { heads := gerritMetaMap()