Skip to content

Commit ea681f4

Browse files
committed
prevent duplicate messages
1 parent 1757e87 commit ea681f4

File tree

2 files changed

+11
-26
lines changed

2 files changed

+11
-26
lines changed

go/client.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,19 +1345,10 @@ func (c *Client) monitorProcess(stderr io.ReadCloser) {
13451345
go func() {
13461346
waitErr := proc.Wait()
13471347
<-stderrDone
1348-
stderr := c.getStderrOutput()
13491348
if waitErr != nil {
1350-
if stderr != "" {
1351-
processError = fmt.Errorf("CLI process exited: %w\nstderr: %s", waitErr, stderr)
1352-
} else {
1353-
processError = fmt.Errorf("CLI process exited: %w", waitErr)
1354-
}
1349+
processError = fmt.Errorf("CLI process exited: %w", waitErr)
13551350
} else {
1356-
if stderr != "" {
1357-
processError = fmt.Errorf("CLI process exited unexpectedly\nstderr: %s", stderr)
1358-
} else {
1359-
processError = errors.New("CLI process exited unexpectedly")
1360-
}
1351+
processError = errors.New("CLI process exited unexpectedly")
13611352
}
13621353
close(done)
13631354
}()

go/internal/jsonrpc2/jsonrpc2.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ type Client struct {
5959
stopChan chan struct{}
6060
wg sync.WaitGroup
6161
processDone chan struct{} // closed when the underlying process exits
62-
processError error // set before processDone is closed
63-
processErrorMu sync.RWMutex // protects processError
62+
processErrorPtr *error // points to error set before processDone is closed
6463
}
6564

6665
// NewClient creates a new JSON-RPC client
@@ -78,22 +77,17 @@ func NewClient(stdin io.WriteCloser, stdout io.ReadCloser) *Client {
7877
// and stores the error that should be returned to pending/future requests.
7978
func (c *Client) SetProcessDone(done chan struct{}, errPtr *error) {
8079
c.processDone = done
81-
// Monitor the channel and copy the error when it closes
82-
go func() {
83-
<-done
84-
if errPtr != nil {
85-
c.processErrorMu.Lock()
86-
c.processError = *errPtr
87-
c.processErrorMu.Unlock()
88-
}
89-
}()
80+
c.processErrorPtr = errPtr
9081
}
9182

92-
// getProcessError returns the process exit error if the process has exited
83+
// getProcessError returns the process exit error if the process has exited.
84+
// Must only be called after <-c.processDone to ensure visibility of the error
85+
// written before close(done) in the monitor goroutine.
9386
func (c *Client) getProcessError() error {
94-
c.processErrorMu.RLock()
95-
defer c.processErrorMu.RUnlock()
96-
return c.processError
87+
if c.processErrorPtr != nil {
88+
return *c.processErrorPtr
89+
}
90+
return nil
9791
}
9892

9993
// Start begins listening for messages in a background goroutine

0 commit comments

Comments
 (0)