Skip to content

Commit

Permalink
Fix underlying slice by providing a new one
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Jan 22, 2025
1 parent a9854f1 commit bfd8959
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions js/modules/k6/browser/common/event_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ func (e *BaseEventEmitter) emit(event string, data any) {
// the read queue until that is again depleted.
if len(eh.queue.read) == 0 {
eh.queue.writeMutex.Lock()
// Clear the read slice before swapping to prevent keeping references
eh.queue.read = make([]Event, 0)
eh.queue.read, eh.queue.write = eh.queue.write, eh.queue.read
eh.queue.writeMutex.Unlock()
}
Expand Down

0 comments on commit bfd8959

Please sign in to comment.