Skip to content

Commit 0a90e3f

Browse files
committed
prevent duplicate messages
1 parent aacbfd5 commit 0a90e3f

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
@@ -1274,19 +1274,10 @@ func (c *Client) monitorProcess(stderr io.ReadCloser) {
12741274
go func() {
12751275
waitErr := proc.Wait()
12761276
<-stderrDone
1277-
stderr := c.getStderrOutput()
12781277
if waitErr != nil {
1279-
if stderr != "" {
1280-
processError = fmt.Errorf("CLI process exited: %w\nstderr: %s", waitErr, stderr)
1281-
} else {
1282-
processError = fmt.Errorf("CLI process exited: %w", waitErr)
1283-
}
1278+
processError = fmt.Errorf("CLI process exited: %w", waitErr)
12841279
} else {
1285-
if stderr != "" {
1286-
processError = fmt.Errorf("CLI process exited unexpectedly\nstderr: %s", stderr)
1287-
} else {
1288-
processError = errors.New("CLI process exited unexpectedly")
1289-
}
1280+
processError = errors.New("CLI process exited unexpectedly")
12901281
}
12911282
close(done)
12921283
}()

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)