Skip to content

Commit afc8626

Browse files
committed
Discoveries processes are now killed if the HELLO command fails
1 parent c7dbdff commit afc8626

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

arduino/discovery/discovery.go

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
2929
"github.com/arduino/go-properties-orderedmap"
3030
"github.com/pkg/errors"
31+
"github.com/sirupsen/logrus"
3132
)
3233

3334
// To work correctly a Pluggable Discovery must respect the state machine specifed on the documentation:
@@ -231,14 +232,39 @@ func (disc *PluggableDiscovery) runProcess() error {
231232
return nil
232233
}
233234

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+
234245
// Run starts the discovery executable process and sends the HELLO command to the discovery to agree on the
235246
// 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 {
238250
return err
239251
}
240252

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 {
242268
return err
243269
}
244270
if msg, err := disc.waitMessage(time.Second * 10); err != nil {

0 commit comments

Comments
 (0)