diff --git a/api/agent.go b/api/agent.go index 6e381e4ba..b7e5e5a10 100644 --- a/api/agent.go +++ b/api/agent.go @@ -255,26 +255,25 @@ func (a *Agent) Monitor(stopCh <-chan struct{}, q *QueryOptions) (chan string, e logCh := make(chan string, 64) go func() { defer resp.Body.Close() - + defer close(logCh) scanner := bufio.NewScanner(resp.Body) + + LOOP: for { select { case <-stopCh: return default: } + if scanner.Scan() { - // An empty string signals to the caller that - // the scan is done, so make sure we only emit - // that when the scanner says it's done, not if - // we happen to ingest an empty line. if text := scanner.Text(); text != "" { logCh <- text } else { logCh <- " " } } else { - logCh <- "" + break LOOP } } }() diff --git a/command/agent_monitor.go b/command/agent_monitor.go index 19e13abcb..9b42562cd 100644 --- a/command/agent_monitor.go +++ b/command/agent_monitor.go @@ -101,8 +101,8 @@ func (c *MonitorCommand) Run(args []string) int { OUTER: for { select { - case log := <-logCh: - if log == "" { + case log, ok := <-logCh: + if !ok { break OUTER } c.Ui.Output(log)