Skip to content

Commit da870b6

Browse files
committed
Fix data race in tailBuffer
1 parent 72c8dc1 commit da870b6

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

‎conn.go‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"os"
1010
"os/exec"
1111
"regexp"
12+
"sync"
1213
"time"
1314

1415
"golang.org/x/sync/errgroup"
@@ -151,14 +152,24 @@ func (c conn) waitWithTimeout() error {
151152
}
152153

153154
type tailBuffer struct {
155+
mu sync.Mutex
156+
154157
limit int
155-
bytes.Buffer
158+
buff bytes.Buffer
156159
}
157160

158161
func (b *tailBuffer) Write(p []byte) (n int, err error) {
159-
if len(p)+b.Buffer.Len() > b.limit {
160-
b.Reset()
162+
b.mu.Lock()
163+
defer b.mu.Unlock()
164+
if len(p)+b.buff.Len() > b.limit {
165+
b.buff.Reset()
161166
}
162-
n, err = b.Buffer.Write(p)
167+
n, err = b.buff.Write(p)
163168
return
164169
}
170+
171+
func (b *tailBuffer) String() string {
172+
b.mu.Lock()
173+
defer b.mu.Unlock()
174+
return b.buff.String()
175+
}

0 commit comments

Comments
 (0)