Skip to content

Commit

Permalink
fix concurrent access crash for handler creator
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokangwang committed Jul 19, 2021
1 parent 8fed55f commit e2d526c
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/log/log_creator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package log

import (
"sync"

"github.com/v2fly/v2ray-core/v4/common"
"github.com/v2fly/v2ray-core/v4/common/log"
)
Expand All @@ -15,16 +17,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)
Expand Down

0 comments on commit e2d526c

Please sign in to comment.