Skip to content

Commit 6e47022

Browse files
committed
Fix Go JSON-RPC client data race
1 parent f9144f1 commit 6e47022

2 files changed

Lines changed: 8 additions & 7 deletions

File tree

go/internal/jsonrpc2/jsonrpc2.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io"
99
"reflect"
1010
"sync"
11+
"sync/atomic"
1112
)
1213

1314
// Error represents a JSON-RPC error response
@@ -54,7 +55,7 @@ type Client struct {
5455
mu sync.Mutex
5556
pendingRequests map[string]chan *Response
5657
requestHandlers map[string]RequestHandler
57-
running bool
58+
running atomic.Bool
5859
stopChan chan struct{}
5960
wg sync.WaitGroup
6061
processDone chan struct{} // closed when the underlying process exits
@@ -97,17 +98,17 @@ func (c *Client) getProcessError() error {
9798

9899
// Start begins listening for messages in a background goroutine
99100
func (c *Client) Start() {
100-
c.running = true
101+
c.running.Store(true)
101102
c.wg.Add(1)
102103
go c.readLoop()
103104
}
104105

105106
// Stop stops the client and cleans up
106107
func (c *Client) Stop() {
107-
if !c.running {
108+
if !c.running.Load() {
108109
return
109110
}
110-
c.running = false
111+
c.running.Store(false)
111112
close(c.stopChan)
112113

113114
// Close stdout to unblock the readLoop
@@ -298,14 +299,14 @@ func (c *Client) readLoop() {
298299

299300
reader := bufio.NewReader(c.stdout)
300301

301-
for c.running {
302+
for c.running.Load() {
302303
// Read Content-Length header
303304
var contentLength int
304305
for {
305306
line, err := reader.ReadString('\n')
306307
if err != nil {
307308
// Only log unexpected errors (not EOF or closed pipe during shutdown)
308-
if err != io.EOF && c.running {
309+
if err != io.EOF && c.running.Load() {
309310
fmt.Printf("Error reading header: %v\n", err)
310311
}
311312
return

go/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ cd "$(dirname "$0")"
4343
echo "=== Running Go SDK E2E Tests ==="
4444
echo
4545

46-
go test -v ./...
46+
go test -v ./... -race
4747

4848
echo
4949
echo "✅ All tests passed!"

0 commit comments

Comments
 (0)