Skip to content

Commit

Permalink
WIP catching of VU panics
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Oct 29, 2020
1 parent bf826ba commit e35b653
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
package js

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net"
"net/http"
"net/http/cookiejar"
"runtime/debug"
"strconv"
"time"

Expand Down Expand Up @@ -601,9 +603,8 @@ func (u *ActiveVU) RunOnce() error {

func (u *VU) runFn(
ctx context.Context, isDefault bool, fn goja.Callable, args ...goja.Value,
) (goja.Value, bool, time.Duration, error) {
) (v goja.Value, isFullIteration bool, t time.Duration, err error) {
if !u.Runner.Bundle.Options.NoCookiesReset.ValueOrZero() {
var err error
u.state.CookieJar, err = cookiejar.New(nil)
if err != nil {
return goja.Undefined(), false, time.Duration(0), err
Expand All @@ -621,11 +622,26 @@ func (u *VU) runFn(
u.Runtime.Set("__ITER", u.Iteration)
u.Iteration++

defer func() {
if r := recover(); r != nil {
gojaStack := u.Runtime.CaptureCallStack(20, nil)
err = fmt.Errorf("a panic occurred in VU code but was caught, %s", r)
// TODO figure out how to use PanicLevel without panicing .. this might require changing
// the logger we use see
// https://github.com/sirupsen/logrus/issues/1028
// https://github.com/sirupsen/logrus/issues/993
b := new(bytes.Buffer)
for _, s := range gojaStack {
s.Write(b)
}
u.state.Logger.Log(logrus.ErrorLevel, r, "\n", string(debug.Stack()), "\nGoja stack:\n", b.String())
}
}()

startTime := time.Now()
v, err := fn(goja.Undefined(), args...) // Actually run the JS script
v, err = fn(goja.Undefined(), args...) // Actually run the JS script
endTime := time.Now()

var isFullIteration bool
select {
case <-ctx.Done():
isFullIteration = false
Expand Down

0 comments on commit e35b653

Please sign in to comment.