@@ -61,6 +61,7 @@ type Muxer struct {
61
61
startChan chan bool
62
62
doneChan chan bool
63
63
waitGroup sync.WaitGroup
64
+ waitGroupMutex sync.Mutex
64
65
protocolSenders map [uint16 ]map [ProtocolRole ]chan * Segment
65
66
protocolReceivers map [uint16 ]map [ProtocolRole ]chan * Segment
66
67
protocolReceiversMutex sync.Mutex
@@ -89,7 +90,9 @@ func New(conn net.Conn) *Muxer {
89
90
// We must do this to break out of pending Read() calls to shut down cleanly
90
91
_ = m .conn .Close ()
91
92
// Wait for other goroutines to shutdown
93
+ m .waitGroupMutex .Lock ()
92
94
m .waitGroup .Wait ()
95
+ m .waitGroupMutex .Unlock ()
93
96
// Close ErrorChan to signify to consumer that we're shutting down
94
97
close (m .errorChan )
95
98
}()
@@ -136,11 +139,20 @@ func (m *Muxer) sendError(err error) {
136
139
}
137
140
138
141
// RegisterProtocol registers the provided protocol ID with the muxer. It returns a channel for sending,
139
- // a channel for receiving, and a channel to know when the muxer is shutting down
142
+ // a channel for receiving, and a channel to know when the muxer is shutting down. If the muxer is shutting
143
+ // down, this function will return nil values.
140
144
func (m * Muxer ) RegisterProtocol (
141
145
protocolId uint16 ,
142
146
protocolRole ProtocolRole ,
143
147
) (chan * Segment , chan * Segment , chan bool ) {
148
+ m .waitGroupMutex .Lock ()
149
+ defer m .waitGroupMutex .Unlock ()
150
+ // Check for shutdown
151
+ select {
152
+ case <- m .doneChan :
153
+ return nil , nil , nil
154
+ default :
155
+ }
144
156
// Generate channels
145
157
senderChan := make (chan * Segment , 10 )
146
158
receiverChan := make (chan * Segment , 10 )
0 commit comments