Skip to content

Commit

Permalink
Merge pull request golang#185 from spenczar/fix_activity_buffer_race
Browse files Browse the repository at this point in the history
Fix data race in activityBuffer
  • Loading branch information
sdboyer authored Mar 10, 2017
2 parents e719f18 + 8ad96e3 commit 4b29d30
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"fmt"
"os/exec"
"sync"
"time"
)

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

func (c *monitoredCmd) hasTimedOut() bool {
t := time.Now().Add(-c.timeout)
return c.stderr.lastActivity.Before(t) &&
c.stdout.lastActivity.Before(t)
return c.stderr.lastActivity().Before(t) &&
c.stdout.lastActivity().Before(t)
}

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

func newActivityBuffer() *activityBuffer {
Expand All @@ -78,10 +80,18 @@ func newActivityBuffer() *activityBuffer {
}

func (b *activityBuffer) Write(p []byte) (int, error) {
b.lastActivity = time.Now()
b.Lock()
b.lastActivityStamp = time.Now()
defer b.Unlock()
return b.buf.Write(p)
}

func (b *activityBuffer) lastActivity() time.Time {
b.Lock()
defer b.Unlock()
return b.lastActivityStamp
}

type timeoutError struct {
timeout time.Duration
}
Expand Down

0 comments on commit 4b29d30

Please sign in to comment.