Skip to content
This repository was archived by the owner on Jan 30, 2025. It is now read-only.

Commit 54e657a

Browse files
committed
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.
1 parent 9d71ade commit 54e657a

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

browser/registry.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,9 @@ func newBrowserRegistry(vu k6modules.VU, remote *remoteRegistry, pids *pidRegist
230230

231231
func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsubscribeFn func()) {
232232
var (
233-
ok bool
234-
data k6event.IterData
235-
ctx = context.Background()
236-
vuCtx = k6ext.WithVU(r.vu.Context(), r.vu)
233+
ok bool
234+
data k6event.IterData
235+
ctx = context.Background()
237236
)
238237

239238
for e := range eventsCh {
@@ -251,6 +250,12 @@ func (r *browserRegistry) handleIterEvents(eventsCh <-chan *k6event.Event, unsub
251250
return
252251
}
253252

253+
// The context in the VU is not thread safe. It can
254+
// be safely accessed during an iteration but not
255+
// before one is started. This is why it is being
256+
// accessed and used here.
257+
vuCtx := k6ext.WithVU(r.vu.Context(), r.vu)
258+
254259
if data, ok = e.Data.(k6event.IterData); !ok {
255260
e.Done()
256261
k6ext.Abort(vuCtx, "unexpected iteration event data format: %v", e.Data)

0 commit comments

Comments
 (0)