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

Commit 1a145db

Browse files
authored
Merge pull request #497 from grafana/bug/executioncontext-data-race
Fix the data race with injectedScript in ExecutionContext
2 parents 82bd89c + b4408d6 commit 1a145db

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

common/execution_context.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"errors"
2727
"fmt"
2828
"regexp"
29+
"sync"
2930

3031
"github.com/grafana/xk6-browser/api"
3132
"github.com/grafana/xk6-browser/k6ext"
@@ -71,6 +72,7 @@ type ExecutionContext struct {
7172
session session
7273
frame *Frame
7374
id runtime.ExecutionContextID
75+
isMutex sync.RWMutex
7476
injectedScript api.JSHandle
7577
vu k6modules.VU
7678

@@ -267,9 +269,13 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
267269
"sid:%s stid:%s fid:%s ectxid:%d efurl:%s",
268270
e.sid, e.stid, e.fid, e.id, e.furl)
269271

272+
e.isMutex.RLock()
270273
if e.injectedScript != nil {
271-
return e.injectedScript, nil
274+
injectedScript := e.injectedScript
275+
e.isMutex.RUnlock()
276+
return injectedScript, nil
272277
}
278+
e.isMutex.RUnlock()
273279

274280
var (
275281
suffix = `//# sourceURL=` + evaluationScriptURL
@@ -295,9 +301,11 @@ func (e *ExecutionContext) getInjectedScript(apiCtx context.Context) (api.JSHand
295301
if !ok {
296302
return nil, ErrJSHandleInvalid
297303
}
304+
e.isMutex.Lock()
298305
e.injectedScript = injectedScript
306+
e.isMutex.Unlock()
299307

300-
return e.injectedScript, nil
308+
return injectedScript, nil
301309
}
302310

303311
// Eval evaluates the provided JavaScript within this execution context and

0 commit comments

Comments
 (0)