Skip to content

Commit 3c2c9c2

Browse files
committed
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
1 parent bbbe55f commit 3c2c9c2

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

pkg/shell/shell.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type Shell struct {
3030
cmd *exec.Cmd
3131
stdin io.WriteCloser
3232
stdout io.ReadCloser
33+
scanner *bufio.Scanner
3334
mergeStderr bool
3435
}
3536

@@ -66,7 +67,7 @@ func StartShell(shell string, mergeStderr bool) (Shell, error) {
6667
if err != nil {
6768
return Shell{}, fmt.Errorf("Unable to start shell %s: %v", shell, err)
6869
}
69-
return Shell{cmd, stdin, stdout, mergeStderr}, nil
70+
return Shell{cmd, stdin, stdout, bufio.NewScanner(stdout), mergeStderr}, nil
7071
}
7172

7273
// commandResult holds the result of a command execution
@@ -117,9 +118,8 @@ func (shell *Shell) ExecuteCommand(ctx context.Context, command string, timeout
117118
var stdout []string
118119
var rc int
119120
beginFound := false
120-
scanner := bufio.NewScanner(shell.stdout)
121-
for scanner.Scan() {
122-
line := scanner.Text()
121+
for shell.scanner.Scan() {
122+
line := shell.scanner.Text()
123123
if beginRx.MatchString(line) {
124124
beginFound = true
125125
continue

0 commit comments

Comments
 (0)