2
0
mirror of https://github.com/soheilhy/cmux.git synced 2024-11-10 03:31:52 +08:00

Add mutex around channel close

Signed-off-by: Abhilash Gnan <abhilashgnan@gmail.com>
This commit is contained in:
Abhilash Gnan 2021-01-05 22:20:47 +01:00 committed by Soheil Hassas Yeganeh
parent ae889c5259
commit ce11cfdf3a

10
cmux.go
View File

@ -114,9 +114,10 @@ type cMux struct {
root net.Listener root net.Listener
bufLen int bufLen int
errh ErrorHandler errh ErrorHandler
donec chan struct{}
sls []matchersListener sls []matchersListener
readTimeout time.Duration readTimeout time.Duration
donec chan struct{}
mu sync.Mutex
} }
func matchersToMatchWriters(matchers []Matcher) []MatchWriter { func matchersToMatchWriters(matchers []Matcher) []MatchWriter {
@ -139,6 +140,7 @@ func (m *cMux) MatchWithWriters(matchers ...MatchWriter) net.Listener {
ml := muxListener{ ml := muxListener{
Listener: m.root, Listener: m.root,
connc: make(chan net.Conn, m.bufLen), connc: make(chan net.Conn, m.bufLen),
donec: make(chan struct{}),
} }
m.sls = append(m.sls, matchersListener{ss: matchers, l: ml}) m.sls = append(m.sls, matchersListener{ss: matchers, l: ml})
return ml return ml
@ -215,6 +217,9 @@ func (m *cMux) Close() {
} }
func (m *cMux) closeDoneChans() { func (m *cMux) closeDoneChans() {
m.mu.Lock()
defer m.mu.Unlock()
select { select {
case <-m.donec: case <-m.donec:
// Already closed. Don't close again // Already closed. Don't close again
@ -262,10 +267,7 @@ func (l muxListener) Accept() (net.Conn, error) {
return c, nil return c, nil
case <-l.donec: case <-l.donec:
return nil, ErrServerClosed return nil, ErrServerClosed
default:
// do nothing
} }
return nil, ErrServerClosed
} }
// MuxConn wraps a net.Conn and provides transparent sniffing of connection data. // MuxConn wraps a net.Conn and provides transparent sniffing of connection data.