Skip to content

Commit

Permalink
Prevent double waitgroup decrement (#10170) (#10175)
Browse files Browse the repository at this point in the history
* Prevent double waitgroup decrement

Co-authored-by: zeripath <art27@cantab.net>
  • Loading branch information
guillep2k and zeripath committed Feb 7, 2020
1 parent e48b460 commit 6af5802
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions modules/graceful/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

Expand Down Expand Up @@ -215,9 +216,12 @@ func (wl *wrappedListener) Accept() (net.Conn, error) {
}
}

closed := int32(0)

c = wrappedConn{
Conn: c,
server: wl.server,
closed: &closed,
}

wl.server.wg.Add(1)
Expand All @@ -241,12 +245,12 @@ func (wl *wrappedListener) File() (*os.File, error) {
type wrappedConn struct {
net.Conn
server *Server
closed *int32
}

func (w wrappedConn) Close() error {
err := w.Conn.Close()
if err == nil {
if atomic.CompareAndSwapInt32(w.closed, 0, 1) {
w.server.wg.Done()
}
return err
return w.Conn.Close()
}

0 comments on commit 6af5802

Please sign in to comment.