Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cscli explain : avoid concurrent map writes #2113

Merged
merged 3 commits into from
Mar 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/parser/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"reflect"
"strings"
"sync"
"time"

"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
Expand Down Expand Up @@ -243,6 +244,7 @@ type ParserResult struct {
var ParseDump bool
var DumpFolder string
var StageParseCache map[string]map[string][]ParserResult
var StageParseMutex sync.Mutex

func Parse(ctx UnixParserCtx, xp types.Event, nodes []Node) (types.Event, error) {
var event = xp
Expand Down Expand Up @@ -274,17 +276,21 @@ func Parse(ctx UnixParserCtx, xp types.Event, nodes []Node) (types.Event, error)

if ParseDump {
if StageParseCache == nil {
StageParseMutex.Lock()
StageParseCache = make(map[string]map[string][]ParserResult)
StageParseCache["success"] = make(map[string][]ParserResult)
StageParseCache["success"][""] = make([]ParserResult, 0)
StageParseMutex.Unlock()
}
}

for _, stage := range ctx.Stages {
if ParseDump {
StageParseMutex.Lock()
if _, ok := StageParseCache[stage]; !ok {
StageParseCache[stage] = make(map[string][]ParserResult)
}
StageParseMutex.Unlock()
}
/* if the node is forward in stages, seek to this stage */
/* this is for example used by testing system to inject logs in post-syslog-parsing phase*/
Expand Down Expand Up @@ -323,11 +329,15 @@ func Parse(ctx UnixParserCtx, xp types.Event, nodes []Node) (types.Event, error)
clog.Tracef("node (%s) ret : %v", node.rn, ret)
if ParseDump {
if len(StageParseCache[stage][node.Name]) == 0 {
StageParseMutex.Lock()
StageParseCache[stage][node.Name] = make([]ParserResult, 0)
StageParseMutex.Unlock()
}
evtcopy := deepcopy.Copy(event)
parserInfo := ParserResult{Evt: evtcopy.(types.Event), Success: ret}
StageParseMutex.Lock()
StageParseCache[stage][node.Name] = append(StageParseCache[stage][node.Name], parserInfo)
StageParseMutex.Unlock()
}
if ret {
isStageOK = true
Expand Down