@@ -17,6 +17,7 @@ package daemon
17
17
18
18
import (
19
19
"context"
20
+ "fmt"
20
21
"io"
21
22
22
23
"github.com/arduino/arduino-cli/arduino/utils"
@@ -28,6 +29,7 @@ import (
28
29
"github.com/arduino/arduino-cli/commands/sketch"
29
30
"github.com/arduino/arduino-cli/commands/upload"
30
31
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
32
+ "github.com/sirupsen/logrus"
31
33
)
32
34
33
35
// ArduinoCoreServerImpl FIXMEDOC
@@ -73,14 +75,37 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
73
75
return err
74
76
}
75
77
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 )
77
88
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
+ }
84
109
}
85
110
}()
86
111
@@ -90,7 +115,10 @@ func (s *ArduinoCoreServerImpl) BoardListWatch(stream rpc.ArduinoCoreService_Boa
90
115
}
91
116
92
117
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
+ }
94
122
}
95
123
96
124
return nil
0 commit comments