From 76a3f24169fcfba8746e1f48961ded7d68ba1bee Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Wed, 20 Oct 2021 07:30:34 -0400 Subject: [PATCH] fix concurrent access crash for handler creator (#772) Co-authored-by: Shelikhoo --- app/log/log_creator.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/app/log/log_creator.go b/app/log/log_creator.go index af4cb210..ba53f2fc 100644 --- a/app/log/log_creator.go +++ b/app/log/log_creator.go @@ -1,6 +1,8 @@ package log import ( + "sync" + "github.com/xtls/xray-core/common" "github.com/xtls/xray-core/common/log" ) @@ -13,16 +15,24 @@ type HandlerCreator func(LogType, HandlerCreatorOptions) (log.Handler, error) var handlerCreatorMap = make(map[LogType]HandlerCreator) +var handlerCreatorMapLock = &sync.RWMutex{} + func RegisterHandlerCreator(logType LogType, f HandlerCreator) error { if f == nil { return newError("nil HandlerCreator") } + handlerCreatorMapLock.Lock() + defer handlerCreatorMapLock.Unlock() + handlerCreatorMap[logType] = f return nil } func createHandler(logType LogType, options HandlerCreatorOptions) (log.Handler, error) { + handlerCreatorMapLock.RLock() + defer handlerCreatorMapLock.RUnlock() + creator, found := handlerCreatorMap[logType] if !found { return nil, newError("unable to create log handler for ", logType)