diff options
| author | Paul Buetow <pbuetow@mimecast.com> | 2020-02-29 17:27:10 +0000 |
|---|---|---|
| committer | Paul Buetow <pbuetow@mimecast.com> | 2020-02-29 17:27:10 +0000 |
| commit | 1d096119505b2eca99ff445644cce94ac0d8b3b8 (patch) | |
| tree | d2c8b4e8f539b4af69bd71540facb11cb2a0924f /internal/io | |
| parent | 7911b102171309dfc43bc2faccac6de9e490f175 (diff) | |
race condition
Diffstat (limited to 'internal/io')
| -rw-r--r-- | internal/io/run/run.go | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/internal/io/run/run.go b/internal/io/run/run.go index 186528d..4d57f9f 100644 --- a/internal/io/run/run.go +++ b/internal/io/run/run.go @@ -30,7 +30,7 @@ func New(command string, args []string) Run { } } -// Start running the command. +// StartBackground starts running the command in background. func (r Run) StartBackground(ctx context.Context, wg *sync.WaitGroup, ec chan<- int, lines chan<- line.Line) (pid int, err error) { pid = -1 @@ -105,22 +105,29 @@ func (r Run) pipeToLines(commandExited chan struct{}, wg *sync.WaitGroup, pid in bufReader := bufio.NewReader(reader) for { + time.Sleep(time.Millisecond * 10) lineStr, err := bufReader.ReadString('\n') - for err == nil { - lines <- line.Line{ - Content: []byte(lineStr), - Count: uint64(pid), - TransmittedPerc: 100, - SourceID: what, + + if err != nil { + select { + case <-commandExited: + return } - lineStr, err = bufReader.ReadString('\n') + continue + } + + newLine := line.Line{ + Content: []byte(lineStr), + Count: uint64(pid), + TransmittedPerc: 100, + SourceID: what, } + select { + case lines <- newLine: case <-commandExited: return - default: } - time.Sleep(time.Millisecond * 10) } } |
