Skip to content

Commit 73fe268

Browse files
committed
stack: add Snapshot.IsRace()
Cleaner than inspecting manually the first goroutine.
1 parent 7c37089 commit 73fe268

4 files changed

Lines changed: 16 additions & 1 deletion

File tree

internal/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ func processInner(out io.Writer, p *Palette, s stack.Similarity, pf pathFormat,
129129
log.Printf("GOPATH=%s", c.RemoteGOPATHs)
130130
needsEnv := len(c.Goroutines) == 1 && showBanner()
131131
// Bucketing should only be done if no data race was detected.
132-
if c.Goroutines[0].RaceAddr == 0 {
132+
if !c.IsRace() {
133133
a := c.Aggregate(s)
134134
if html == "" {
135135
return writeBucketsToConsole(out, p, a, pf, needsEnv, filter, match)

stack/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,15 @@ func ScanSnapshot(in io.Reader, prefix io.Writer, opts *Opts) (*Snapshot, []byte
207207
return nil, suffix, err
208208
}
209209

210+
// IsRace returns true if a race detector stack trace was found.
211+
//
212+
// Otherwise, it is a normal goroutines snapshot.
213+
//
214+
// When a race condition was detected, it is preferable to not call Aggregate().
215+
func (s *Snapshot) IsRace() bool {
216+
return s.Goroutines[0].RaceAddr != 0
217+
}
218+
210219
func (s *Snapshot) guessPaths() bool {
211220
b := s.findRoots() == 0
212221
for _, r := range s.Goroutines {

stack/context_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,9 @@ func TestGomoduleComplex(t *testing.T) {
13441344
if s == nil {
13451345
t.Fatal("expected snapshot")
13461346
}
1347+
if s.IsRace() {
1348+
t.Fatal("unexpected race")
1349+
}
13471350
compareString(t, "panic: 42\n\n", prefix.String())
13481351
compareString(t, "", string(suffix))
13491352
wantGOROOT := ""

stack/html_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,9 @@ func TestSnapshot_ToHTML(t *testing.T) {
245245
if s.Goroutines[0].RaceAddr == 0 {
246246
t.Fatal("expected a race")
247247
}
248+
if !s.IsRace() {
249+
t.Fatal("expected a race")
250+
}
248251
if err := s.ToHTML(ioutil.Discard, ""); err != nil {
249252
t.Fatal(err)
250253
}

0 commit comments

Comments
 (0)