Skip to content

Commit 25cbb67

Browse files
authored
internal/stack: Use control flow for state (#110)
In anticipation of parsing more information from stack traces make the stack trace parsing logic more manageable by moving it from a state machine into a layout closer to a recursive descent parser. That is, instead of a central loop that reads input line-by-line and needs to manage its various states: current, result := ... for { input := read() if cond(input) { result.append(current) current = startNew(input) } else { current = accumulate(input) } } result = flush(current) Break it down so that parsing of individual results is its own function, representing the state machine via control flow. result := ... for { input := read() if cond(input) { result.append(parseOne()) } } // where func parseOne(input) { value := ... for ; !cond(input); input = read() { value = accumulate(input) } return value } The net effect of this is to make the parsing logic more maintainable once it gets more complex -- adds more states. For example, to parse more information for individual stacks with a state machine, we'd have to make the main loop more complex. State for an individual stack (e.g. "all the functions in the stack") will leak into the state management for the whole state machine. On the other hand, with this method, we'll only modify parseStack, keeping its responsiblity encapsulated to parsing a single stack trace. This idea was also demonstrated recently in the first section of [Storing Data in Control flow by Russ Cox][1]. [1]: https://research.swtch.com/pcdata#step --- To make it easy to write this parser, we switch from bufio.Reader to bufio.Scanner, and wrap it with the ability to "Unscan": basically "don't move forward on next Scan()". Lastly, we need to bump the `go` directive in go.mod to Go 1.20 to allow use of errors.Join.
1 parent f995fdb commit 25cbb67

File tree

5 files changed

+241
-44
lines changed

5 files changed

+241
-44
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module go.uber.org/goleak
22

3-
go 1.18
3+
go 1.20
44

55
require github.com/stretchr/testify v1.8.0
66

internal/stack/scan.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright (c) 2023 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package stack
22+
23+
import (
24+
"bufio"
25+
"io"
26+
)
27+
28+
// scanner provides a bufio.Scanner the ability to Unscan,
29+
// which allows the current token to be read again
30+
// after the next Scan.
31+
type scanner struct {
32+
*bufio.Scanner
33+
34+
unscanned bool
35+
}
36+
37+
func newScanner(r io.Reader) *scanner {
38+
return &scanner{Scanner: bufio.NewScanner(r)}
39+
}
40+
41+
func (s *scanner) Scan() bool {
42+
if s.unscanned {
43+
s.unscanned = false
44+
return true
45+
}
46+
return s.Scanner.Scan()
47+
}
48+
49+
// Unscan stops the scanner from advancing its position
50+
// for the next Scan.
51+
//
52+
// Bytes and Text will return the same token after next Scan
53+
// that they do right now.
54+
func (s *scanner) Unscan() {
55+
s.unscanned = true
56+
}

internal/stack/scan_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) 2023 Uber Technologies, Inc.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package stack
22+
23+
import (
24+
"strings"
25+
"testing"
26+
27+
"github.com/stretchr/testify/assert"
28+
"github.com/stretchr/testify/require"
29+
)
30+
31+
func TestScanner(t *testing.T) {
32+
scanner := newScanner(strings.NewReader("foo\nbar\nbaz\n"))
33+
34+
require.True(t, scanner.Scan())
35+
assert.Equal(t, "foo", scanner.Text())
36+
37+
require.True(t, scanner.Scan())
38+
assert.Equal(t, "bar", scanner.Text())
39+
40+
scanner.Unscan()
41+
assert.Equal(t, "bar", scanner.Text())
42+
43+
require.True(t, scanner.Scan())
44+
assert.Equal(t, "bar", scanner.Text())
45+
46+
require.True(t, scanner.Scan())
47+
assert.Equal(t, "baz", scanner.Text())
48+
}

internal/stack/stacks.go

Lines changed: 95 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Uber Technologies, Inc.
1+
// Copyright (c) 2017-2023 Uber Technologies, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -21,8 +21,8 @@
2121
package stack
2222

