Skip to content

Commit

Permalink
k6.check: Fix returned values when iteration is ending
Browse files Browse the repository at this point in the history
Before this check was only returning `false` if it also could emit it,
and true otherwise.

This should rarely be a problem as the same context that is being
checked here is the one that interrupts the VM - so no code will be able
to run after this.

Unfortunately the code checking if it should be emitted is racing with
the one that interrupts the VM. So it is possible for the VM to still
not be interrupted when a `check` returns a wrong value. It is even
possible for more code to be run before the interrupt is actually
called.

The code still checks the context as this also updates the internal
check structure and we don't want to that if the context is done.
The above should be changed with #2869

Fixes #2912
  • Loading branch information
mstoykov committed Feb 14, 2023
1 parent a37aec3 commit b36bb5c
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions js/modules/k6/k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error)
exc = err
}
}
booleanVal := val.ToBoolean()
if !booleanVal {
// A single failure makes the return value false.
succ = false
}

// Emit! (But only if we have a valid context.)
select {
Expand All @@ -221,20 +226,18 @@ func (mi *K6) Check(arg0, checks goja.Value, extras ...goja.Value) (bool, error)
Metadata: commonTagsAndMeta.Metadata,
Value: 0,
}
if val.ToBoolean() {
if booleanVal {
atomic.AddInt64(&check.Passes, 1)
sample.Value = 1
} else {
atomic.AddInt64(&check.Fails, 1)

// A single failure makes the return value false.
succ = false
}

metrics.PushIfNotDone(ctx, state.Samples, sample)
}

if exc != nil {
return succ, exc
return false, exc
}
}

Expand Down

0 comments on commit b36bb5c

Please sign in to comment.