Skip to content

Commit 3f1536e

Browse files
committed
Fix underlying slice by providing a new one
When we read off the queue.read slice, it essentially pops the first available element by doing this: case eh.ch <- eh.queue.read[0]: eh.queue.read[0] = Event{} eh.queue.read = eh.queue.read[1:] So the entry where the event used to exist has been overwritten with a fresh instance of Event which will avoid a memory leak. However the issue is that the underlying slice keeps growing. During a long running test the slice could grow to an unnecessarily length. To avoid this issue, when queue.read is empty and is swapped with queue.write, just before swapping them around the existing queue.read is overwritten with a new slice, freeing up the old potentially long slice. This should help avoid memory leaks.
1 parent f61bad6 commit 3f1536e

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

js/modules/k6/browser/common/event_emitter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ func (e *BaseEventEmitter) emit(event string, data any) {
136136
// the read queue until that is again depleted.
137137
if len(eh.queue.read) == 0 {
138138
eh.queue.writeMutex.Lock()
139+
// Clear the read slice before swapping to prevent keeping references
140+
eh.queue.read = make([]Event, 0)
139141
eh.queue.read, eh.queue.write = eh.queue.write, eh.queue.read
140142
eh.queue.writeMutex.Unlock()
141143
}

0 commit comments

Comments
 (0)