2323
import (
24-
"bufio"
2524
"bytes"
25+
"errors"
2626
"fmt"
2727
"io"
2828
"runtime"
@@ -37,7 +37,9 @@ type Stack struct {
3737
id int
3838
state string
3939
firstFunction string
40-
fullStack *bytes.Buffer
40+
41+
// Full, raw stack trace.
42+
fullStack string
4143
}
4244

4345
// ID returns the goroutine ID.
@@ -52,7 +54,7 @@ func (s Stack) State() string {
5254

5355
// Full returns the full stack trace for this goroutine.
5456
func (s Stack) Full() string {
55-
return s.fullStack.String()
57+
return s.fullStack
5658
}
5759

5860
// FirstFunction returns the name of the first function on the stack.
@@ -67,45 +69,92 @@ func (s Stack) String() string {
6769
}
6870

6971
func getStacks(all bool) []Stack {
70-
var stacks []Stack
72+
trace := getStackBuffer(all)
73+
stacks, err := newStackParser(bytes.NewReader(trace)).Parse()
74+
if err != nil {
75+
// Well-formed stack traces should never fail to parse.
76+
// If they do, it's a bug in this package.
77+
// Panic so we can fix it.
78+
panic(fmt.Sprintf("Failed to parse stack trace: %v\n%s", err, trace))
79+
}
80+
return stacks
81+
}
7182

72-
var curStack *Stack
73-
stackReader := bufio.NewReader(bytes.NewReader(getStackBuffer(all)))
74-
for {
75-
line, err := stackReader.ReadString('\n')
76-
if err == io.EOF {
77-
break
78-
}
79-
if err != nil {
80-
// We're reading using bytes.NewReader which should never fail.
81-
panic("bufio.NewReader failed on a fixed string")
82-
}
83+
type stackParser struct {
84+
scan *scanner
85+
stacks []Stack
86+
errors []error
87+
}
88+
89+
func newStackParser(r io.Reader) *stackParser {
90+
return &stackParser{
91+
scan: newScanner(r),
92+
}
93+
}
94+
95+
func (p *stackParser) Parse() ([]Stack, error) {
96+
for p.scan.Scan() {
97+
line := p.scan.Text()
8398

8499
// If we see the goroutine header, start a new stack.
85-
isFirstLine := false
86100
if strings.HasPrefix(line, "goroutine ") {
87-
// flush any previous stack
88-
if curStack != nil {
89-
stacks = append(stacks, *curStack)
90-
}
91-
id, goState := parseGoStackHeader(line)
92-
curStack = &Stack{
93-
id: id,
94-
state: goState,
95-
fullStack: &bytes.Buffer{},
101+
stack, err := p.parseStack(line)
102+
if err != nil {
103+
p.errors = append(p.errors, err)
104+
continue
96105
}
97-
isFirstLine = true
98-
}
99-
curStack.fullStack.WriteString(line)
100-
if !isFirstLine && curStack.firstFunction == "" {
101-
curStack.firstFunction = parseFirstFunc(line)
106+
p.stacks = append(p.stacks, stack)
102107
}
103108
}
104109

105-
if curStack != nil {
106-
stacks = append(stacks, *curStack)
110+
p.errors = append(p.errors, p.scan.Err())
111+
return p.stacks, errors.Join(p.errors...)
112+
}
113+
114+
// parseStack parses a single stack trace from the given scanner.
115+
// line is the first line of the stack trace, which should look like:
116+
//
117+
// goroutine 123 [runnable]:
118+
func (p *stackParser) parseStack(line string) (Stack, error) {
119+
id, state, err := parseGoStackHeader(line)
120+
if err != nil {
121+
return Stack{}, fmt.Errorf("parse header: %w", err)
107122
}
108-
return stacks
123+
124+
// Read the rest of the stack trace.
125+
var (
126+
firstFunction string
127+
fullStack bytes.Buffer
128+
)
129+
for p.scan.Scan() {
130+
line := p.scan.Text()
131+
132+
if strings.HasPrefix(line, "goroutine ") {
133+
// If we see the goroutine header,
134+
// it's the end of this stack.
135+
// Unscan so the next Scan sees the same line.
136+
p.scan.Unscan()
137+
break
138+
}
139+
140+
fullStack.WriteString(line)
141+
fullStack.WriteByte('\n') // scanner trims the newline
142+
143+
// The first line after the header is the top of the stack.
144+
if firstFunction == "" {
145+
firstFunction, err = parseFirstFunc(line)
146+
if err != nil {
147+
return Stack{}, fmt.Errorf("extract function: %w", err)
148+
}
149+
}
150+
}
151+
152+
return Stack{
153+
id: id,
154+
state: state,
155+
firstFunction: firstFunction,
156+
fullStack: fullStack.String(),
157+
}, nil
109158
}
110159

111160
// All returns the stacks for all running goroutines.
@@ -127,29 +176,33 @@ func getStackBuffer(all bool) []byte {
127176
}
128177
}
129178

130-
func parseFirstFunc(line string) string {
179+
func parseFirstFunc(line string) (string, error) {
131180
line = strings.TrimSpace(line)
132181
if idx := strings.LastIndex(line, "("); idx > 0 {
133-
return line[:idx]
182+
return line[:idx], nil
134183
}
135-
panic(fmt.Sprintf("function calls missing parents: %q", line))
184+
return "", fmt.Errorf("no function found: %q", line)
136185
}
137186

138187
// parseGoStackHeader parses a stack header that looks like:
139188
// goroutine 643 [runnable]:\n
140189
// And returns the goroutine ID, and the state.
141-
func parseGoStackHeader(line string) (goroutineID int, state string) {
142-
line = strings.TrimSuffix(line, ":\n")
190+
func parseGoStackHeader(line string) (goroutineID int, state string, err error) {
191+
// The scanner will have already trimmed the "\n",
192+
// but we'll guard against it just in case.
193+
//
194+
// Trimming them separately makes them both optional.
195+
line = strings.TrimSuffix(strings.TrimSuffix(line, ":"), "\n")
143196
parts := strings.SplitN(line, " ", 3)
144197
if len(parts) != 3 {
145-
panic(fmt.Sprintf("unexpected stack header format: %q", line))
198+
return 0, "", fmt.Errorf("unexpected format: %q", line)
146199
}
147200

148201
id, err := strconv.Atoi(parts[1])
149202
if err != nil {
150-
panic(fmt.Sprintf("failed to parse goroutine ID: %v in line %q", parts[1], line))
203+
return 0, "", fmt.Errorf("bad goroutine ID %q in line %q", parts[1], line)
151204
}
152205

153206
state = strings.TrimSuffix(strings.TrimPrefix(parts[2], "["), "]")
154-
return id, state
207+
return id, state, nil
155208
}

internal/stack/stacks_test.go

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 Uber Technologies, Inc.
1+
// Copyright (c) 2017-2023 Uber Technologies, Inc.
22
//
33
// Permission is hereby granted, free of charge, to any person obtaining a copy
44
// of this software and associated documentation files (the "Software"), to deal
@@ -134,6 +134,46 @@ func TestAllLargeStack(t *testing.T) {
134134
close(done)
135135
}
136136

137+
func TestParseStackErrors(t *testing.T) {
138+
tests := []struct {
139+
name string
140+
give string
141+
wantErr string
142+
}{
143+
{
144+
name: "bad goroutine ID",
145+
give: "goroutine no-number [running]:",
146+
wantErr: `bad goroutine ID "no-number"`,
147+
},
148+
{
149+
name: "not enough parts",
150+
give: "goroutine [running]:",
151+
wantErr: `unexpected format`,
152+
},
153+
{
154+
name: "bad function name",
155+
give: joinLines(
156+
"goroutine 1 [running]:",
157+
"example.com/foo/bar.baz", // no arguments
158+
" example.com/foo/bar.go:123",
159+
),
160+
wantErr: `no function found`,
161+
},
162+
}
163+
164+
for _, tt := range tests {
165+
t.Run(tt.name, func(t *testing.T) {
166+
_, err := newStackParser(strings.NewReader(tt.give)).Parse()
167+
require.Error(t, err)
168+
assert.ErrorContains(t, err, tt.wantErr)
169+
})
170+
}
171+
}
172+
173+
func joinLines(lines ...string) string {
174+
return strings.Join(lines, "\n") + "\n"
175+
}
176+
137177
type byGoroutineID []Stack
138178

139179
func (ss byGoroutineID) Len() int { return len(ss) }

0 commit comments

Comments
 (0)