Skip to content

Commit 071098d

Browse files
author
tangyuyi
committed
fix: use bufio.Scanner for stdio transport to avoid panic
Change-Id: Iaaaf44f80d2e49f5275c5f6903c87dcb4dbb53a3
1 parent 1eddde7 commit 071098d

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

client/transport/stdio.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type Stdio struct {
2424

2525
cmd *exec.Cmd
2626
stdin io.WriteCloser
27-
stdout *bufio.Reader
27+
stdout *bufio.Scanner
2828
stderr io.ReadCloser
2929
responses map[string]chan *JSONRPCResponse
3030
mu sync.RWMutex
@@ -39,7 +39,7 @@ type Stdio struct {
3939
func NewIO(input io.Reader, output io.WriteCloser, logging io.ReadCloser) *Stdio {
4040
return &Stdio{
4141
stdin: output,
42-
stdout: bufio.NewReader(input),
42+
stdout: bufio.NewScanner(input),
4343
stderr: logging,
4444

4545
responses: make(map[string]chan *JSONRPCResponse),
@@ -114,7 +114,7 @@ func (c *Stdio) spawnCommand(ctx context.Context) error {
114114
c.cmd = cmd
115115
c.stdin = stdin
116116
c.stderr = stderr
117-
c.stdout = bufio.NewReader(stdout)
117+
c.stdout = bufio.NewScanner(stdout)
118118

119119
if err := cmd.Start(); err != nil {
120120
return fmt.Errorf("failed to start command: %w", err)
@@ -167,14 +167,18 @@ func (c *Stdio) readResponses() {
167167
case <-c.done:
168168
return
169169
default:
170-
line, err := c.stdout.ReadString('\n')
170+
if !c.stdout.Scan() {
171+
return
172+
}
173+
174+
err := c.stdout.Err()
171175
if err != nil {
172-
if err != io.EOF {
173-
fmt.Printf("Error reading response: %v\n", err)
174-
}
176+
fmt.Printf("Error reading response: %v\n", err)
175177
return
176178
}
177179

180+
line := c.stdout.Text()
181+
178182
var baseMessage JSONRPCResponse
179183
if err := json.Unmarshal([]byte(line), &baseMessage); err != nil {
180184
continue

0 commit comments

Comments
 (0)