Skip to content

Move to bufio reader to support larger tokens #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions internal/supervisor/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
Expand Down Expand Up @@ -54,10 +55,17 @@ func (m *multiOutput) PipeOutput(proc *process) {
pipe := m.openPipe(proc)

go func(proc *process, pipe *ptyPipe) {
scanner := bufio.NewScanner(pipe.pty)

for scanner.Scan() {
m.WriteLine(proc, scanner.Bytes())
reader := bufio.NewReader(pipe.pty)
for {
line, err := reader.ReadBytes('\n')
if err != nil {
// EOF is expected, so don't log it
if err != io.EOF {
log.Printf("reader error: %v", err)
}
break
}
m.WriteLine(proc, line)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the docs, line may be non-empty even in case of an error (e.g. if the last output line is not terminated by \n), so may be worth piping it through regardless:

Suggested change
line, err := reader.ReadBytes('\n')
if err != nil {
// EOF is expected, so don't log it
if err != io.EOF {
log.Printf("reader error: %v", err)
}
break
}
m.WriteLine(proc, line)
line, err := reader.ReadBytes('\n')
if len(line) > 0 {
m.WriteLine(proc, line)
}
if err != nil {
// EOF is expected, so don't log it
if err != io.EOF {
log.Printf("reader error: %v", err)
}
break
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And one more thing, ReadBytes keeps the terminator in the returned line, so WriteLine may need to account for it:

 	buf.WriteString("\033[0m | ")
 
 	buf.Write(p)
-	buf.WriteByte('\n')
+	if p[len(p)-1] != '\n' {
+		buf.WriteByte('\n')
+	}
 
 	m.mutex.Lock()
 	defer m.mutex.Unlock()

}
}(proc, pipe)
}
Expand Down
Loading