From 54e657af23f4c7fb2e381d6cec14f6a96284f023 Mon Sep 17 00:00:00 2001 From: ankur22 Date: Mon, 7 Aug 2023 11:18:01 +0100 Subject: [PATCH] Move the vu context creation during iteration The context in the vu is not thread safe outside of the iteration or the start or end of the vu. By retrieving the vu's context inside the iteration we are working with the vu's context in a thread safe way. --- browser/registry.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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)