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

Fix panic handling and add a new exit code and a test for panics #2997

Merged
merged 1 commit into from
Mar 31, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
stdlog "log"
"runtime/debug"
"strconv"
"strings"
"sync"
Expand All @@ -17,6 +18,7 @@ import (

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/log"
)
Expand Down Expand Up @@ -88,6 +90,17 @@ func (c *rootCommand) execute() {
c.globalState.OSExit(exitCode)
}()

defer func() {
if r := recover(); r != nil {
exitCode = int(exitcodes.GoPanic)
err := fmt.Errorf("unexpected k6 panic: %s\n%s", r, debug.Stack())
if c.loggerIsRemote {
c.globalState.FallbackLogger.Error(err)
}
c.globalState.Logger.Error(err)
}
}()

err := c.cmd.Execute()
if err == nil {
exitCode = 0
Expand Down
24 changes: 24 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"testing"

"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
"go.k6.io/k6/cmd/tests"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/lib/testutils"
)

Expand Down Expand Up @@ -37,3 +39,25 @@ func TestDeprecatedOptionWarning(t *testing.T) {
assert.False(t, testutils.LogContains(logMsgs, logrus.InfoLevel, "logformat"))
assert.Contains(t, ts.Stdout.String(), `--logformat has been deprecated`)
}

func TestPanicHandling(t *testing.T) {
t.Parallel()

ts := tests.NewGlobalTestState(t)
ts.CmdArgs = []string{"k6", "panic"}
ts.ExpectedExitCode = int(exitcodes.GoPanic)

rootCmd := newRootCommand(ts.GlobalState)
rootCmd.cmd.AddCommand(&cobra.Command{
Use: "panic",
RunE: func(cmd *cobra.Command, args []string) error {
panic("oh no, oh no, oh no,no,no,no,no")
Copy link
Contributor

Choose a reason for hiding this comment

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

PTSD dog

},
})
rootCmd.execute()

t.Log(ts.Stderr.String())
logMsgs := ts.LoggerHook.Drain()
assert.True(t, testutils.LogContains(logMsgs, logrus.ErrorLevel, "unexpected k6 panic: oh no"))
assert.True(t, testutils.LogContains(logMsgs, logrus.ErrorLevel, "cmd.TestPanicHandling")) // check stacktrace
}
1 change: 1 addition & 0 deletions errext/exitcodes/codes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ const (
CannotStartRESTAPI ExitCode = 106
ScriptException ExitCode = 107
ScriptAborted ExitCode = 108
GoPanic ExitCode = 109
)