From c6ec2c669fc9fd94a3a6ad9ab8f8095b441cf32c Mon Sep 17 00:00:00 2001 From: mzz2017 Date: Fri, 1 Jan 2021 18:36:33 +0800 Subject: [PATCH] fix: a potential concurrency problem --- main.go | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/main.go b/main.go index 36dbd45..0380a22 100644 --- a/main.go +++ b/main.go @@ -27,11 +27,6 @@ func main() { } func listen(group *config.Group) { - mMutex.Lock() - if _, ok := mPortDispatcher[group.Port]; !ok { - mPortDispatcher[group.Port] = new([2]dispatcher.Dispatcher) - } - mMutex.Unlock() err := listenWithProtocols(group, protocols[:]) if err != nil { mMutex.Lock() @@ -45,10 +40,17 @@ func listen(group *config.Group) { } func listenWithProtocols(group *config.Group, protocols []string) error { + mMutex.Lock() + if _, ok := mPortDispatcher[group.Port]; !ok { + mPortDispatcher[group.Port] = new([2]dispatcher.Dispatcher) + } + t := mPortDispatcher[group.Port] + mMutex.Unlock() + ch := make(chan error, len(protocols)) for i, protocol := range protocols { d, _ := dispatcher.New(protocol, group) - (*mPortDispatcher[group.Port])[i] = d + (*t)[i] = d go func() { var err error err = d.Listen()