Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a mutex to js.VU to prevent multiple asynchrnous calls to RunOnce #1252

Merged
merged 1 commit into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"net/http"
"net/http/cookiejar"
"strconv"
"sync"
"time"

"github.com/dop251/goja"
Expand Down Expand Up @@ -193,6 +194,7 @@ func (r *Runner) newVU(samplesOut chan<- stats.SampleContainer) (*VU, error) {
Console: r.console,
BPool: bpool.NewBufferPool(100),
Samples: samplesOut,
m: &sync.Mutex{},
}
vu.Runtime.Set("console", common.Bind(vu.Runtime, vu.Console, vu.Context))
common.BindToGlobal(vu.Runtime, map[string]interface{}{
Expand Down Expand Up @@ -372,6 +374,8 @@ type VU struct {
// goroutine per call.
interruptTrackedCtx context.Context
interruptCancel context.CancelFunc

m *sync.Mutex
cuonglm marked this conversation as resolved.
Show resolved Hide resolved
}

// Verify that VU implements lib.VU
Expand All @@ -385,6 +389,8 @@ func (u *VU) Reconfigure(id int64) error {
}

func (u *VU) RunOnce(ctx context.Context) error {
u.m.Lock()
defer u.m.Unlock()
// Track the context and interrupt JS execution if it's cancelled.
if u.interruptTrackedCtx != ctx {
interCtx, interCancel := context.WithCancel(context.Background())
Expand Down
51 changes: 51 additions & 0 deletions js/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"net/http"
"os"
"strings"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -534,6 +535,56 @@ func TestVURunInterrupt(t *testing.T) {
}
}

func TestVURunInterruptDoesntPanic(t *testing.T) {
//TODO: figure out why interrupt sometimes fails... data race in goja?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the issue would disappear after we update goja to include this fix: dop251/goja@f95411d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think we even have an issue for this ... maybe we should upgrade goja ... soon ™️

if isWindows {
t.Skip()
}

r1, err := getSimpleRunner("/script.js", `
export default function() { while(true) {} }
`)
require.NoError(t, err)
require.NoError(t, r1.SetOptions(lib.Options{Throw: null.BoolFrom(true)}))

r2, err := NewFromArchive(r1.MakeArchive(), lib.RuntimeOptions{})
require.NoError(t, err)
testdata := map[string]*Runner{"Source": r1, "Archive": r2}
for name, r := range testdata {
name, r := name, r
t.Run(name, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Minute)
defer cancel()
samples := make(chan stats.SampleContainer, 100)
defer close(samples)
go func() {
for range samples {
}
}()
var wg sync.WaitGroup

vu, err := r.newVU(samples)
require.NoError(t, err)
for i := 0; i < 1000; i++ {
wg.Add(1)
newCtx, newCancel := context.WithCancel(ctx)
var ch = make(chan struct{})
go func() {
defer wg.Done()
close(ch)
vuErr := vu.RunOnce(newCtx)
assert.Error(t, vuErr)
assert.Contains(t, vuErr.Error(), "context cancelled")
}()
<-ch
time.Sleep(time.Millisecond * 1) // NOTE: increase this in case of problems ;)
newCancel()
}
wg.Wait()
})
}
}

func TestVUIntegrationGroups(t *testing.T) {
r1, err := getSimpleRunner("/script.js", `
import { group } from "k6";
Expand Down