Skip to content

Commit

Permalink
[release-17.0] Flakes: Synchronize access to logErrStacks in vterrors (
Browse files Browse the repository at this point in the history
…#13827) (#13834)

Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com>
  • Loading branch information
vitess-bot[bot] authored Sep 19, 2023
1 parent 2ea0922 commit fae75a8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
8 changes: 4 additions & 4 deletions go/vt/vterrors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ func TestStackFormat(t *testing.T) {
assertContains(t, got, "middle", false)
assertContains(t, got, "outer", false)

logErrStacks = true
defer func() { logErrStacks = false }()
setLogErrStacks(true)
defer func() { setLogErrStacks(false) }()
got = fmt.Sprintf("%v", err)
assertContains(t, got, "innerMost", true)
assertContains(t, got, "middle", true)
Expand Down Expand Up @@ -340,9 +340,9 @@ func TestWrapping(t *testing.T) {
err3 := Wrapf(err2, "baz")
errorWithoutStack := fmt.Sprintf("%v", err3)

logErrStacks = true
setLogErrStacks(true)
errorWithStack := fmt.Sprintf("%v", err3)
logErrStacks = false
setLogErrStacks(false)

assertEquals(t, err3.Error(), "baz: bar: foo")
assertContains(t, errorWithoutStack, "foo", true)
Expand Down
22 changes: 19 additions & 3 deletions go/vt/vterrors/vterrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,35 @@ import (
"errors"
"fmt"
"io"
"sync"

"github.com/spf13/pflag"

vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
)

// logErrStacks controls whether or not printing errors includes the
// logErrStacks controls whether printing errors includes the
// embedded stack trace in the output.
var logErrStacks bool
var muLogErrStacks sync.Mutex

func getLogErrStacks() bool {
muLogErrStacks.Lock()
defer muLogErrStacks.Unlock()
return logErrStacks
}

func setLogErrStacks(val bool) {
muLogErrStacks.Lock()
defer muLogErrStacks.Unlock()
logErrStacks = val
}

// RegisterFlags registers the command-line options that control vterror
// behavior on the provided FlagSet.
func RegisterFlags(fs *pflag.FlagSet) {
muLogErrStacks.Lock()
defer muLogErrStacks.Unlock()
fs.BoolVar(&logErrStacks, "log_err_stacks", false, "log stack traces for errors")
}

Expand Down Expand Up @@ -161,7 +177,7 @@ func (f *fundamental) Format(s fmt.State, verb rune) {
case 'v':
panicIfError(io.WriteString(s, "Code: "+f.code.String()+"\n"))
panicIfError(io.WriteString(s, f.msg+"\n"))
if logErrStacks {
if getLogErrStacks() {
f.stack.Format(s, verb)
}
return
Expand Down Expand Up @@ -278,7 +294,7 @@ func (w *wrapping) Format(s fmt.State, verb rune) {
if rune('v') == verb {
panicIfError(fmt.Fprintf(s, "%v\n", w.Cause()))
panicIfError(io.WriteString(s, w.msg))
if logErrStacks {
if getLogErrStacks() {
w.stack.Format(s, verb)
}
return
Expand Down

0 comments on commit fae75a8

Please sign in to comment.