Skip to content

Commit 26c0d5b

Browse files
committed
fix: use mutex to prevent race condition when stopping muxer
Fixes #83
1 parent 04d0a38 commit 26c0d5b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

muxer/muxer.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Muxer struct {
2222
sendMutex sync.Mutex
2323
startChan chan bool
2424
doneChan chan bool
25+
stopMutex sync.Mutex
2526
ErrorChan chan error
2627
protocolSenders map[uint16]chan *Segment
2728
protocolReceivers map[uint16]chan *Segment
@@ -45,21 +46,25 @@ func (m *Muxer) Start() {
4546
}
4647

4748
func (m *Muxer) Stop() {
49+
// We use a mutex to prevent this function from being called multiple times
50+
// concurrently, which would cause a race condition
51+
m.stopMutex.Lock()
52+
defer m.stopMutex.Unlock()
4853
// Immediately return if we're already shutting down
4954
select {
5055
case <-m.doneChan:
5156
return
5257
default:
5358
}
59+
// Close doneChan to signify that we're shutting down
60+
close(m.doneChan)
5461
// Close protocol receive channels
5562
// We rely on the individual mini-protocols to close the sender channel
5663
for _, recvChan := range m.protocolReceivers {
5764
close(recvChan)
5865
}
5966
// Close ErrorChan to signify to consumer that we're shutting down
6067
close(m.ErrorChan)
61-
// Close doneChan to signify that we're shutting down
62-
close(m.doneChan)
6368
}
6469

6570
func (m *Muxer) sendError(err error) {

0 commit comments

Comments
 (0)