From 0287a97091145831825637431330888de04c7ef9 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Thu, 7 Mar 2024 14:20:12 +0100 Subject: [PATCH 1/2] add mutex to protect access to the internal tail map --- pkg/acquisition/modules/file/file.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pkg/acquisition/modules/file/file.go b/pkg/acquisition/modules/file/file.go index 9ab418a8442..499f8f03cdc 100644 --- a/pkg/acquisition/modules/file/file.go +++ b/pkg/acquisition/modules/file/file.go @@ -11,6 +11,7 @@ import ( "regexp" "strconv" "strings" + "sync" "time" "github.com/fsnotify/fsnotify" @@ -52,6 +53,7 @@ type FileSource struct { logger *log.Entry files []string exclude_regexps []*regexp.Regexp + tailMapMutex *sync.RWMutex } func (f *FileSource) GetUuid() string { @@ -350,7 +352,9 @@ func (f *FileSource) StreamingAcquisition(out chan types.Event, t *tomb.Tomb) er f.logger.Errorf("Could not start tailing file %s : %s", file, err) continue } + f.tailMapMutex.Lock() f.tails[file] = true + f.tailMapMutex.Unlock() t.Go(func() error { defer trace.CatchPanic("crowdsec/acquis/file/live/fsnotify") return f.tailFile(out, t, tail) @@ -412,11 +416,14 @@ func (f *FileSource) monitorNewFiles(out chan types.Event, t *tomb.Tomb) error { continue } + f.tailMapMutex.RLock() if f.tails[event.Name] { + f.tailMapMutex.RUnlock() //we already have a tail on it, do not start a new one logger.Debugf("Already tailing file %s, not creating a new tail", event.Name) break } + f.tailMapMutex.RUnlock() //cf. https://github.com/crowdsecurity/crowdsec/issues/1168 //do not rely on stat, reclose file immediately as it's opened by Tail fd, err := os.Open(event.Name) @@ -453,7 +460,9 @@ func (f *FileSource) monitorNewFiles(out chan types.Event, t *tomb.Tomb) error { logger.Errorf("Could not start tailing file %s : %s", event.Name, err) break } + f.tailMapMutex.Lock() f.tails[event.Name] = true + f.tailMapMutex.Unlock() t.Go(func() error { defer trace.CatchPanic("crowdsec/acquis/tailfile") return f.tailFile(out, t, tail) From ad306a6e11b64b4317621d4e9ebf67fb41c85014 Mon Sep 17 00:00:00 2001 From: Sebastien Blot Date: Thu, 7 Mar 2024 14:21:58 +0100 Subject: [PATCH 2/2] init mutex --- pkg/acquisition/modules/file/file.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/acquisition/modules/file/file.go b/pkg/acquisition/modules/file/file.go index 499f8f03cdc..a0c22657445 100644 --- a/pkg/acquisition/modules/file/file.go +++ b/pkg/acquisition/modules/file/file.go @@ -107,6 +107,7 @@ func (f *FileSource) Configure(yamlConfig []byte, logger *log.Entry) error { } f.watchedDirectories = make(map[string]bool) + f.tailMapMutex = &sync.RWMutex{} f.tails = make(map[string]bool) f.watcher, err = fsnotify.NewWatcher()