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

Flakes: Synchronize access to logErrStacks in vterrors #13827

Merged
merged 1 commit into from
Aug 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
Copy link
Contributor

Choose a reason for hiding this comment

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

just FYI this doesn't actually lock the Set the happens during flag parsing. if that's happening during tests you'll need to wrap the underlying bool in a custom pflag.Value implementation that does the locking during its Set call.

Copy link
Contributor

Choose a reason for hiding this comment

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

To tie this back to my comment as well, we could do something like this:

diff --git a/go/vt/vterrors/vterrors.go b/go/vt/vterrors/vterrors.go
index 3b264b0110..78510d79ae 100644
--- a/go/vt/vterrors/vterrors.go
+++ b/go/vt/vterrors/vterrors.go
@@ -91,20 +91,49 @@ import (
 	"errors"
 	"fmt"
 	"io"
+	"sync/atomic"
 
 	"github.com/spf13/pflag"
 
 	vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
 )
 
+// atomicBoolValue is a wrapper for atomic.Bool that implements
+// pflag.Value.
+type atomicBoolValue struct {
+	atomic.Bool
+}
+
 // logErrStacks controls whether or not printing errors includes the
 // embedded stack trace in the output.
-var logErrStacks bool
+var logErrStacks *atomicBoolValue = &atomicBoolValue{}
+
+func (ab *atomicBoolValue) Type() string {
+	// This is the user facing type for the flag.
+	return "bool"
+}
+func (ab *atomicBoolValue) Set(val string) error {
+	switch val {
+	case "true":
+		ab.Store(true)
+	case "false":
+		ab.Store(false)
+	default:
+		return Errorf(vtrpcpb.Code_INVALID_ARGUMENT, "invalid value for log_err_stacks: %s", val)
+	}
+	return nil
+}
+func (ab *atomicBoolValue) String() string {
+	if ab.Load() {
+		return "true"
+	}
+	return "false"
+}
 
 // RegisterFlags registers the command-line options that control vterror
 // behavior on the provided FlagSet.
 func RegisterFlags(fs *pflag.FlagSet) {
-	fs.BoolVar(&logErrStacks, "log_err_stacks", false, "log stack traces for errors")
+	fs.Var(logErrStacks, "log_err_stacks", "log stack traces for errors")
 }
 
 // New returns an error with the supplied message.
@@ -161,7 +190,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 logErrStacks.Load() {
 			f.stack.Format(s, verb)
 		}
 		return
@@ -278,7 +307,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 logErrStacks.Load() {
 			w.stack.Format(s, verb)
 		}
 		return

}

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