Skip to content

Commit 10f1c4d

Browse files
committed
Fixed BoardWatch message loop
1 parent 9aa1ac9 commit 10f1c4d

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

Diff for: commands/daemon/daemon.go

+36-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package daemon
1717

1818
import (
1919
"context"
20+
"fmt"
2021
"io"
2122

2223
"github.com/arduino/arduino-cli/arduino/utils"
@@ -28,6 +29,7 @@ import (
2829
"github.com/arduino/arduino-cli/commands/sketch"
2930
"github.com/arduino/arduino-cli/commands/upload"
3031
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32+
"github.com/sirupsen/logrus"
3133
)
3234

3335
// ArduinoCoreServerImpl FIXMEDOC
@@ -73,14 +75,37 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
7375
return err
7476
}
7577

76-
interrupt := make(chan bool)
78+
if msg.Instance == nil {
79+
err = fmt.Errorf("no instance specified")
80+
stream.Send(&rpc.BoardListWatchResponse{
81+
EventType: "error",
82+
Error: err.Error(),
83+
})
84+
return err
85+
}
86+
87+
interrupt := make(chan bool, 1)
7788
go func() {
78-
msg, err := stream.Recv()
79-
if err != nil {
80-
interrupt <- true
81-
}
82-
if msg != nil {
83-
interrupt <- msg.Interrupt
89+
defer close(interrupt)
90+
for {
91+
msg, err := stream.Recv()
92+
// Handle client closing the stream and eventual errors
93+
if err == io.EOF {
94+
logrus.Info("boards watcher stream closed")
95+
interrupt <- true
96+
return
97+
} else if err != nil {
98+
logrus.Infof("interrupting boards watcher: %v", err)
99+
interrupt <- true
100+
return
101+
}
102+
103+
// Message received, does the client want to interrupt?
104+
if msg != nil && msg.Interrupt {
105+
logrus.Info("boards watcher interrupted by client")
106+
interrupt <- msg.Interrupt
107+
return
108+
}
84109
}
85110
}()
86111

@@ -90,7 +115,10 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
90115
}
91116

92117
for event := range eventsChan {
93-
stream.Send(event)
118+
err = stream.Send(event)
119+
if err != nil {
120+
logrus.Infof("sending board watch message: %v", err)
121+
}
94122
}
95123

96124
return nil

0 commit comments

Comments
 (0)