From fae75a856cb2f5b8cf27152e937448d1364b9917 Mon Sep 17 00:00:00 2001 From: "vitess-bot[bot]" <108069721+vitess-bot[bot]@users.noreply.github.com> Date: Tue, 19 Sep 2023 16:06:56 -0400 Subject: [PATCH] [release-17.0] Flakes: Synchronize access to logErrStacks in vterrors (#13827) (#13834) Co-authored-by: vitess-bot[bot] <108069721+vitess-bot[bot]@users.noreply.github.com> --- go/vt/vterrors/errors_test.go | 8 ++++---- go/vt/vterrors/vterrors.go | 22 +++++++++++++++++++--- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/go/vt/vterrors/errors_test.go b/go/vt/vterrors/errors_test.go index c115fb41686..8c039e5874f 100644 --- a/go/vt/vterrors/errors_test.go +++ b/go/vt/vterrors/errors_test.go @@ -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) @@ -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) diff --git a/go/vt/vterrors/vterrors.go b/go/vt/vterrors/vterrors.go index 3b264b01104..6a322837de9 100644 --- a/go/vt/vterrors/vterrors.go +++ b/go/vt/vterrors/vterrors.go @@ -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") } @@ -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 @@ -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