Skip to content

Commit b84d52c

Browse files
committed
Fix multi-listener deadlock. Fixes #182
1 parent 738ecfa commit b84d52c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

multi_listener.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,37 +30,41 @@ type MultiListener struct {
3030
}
3131

3232
func (self *MultiListener) AcceptUnderlay(underlay Underlay) {
33-
self.lock.Lock()
34-
defer self.lock.Unlock()
35-
3633
log := pfxlog.Logger().WithField("underlayId", underlay.ConnectionId()).
3734
WithField("underlayType", GetUnderlayType(underlay))
3835

3936
chId := underlay.ConnectionId()
4037

41-
if mc, ok := self.channels[chId]; ok {
38+
self.lock.Lock()
39+
mc, ok := self.channels[chId]
40+
self.lock.Unlock()
41+
42+
if ok {
4243
log.Info("found existing channel for underlay")
4344
mc.AcceptUnderlay(underlay)
4445
} else {
4546
log.Info("no existing channel found for underlay")
46-
mc, err := self.channelFactory(underlay, func() {
47+
var err error
48+
mc, err = self.channelFactory(underlay, func() {
4749
self.CloseChannel(chId)
4850
})
4951

5052
if mc != nil {
5153
if err != nil {
5254
pfxlog.Logger().WithError(err).Errorf("failed to create multi-underlay channel")
5355
} else {
56+
self.lock.Lock()
5457
self.channels[chId] = mc
58+
self.lock.Unlock()
5559
}
5660
}
5761
}
5862
}
5963

6064
func (self *MultiListener) CloseChannel(chId string) {
6165
self.lock.Lock()
62-
defer self.lock.Unlock()
6366
delete(self.channels, chId)
67+
self.lock.Unlock()
6468
}
6569

6670
func NewMultiListener(channelF MultiChannelFactory) *MultiListener {

0 commit comments

Comments
 (0)