Skip to content

Commit

Permalink
pkg/trace/api: ensure listener gets closed only once (#3276)
Browse files Browse the repository at this point in the history
* pkg/trace/api: ensure listener gets closed only once

This change fixes a non-critical panic which was occurring with go1.10.
In some situations, Close would get called multiple times on the
listener when exiting the trace-agent. For details, see golang/go#24803
  • Loading branch information
gbbr committed Apr 9, 2019
1 parent 5e1bec3 commit 6c062b7
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pkg/trace/api/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ type rateLimitedListener struct {
connLease int32 // How many connections are available for this listener before rate-limiting kicks in
*net.TCPListener

exit chan struct{}
exit chan struct{}
closed uint32
}

// newRateLimitedListener returns a new wrapped listener, which is non-initialized
Expand Down Expand Up @@ -95,6 +96,11 @@ func (sl *rateLimitedListener) Accept() (net.Conn, error) {

// Close wraps the Close method of the underlying tcp listener
func (sl *rateLimitedListener) Close() error {
if !atomic.CompareAndSwapUint32(&sl.closed, 0, 1) {
// already closed; avoid multiple calls if we're on go1.10
// https://golang.org/issue/24803
return nil
}
sl.exit <- struct{}{}
<-sl.exit
return sl.TCPListener.Close()
Expand Down

0 comments on commit 6c062b7

Please sign in to comment.