Skip to content

Commit

Permalink
Add mutex around channel close
Browse files Browse the repository at this point in the history
Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
  • Loading branch information
jan25 authored and soheilhy committed Jan 14, 2021
1 parent ae889c5 commit ce11cfd
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ type cMux struct {
root net.Listener
bufLen int
errh ErrorHandler
donec chan struct{}
sls []matchersListener
readTimeout time.Duration
donec chan struct{}
mu sync.Mutex
}

func matchersToMatchWriters(matchers []Matcher) []MatchWriter {
Expand All @@ -139,6 +140,7 @@ func (m *cMux) MatchWithWriters(matchers ...MatchWriter) net.Listener {
ml := muxListener{
Listener: m.root,
connc: make(chan net.Conn, m.bufLen),
donec: make(chan struct{}),
}
m.sls = append(m.sls, matchersListener{ss: matchers, l: ml})
return ml
Expand Down Expand Up @@ -215,6 +217,9 @@ func (m *cMux) Close() {
}

func (m *cMux) closeDoneChans() {
m.mu.Lock()
defer m.mu.Unlock()

select {
case <-m.donec:
// Already closed. Don't close again
Expand Down Expand Up @@ -262,10 +267,7 @@ func (l muxListener) Accept() (net.Conn, error) {
return c, nil
case <-l.donec:
return nil, ErrServerClosed
default:
// do nothing
}
return nil, ErrServerClosed
}

// MuxConn wraps a net.Conn and provides transparent sniffing of connection data.
Expand Down

0 comments on commit ce11cfd

Please sign in to comment.