From 595aeb7e2fd60809077935cec97a5639a8cbe17f Mon Sep 17 00:00:00 2001 From: ankur22 Date: Wed, 2 Aug 2023 12:27:10 +0100 Subject: [PATCH] Add unsubscribe in handleExitEvent We've found that when we perform a e2e test in k6 that the goroutines handling the handleIterEvents do not get cleared away. This is due to us not unsubscribing when the test exits. --- browser/registry.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/browser/registry.go b/browser/registry.go index b4eb28386..511e26544 100644 --- a/browser/registry.go +++ b/browser/registry.go @@ -213,8 +213,6 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist exitSubID, exitCh := vu.Events().Global.Subscribe( k6event.Exit, ) - go r.handleExitEvent(exitCh) - iterSubID, eventsCh := vu.Events().Local.Subscribe( k6event.IterStart, k6event.IterEnd, @@ -223,6 +221,8 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist vu.Events().Local.Unsubscribe(iterSubID) vu.Events().Global.Unsubscribe(exitSubID) } + + go r.handleExitEvent(exitCh, unsubscribe) go r.handleIterEvents(eventsCh, unsubscribe) return r @@ -282,7 +282,9 @@ func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsub } } -func (r *browserRegistry) handleExitEvent(exitCh <-chan *k6event.Event) { +func (r *browserRegistry) handleExitEvent(exitCh <-chan *k6event.Event, unsubscribeFn func()) { + defer unsubscribeFn() + e, ok := <-exitCh if !ok { return