diff --git a/browser/registry.go b/browser/registry.go index 511e26544..b21e29093 100644 --- a/browser/registry.go +++ b/browser/registry.go @@ -230,10 +230,9 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsubscribeFn func()) { var ( - ok bool - data k6event.IterData - ctx = context.Background() - vuCtx = k6ext.WithVU(r.vu.Context(), r.vu) + ok bool + data k6event.IterData + ctx = context.Background() ) for e := range eventsCh { @@ -251,6 +250,12 @@ func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsub return } + // The context in the VU is not thread safe. It can + // be safely accessed during an iteration but not + // before one is started. This is why it is being + // accessed and used here. + vuCtx := k6ext.WithVU(r.vu.Context(), r.vu) + if data, ok = e.Data.(k6event.IterData); !ok { e.Done() k6ext.Abort(vuCtx, "unexpected iteration event data format: %v", e.Data)