From 55e6c15073cf127a0a9a3af3fbc68e85c87f7d93 Mon Sep 17 00:00:00 2001 From: huwenbo Date: Tue, 13 Jun 2017 15:19:51 +0800 Subject: [PATCH 1/3] fix panic: sync: negative WaitGroup counter --- grace/conn.go | 13 ++++++++++++- grace/listener.go | 2 +- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/grace/conn.go b/grace/conn.go index 6807e1ac..f5d2b236 100644 --- a/grace/conn.go +++ b/grace/conn.go @@ -3,11 +3,14 @@ package grace import ( "errors" "net" + "sync" ) type graceConn struct { net.Conn server *Server + m sync.Mutex + closed bool } func (c graceConn) Close() (err error) { @@ -23,6 +26,14 @@ func (c graceConn) Close() (err error) { } } }() + + c.m.Lock() + if c.closed { + c.m.Unlock() + return + } c.server.wg.Done() + c.closed = true + c.m.Unlock() return c.Conn.Close() -} +} \ No newline at end of file diff --git a/grace/listener.go b/grace/listener.go index 823d3cce..7ede63a3 100644 --- a/grace/listener.go +++ b/grace/listener.go @@ -37,7 +37,7 @@ func (gl *graceListener) Accept() (c net.Conn, err error) { tc.SetKeepAlive(true) tc.SetKeepAlivePeriod(3 * time.Minute) - c = graceConn{ + c = &graceConn{ Conn: tc, server: gl.server, } From 3d9286f089c5661641da9651cf56bd4f1666d4db Mon Sep 17 00:00:00 2001 From: huwenbo Date: Tue, 13 Jun 2017 15:34:57 +0800 Subject: [PATCH 2/3] fix panic: sync: negative WaitGroup counter --- grace/conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grace/conn.go b/grace/conn.go index f5d2b236..4553e182 100644 --- a/grace/conn.go +++ b/grace/conn.go @@ -36,4 +36,4 @@ func (c graceConn) Close() (err error) { c.closed = true c.m.Unlock() return c.Conn.Close() -} \ No newline at end of file +} From 2b00b7d66dd6582405aaae64024bbb9590cc9604 Mon Sep 17 00:00:00 2001 From: huwenbo Date: Tue, 13 Jun 2017 20:15:43 +0800 Subject: [PATCH 3/3] fix panic: sync: negative WaitGroup counter --- grace/conn.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/grace/conn.go b/grace/conn.go index 4553e182..6f52045f 100644 --- a/grace/conn.go +++ b/grace/conn.go @@ -13,7 +13,7 @@ type graceConn struct { closed bool } -func (c graceConn) Close() (err error) { +func (c *graceConn) Close() (err error) { defer func() { if r := recover(); r != nil { switch x := r.(type) {