Skip to content
Merged
Changes from all commits
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
20 changes: 19 additions & 1 deletion internal/extensions/mcp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"os/exec"
"strconv"
"strings"
"sync"
"time"
)

Expand Down Expand Up @@ -297,7 +298,7 @@ func (c *StdioClient) runRPC(ctx context.Context, cfg ServerConfig, requests []r
if err != nil {
return nil, newClientError(ClientErrorTransport, "failed to open mcp stdout pipe", err)
}
var stderr bytes.Buffer
var stderr lockedBuffer
cmd.Stderr = &stderr

if err := cmd.Start(); err != nil {
Expand Down Expand Up @@ -489,6 +490,23 @@ func stopCommand(cmd *exec.Cmd, stdin io.WriteCloser) {
<-done

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The locking change looks correct, but this race fix lands without a regression test. This package already has helper-process coverage around eof_with_stderr; adding a test that exercises concurrent stderr writes while the EOF path reads stderr.String() would make go test -race catch any future reintroduction.

}

type lockedBuffer struct {
mu sync.Mutex
buf bytes.Buffer
}

func (b *lockedBuffer) Write(p []byte) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.Write(p)
}

func (b *lockedBuffer) String() string {
b.mu.Lock()
defer b.mu.Unlock()
return b.buf.String()
}

type rpcRequest struct {
JSONRPC string `json:"jsonrpc"`
ID any `json:"id,omitempty"`
Expand Down
Loading