Skip to content

Commit

Permalink
Move the vu context creation during iteration
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ankur22 committed Aug 7, 2023
1 parent 9d71ade commit 54e657a
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions browser/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 54e657a

Please sign in to comment.