From 3c2c9c279c421cfba8bbacad779a2e66ee26f63e Mon Sep 17 00:00:00 2001 From: Mirko Boehm Date: Tue, 31 Mar 2026 17:33:04 +0200 Subject: [PATCH] Fix: share bufio.Scanner across ExecuteCommand calls bufio.NewScanner wraps the stdout pipe in an internal 4096-byte read-ahead buffer. Creating a new Scanner on each ExecuteCommand call discards any bytes already buffered from the previous call, causing the next command's BEGIN_MARKER to be lost and the goroutine to block indefinitely waiting for output that was already consumed. Fix by creating the Scanner once in StartShell and reusing it for the lifetime of the shell process. Closes #71 --- pkg/shell/shell.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/shell/shell.go b/pkg/shell/shell.go index 92a450c..4ab797e 100644 --- a/pkg/shell/shell.go +++ b/pkg/shell/shell.go @@ -30,6 +30,7 @@ type Shell struct { cmd *exec.Cmd stdin io.WriteCloser stdout io.ReadCloser + scanner *bufio.Scanner mergeStderr bool } @@ -66,7 +67,7 @@ func StartShell(shell string, mergeStderr bool) (Shell, error) { if err != nil { return Shell{}, fmt.Errorf("Unable to start shell %s: %v", shell, err) } - return Shell{cmd, stdin, stdout, mergeStderr}, nil + return Shell{cmd, stdin, stdout, bufio.NewScanner(stdout), mergeStderr}, nil } // commandResult holds the result of a command execution @@ -117,9 +118,8 @@ func (shell *Shell) ExecuteCommand(ctx context.Context, command string, timeout var stdout []string var rc int beginFound := false - scanner := bufio.NewScanner(shell.stdout) - for scanner.Scan() { - line := scanner.Text() + for shell.scanner.Scan() { + line := shell.scanner.Text() if beginRx.MatchString(line) { beginFound = true continue