@@ -17,6 +17,7 @@ package daemon
1717
1818import (
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