@@ -28,6 +28,7 @@ import (
28
28
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
29
29
"github.com/arduino/go-properties-orderedmap"
30
30
"github.com/pkg/errors"
31
+ "github.com/sirupsen/logrus"
31
32
)
32
33
33
34
// To work correctly a Pluggable Discovery must respect the state machine specifed on the documentation:
@@ -231,14 +232,39 @@ func (disc *PluggableDiscovery) runProcess() error {
231
232
return nil
232
233
}
233
234
235
+ func (disc * PluggableDiscovery ) killProcess () error {
236
+ if err := disc .process .Kill (); err != nil {
237
+ return err
238
+ }
239
+ disc .statusMutex .Lock ()
240
+ defer disc .statusMutex .Unlock ()
241
+ disc .state = Dead
242
+ return nil
243
+ }
244
+
234
245
// Run starts the discovery executable process and sends the HELLO command to the discovery to agree on the
235
246
// pluggable discovery protocol. This must be the first command to run in the communication with the discovery.
236
- func (disc * PluggableDiscovery ) Run () error {
237
- if err := disc .runProcess (); err != nil {
247
+ // If the process is started but the HELLO command fails the process is killed.
248
+ func (disc * PluggableDiscovery ) Run () (err error ) {
249
+ if err = disc .runProcess (); err != nil {
238
250
return err
239
251
}
240
252
241
- if err := disc .sendCommand ("HELLO 1 \" arduino-cli " + globals .VersionInfo .VersionString + "\" \n " ); err != nil {
253
+ defer func () {
254
+ // If the discovery process is started successfully but the HELLO handshake
255
+ // fails the discovery is an unusable state, we kill the process to avoid
256
+ // further issues down the line.
257
+ if err == nil {
258
+ return
259
+ }
260
+ if err := disc .killProcess (); err != nil {
261
+ // Log failure to kill the process, ideally that should never happen
262
+ // but it's best to know it if it does
263
+ logrus .Errorf ("Killing discovery %s after unsuccessful start: %s" , disc .id , err )
264
+ }
265
+ }()
266
+
267
+ if err = disc .sendCommand ("HELLO 1 \" arduino-cli " + globals .VersionInfo .VersionString + "\" \n " ); err != nil {
242
268
return err
243
269
}
244
270
if msg , err := disc .waitMessage (time .Second * 10 ); err != nil {
0 commit comments