Skip to content

Commit

Permalink
fix: Correct errors.WithStack behaviour (sourcenetwork#984)
Browse files Browse the repository at this point in the history
* Correct typo

* Remove --race ommision from TestWithStack

* Correctly handle varying stack depth generated within this library

Different entry points result in a different depth - this results in a failing test with or without a `race` flag.

Also, when compiling with optimizations enabled the compiler would inline the `New` function resulting in an incorrect result (the --race flag must do this).

* Include ommited test assertions

* Block inlining of other stack-sensitive functions

They arent inlined now as they do stuff in them, and that seems to be all the go compiler cares about, but this should help prevent the introduction of new bugs should this change
  • Loading branch information
AndrewSisley authored Dec 20, 2022
1 parent daa1319 commit c0006b5
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 56 deletions.
35 changes: 25 additions & 10 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ func NewKV(key string, value any) KV {
// pairs provided.
//
// A stacktrace will be yielded if formatting with a `+`, e.g `fmt.Sprintf("%+v", err)`.
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func New(message string, keyvals ...KV) error {
return newError(message, keyvals...)
return withStackTrace(message, 1, keyvals...)
}

// Wrap creates a new error of the given message that contains
// the given inner error, suffixing any key-value pairs provided.
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func Wrap(message string, inner error, keyvals ...KV) error {
err := newError(message, keyvals...)
err := withStackTrace(message, 1, keyvals...)
err.inner = inner
return err
}
Expand All @@ -54,19 +60,28 @@ func Is(err, target error) bool {
return errors.Is(err, target)
}

// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func WithStack(err error, keyvals ...KV) error {
return withStackTrace(err.Error(), keyvals...)
return withStackTrace(err.Error(), 1, keyvals...)
}

func newError(message string, keyvals ...KV) *defraError {
return withStackTrace(message, keyvals...)
}

func withStackTrace(message string, keyvals ...KV) *defraError {
// withStackTrace creates a `defraError` with a stacktrace and the given key-value pairs.
//
// The stacktrace will skip the top `depthToSkip` frames, allowing frames/calls generated from
// within this package to not polute the resultant stacktrace.
//
// This function will not be inlined by the compiler as it will spoil any stacktrace
// generated.
//go:noinline
func withStackTrace(message string, depthToSkip int, keyvals ...KV) *defraError {
stackBuffer := make([]uintptr, MaxStackDepth)

// Skip the first X frames as they are part of this library (and dependencies) and are
// best hidden.
length := runtime.Callers(4, stackBuffer[:])
// best hidden, also account for any parent calls within this library.
const depthFromHereToSkip int = 2
length := runtime.Callers(depthFromHereToSkip+depthToSkip, stackBuffer[:])
stack := stackBuffer[:length]
stackText := toString(stack)

Expand Down
70 changes: 24 additions & 46 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,20 @@ func TestErrorIsDefraError(t *testing.T) {
}

func TestErrorWithStack(t *testing.T) {
err := errors.New("gndjdhs")
errorMessage := "gndjdhs"
err := errors.New(errorMessage)

errWithStach := WithStack(err)
errWithStack := WithStack(err)

result := fmt.Sprintf("%+v", errWithStach)
result := fmt.Sprintf("%+v", errWithStack)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := Error\\(errorMessage\\)", result)
*/

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: ", err.Error()), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorWithStack: errWithStack := WithStack\\(err\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand Down Expand Up @@ -146,23 +139,16 @@ func TestErrorFmtvWithStacktrace(t *testing.T) {
const errorMessage string = "gndjdhs"

err := New(errorMessage)
result := fmt.Sprintf("%+v", err)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.
result := fmt.Sprintf("%+v", err)

// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := Error\\(errorMessage\\)", result)
*/
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Stack: ", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktrace: err := New\\(errorMessage\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand All @@ -174,21 +160,13 @@ func TestErrorFmtvWithStacktraceAndKvps(t *testing.T) {
err := New(errorMessage, NewKV("Kv1", 1), NewKV("Kv2", "2"))
result := fmt.Sprintf("%+v", err)

/*
The Go test flag `-race` messes with the stacktrace causing this function's frame to be ommited from
the stacktrace, as our CI runs with the `-race` flag, these assertions need to be disabled.
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)
// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktraceAndKvps: err := Error\\(errorMessage\\)", result)
*/

// As noted above, we cannot assert that this function's stack frame is included in the trace,
// however we should still assert that the error message is present.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: ", errorMessage), result)
// Assert that the first line starts with the error message and contains this [test] function's stacktrace-line -
// including file, line number, and function reference. An exact string match should not be used as the stacktrace
// is machine dependent.
assert.Regexp(t, fmt.Sprintf("^%s\\. Kv1: 1, Kv2: 2\\. Stack: .*\\/defradb\\/errors\\/errors_test\\.go:[0-9]+ \\([a-zA-Z0-9]*\\)", errorMessage), result)

// Assert that the error contains this function's name, and a print-out of the generating line.
assert.Regexp(t, "TestErrorFmtvWithStacktraceAndKvps: err := New\\(errorMessage, NewKV\\(\"Kv1\", 1\\), NewKV\\(\"Kv2\", \"2\"\\)\\)", result)

// Assert that the next line of the stacktrace is also present.
assert.Regexp(t, ".*\\/testing/testing.go:[0-9]+ \\([a-zA-Z0-9]*\\)", result)
Expand Down

0 comments on commit c0006b5

Please sign in to comment.