@@ -82,7 +82,7 @@ func NewEventsStore() *EventsStore {
82
82
// If computing checksums is enabled, new checksums will be computed if needed,
83
83
// or reused existing ones otherwise.
84
84
func (e * EventsStore ) Add (proc * Process ) {
85
- log .Debug ("[cache] EventsStore.Add() %d, %s, %s, total: %d" , proc .ID , proc .Path , proc .Tree , e .Len ())
85
+ log .Debug ("[cache] EventsStore.Add() %d, %s, %s, %d, total: %d" , proc .ID , proc .Path , proc .Tree , proc . Starttime , e .Len ())
86
86
// Add the item to cache ASAP,
87
87
// then calculate the checksums if needed.
88
88
e .UpdateItem (proc )
@@ -96,25 +96,32 @@ func (e *EventsStore) Add(proc *Process) {
96
96
97
97
// UpdateItem updates a cache item
98
98
func (e * EventsStore ) UpdateItem (proc * Process ) {
99
- log .Trace ("[cache] updateItem() updating events store (total: %d), pid: %d, path: %s, %v" , e .Len (), proc .ID , proc .Path , proc .Tree )
99
+ log .Trace ("[cache] updateItem() updating events store (total: %d), pid: %d, path: %s, %d, % v" , e .Len (), proc .ID , proc .Path , proc . Starttime , proc .Tree )
100
100
if proc .Path == "" {
101
101
return
102
102
}
103
103
e .mu .Lock ()
104
+ defer e .mu .Unlock ()
105
+
106
+ oldItem := e .eventByPID [proc .ID ]
107
+
108
+ // Avoid replacing new procs with old ones.
109
+ // This can occur when QueueEventsSize is > 0 and computing the checksum takes more time than expected.
110
+ if oldItem .Proc .Path != proc .Path && oldItem .Proc .Starttime > proc .Starttime {
111
+ log .Trace ("skipping out-of-order updateItem: %s (%d) -> %s (%d)" , oldItem .Proc .Path , oldItem .Proc .Starttime , proc .Path , proc .Starttime )
112
+ return
113
+ }
114
+
104
115
ev := ExecEventItem {
105
116
Proc : * proc ,
106
117
LastSeen : time .Now ().UnixNano (),
107
118
}
108
119
e .eventByPID [proc .ID ] = ev
109
- e .mu .Unlock ()
110
120
}
111
121
112
122
// ReplaceItem replaces an existing process with a new one.
113
123
func (e * EventsStore ) ReplaceItem (oldProc , newProc * Process ) {
114
- log .Trace ("[event inCache, replacement] new: %d, %s -> inCache: %d -> %s - Trees: %s, %s" , newProc .ID , newProc .Path , oldProc .ID , oldProc .Path , oldProc .Tree , newProc .Tree )
115
- // Note: in rare occasions, the process being replaced is the older one.
116
- // if oldProc.Starttime > newProc.Starttime {}
117
- //
124
+ log .Trace ("[event inCache, replacement] new: %d, %s -> inCache: %d -> %s - %d, Trees: %s, %s" , newProc .ID , newProc .Path , oldProc .ID , oldProc .Path , newProc .Starttime , oldProc .Tree , newProc .Tree )
118
125
119
126
newProc .PPID = oldProc .ID
120
127
e .UpdateItem (newProc )
0 commit comments