Skip to content

Commit 4b29d30

Browse files
authored
Merge pull request golang#185 from spenczar/fix_activity_buffer_race
Fix data race in activityBuffer
2 parents e719f18 + 8ad96e3 commit 4b29d30

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

cmd.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"os/exec"
7+
"sync"
78
"time"
89
)
910

@@ -52,8 +53,8 @@ func (c *monitoredCmd) run() error {
5253

5354
func (c *monitoredCmd) hasTimedOut() bool {
5455
t := time.Now().Add(-c.timeout)
55-
return c.stderr.lastActivity.Before(t) &&
56-
c.stdout.lastActivity.Before(t)
56+
return c.stderr.lastActivity().Before(t) &&
57+
c.stdout.lastActivity().Before(t)
5758
}
5859

5960
func (c *monitoredCmd) combinedOutput() ([]byte, error) {
@@ -67,8 +68,9 @@ func (c *monitoredCmd) combinedOutput() ([]byte, error) {
6768
// activityBuffer is a buffer that keeps track of the last time a Write
6869
// operation was performed on it.
6970
type activityBuffer struct {
70-
buf *bytes.Buffer
71-
lastActivity time.Time
71+
sync.Mutex
72+
buf *bytes.Buffer
73+
lastActivityStamp time.Time
7274
}
7375

7476
func newActivityBuffer() *activityBuffer {
@@ -78,10 +80,18 @@ func newActivityBuffer() *activityBuffer {
7880
}
7981

8082
func (b *activityBuffer) Write(p []byte) (int, error) {
81-
b.lastActivity = time.Now()
83+
b.Lock()
84+
b.lastActivityStamp = time.Now()
85+
defer b.Unlock()
8286
return b.buf.Write(p)
8387
}
8488

89+
func (b *activityBuffer) lastActivity() time.Time {
90+
b.Lock()
91+
defer b.Unlock()
92+
return b.lastActivityStamp
93+
}
94+
8595
type timeoutError struct {
8696
timeout time.Duration
8797
}

0 commit comments

Comments
 (0)