From 78399108ffd4d23931e2d93b13bd2b5171e322ec Mon Sep 17 00:00:00 2001 From: John Fastabend Date: Tue, 2 Aug 2022 19:32:30 -0700 Subject: [PATCH] tetragon: Update eventcache on New() call and cleanup old state Currently, if eventcache.New() is called multiple times the old cache will continue to be used. But, this is problematic if the caller is trying to configure the cache with new parameters, such as duration or exporter. Specifically this is the case in the unit tests where each Test* is creating a new process manager and exporter using a new file so that we have specific logs for each test. But, the current behavior means any events going through the eventcache (after the first test) are then lossed because they attempt to export into the old filename. To fix simply stop the old go() on New() and reconfigure it. When I ran this locally forcing all events through the cache I am now able to PASS multiple unit tests. Signed-off-by: John Fastabend --- pkg/eventcache/eventcache.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/pkg/eventcache/eventcache.go b/pkg/eventcache/eventcache.go index 088966ff277..0514cb4af56 100644 --- a/pkg/eventcache/eventcache.go +++ b/pkg/eventcache/eventcache.go @@ -44,6 +44,7 @@ type cacheObj struct { type Cache struct { objsChan chan cacheObj + done chan bool cache []cacheObj server *server.Server dur time.Duration @@ -141,6 +142,9 @@ func (ec *Cache) loop() { case event := <-ec.objsChan: eventcachemetrics.EventCacheCount.Inc() ec.cache = append(ec.cache, event) + + case <-ec.done: + return } } } @@ -181,11 +185,12 @@ func (ec *Cache) Add(internal *process.ProcessInternal, func NewWithTimer(s *server.Server, dur time.Duration) *Cache { if cache != nil { - return cache + cache.done <- true } cache = &Cache{ objsChan: make(chan cacheObj), + done: make(chan bool), cache: make([]cacheObj, 0), server: s, dur: dur,