From bf15535a5bf5783b53e6764268dd1554cde3608e Mon Sep 17 00:00:00 2001 From: sanghee Date: Thu, 3 Jan 2019 22:44:32 +0900 Subject: [PATCH] fix panic: sync: negative WaitGroup counter --- grace/conn.go | 3 +-- grace/server.go | 5 +++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/grace/conn.go b/grace/conn.go index e020f850..32623650 100644 --- a/grace/conn.go +++ b/grace/conn.go @@ -28,12 +28,11 @@ func (c *graceConn) Close() (err error) { }() c.m.Lock() + defer c.m.Unlock() if c.closed { - c.m.Unlock() return } c.server.wg.Done() c.closed = true - c.m.Unlock() return c.Conn.Close() } diff --git a/grace/server.go b/grace/server.go index 513a52a9..ef5cbe7e 100644 --- a/grace/server.go +++ b/grace/server.go @@ -34,6 +34,11 @@ type Server struct { // creating a new service goroutine for each. // The service goroutines read requests and then call srv.Handler to reply to them. func (srv *Server) Serve() (err error) { + defer func() { + if r := recover(); r != nil { + log.Println("wait group counter is negative", r) + } + }() srv.state = StateRunning err = srv.Server.Serve(srv.GraceListener) log.Println(syscall.Getpid(), "Waiting for connections to finish...")