From 2b190eccb73ec5abdd252e2604c59b9ab5997965 Mon Sep 17 00:00:00 2001 From: Shane C Date: Thu, 11 Jul 2024 16:34:08 -0400 Subject: [PATCH] fix goroutine call for commands with no handlers --- linux/run.go | 58 ++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/linux/run.go b/linux/run.go index 960fd37..52b0469 100644 --- a/linux/run.go +++ b/linux/run.go @@ -119,40 +119,44 @@ func (cmd *LinuxCommand) Run() error { } } - cmd.wg.Add(3) + cmd.wg.Add(1) - go func() { - defer cmd.wg.Done() - scanner := bufio.NewScanner(cmd.stderr) - for scanner.Scan() { - line := scanner.Text() - if h, ok := cmd.handlers[EventOutput]; ok { - if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, - CmdOptions: cmd.Options, - }); err != nil { - return + if len(cmd.handlers) != 0 { + cmd.wg.Add(2) + + go func() { + defer cmd.wg.Done() + scanner := bufio.NewScanner(cmd.stderr) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + CmdOptions: cmd.Options, + }); err != nil { + return + } } } - } - }() + }() - go func() { - defer cmd.wg.Done() + go func() { + defer cmd.wg.Done() - scanner := bufio.NewScanner(cmd.stdout) - for scanner.Scan() { - line := scanner.Text() - if h, ok := cmd.handlers[EventOutput]; ok { - if err := h.(func(data EventOutputData) error)(EventOutputData{ - Output: line, - CmdOptions: cmd.Options, - }); err != nil { - return + scanner := bufio.NewScanner(cmd.stdout) + for scanner.Scan() { + line := scanner.Text() + if h, ok := cmd.handlers[EventOutput]; ok { + if err := h.(func(data EventOutputData) error)(EventOutputData{ + Output: line, + CmdOptions: cmd.Options, + }); err != nil { + return + } } } - } - }() + }() + } go func() { defer cmd.wg.Done()