Skip to content

Commit

Permalink
Fix regression: do not crash on board list if a discovery is not pr…
Browse files Browse the repository at this point in the history
…operly installed (arduino#1814)

* Added tests for arduino#1669

* Fixed regession in discoveries startup

Fix arduino#1669
  • Loading branch information
cmaglie authored Jul 26, 2022
1 parent 436f0bb commit 5332ffd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
17 changes: 10 additions & 7 deletions arduino/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,29 +247,32 @@ func (disc *PluggableDiscovery) runProcess() error {
return err
}
disc.outgoingCommandsPipe = stdin
disc.process = proc

messageChan := make(chan *discoveryMessage)
disc.incomingMessagesChan = messageChan
go disc.jsonDecodeLoop(stdout, messageChan)

if err := disc.process.Start(); err != nil {
if err := proc.Start(); err != nil {
return err
}

disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
disc.process = proc
disc.state = Alive
logrus.Infof("started discovery %s process", disc.id)
return nil
}

func (disc *PluggableDiscovery) killProcess() error {
logrus.Infof("killing discovery %s process", disc.id)
if err := disc.process.Kill(); err != nil {
return err
}
if err := disc.process.Wait(); err != nil {
return err
if disc.process != nil {
if err := disc.process.Kill(); err != nil {
return err
}
if err := disc.process.Wait(); err != nil {
return err
}
}
disc.statusMutex.Lock()
defer disc.statusMutex.Unlock()
Expand Down
19 changes: 19 additions & 0 deletions test/test_board.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# a commercial license, send an email to [email protected].
from pathlib import Path
from git import Repo
import os
import glob
import simplejson as json
import semver
import pytest
Expand Down Expand Up @@ -405,6 +407,23 @@ def test_board_list(run_command):
assert "protocol_label" in port["port"]


def test_board_list_with_invalid_discovery(run_command, data_dir):
run_command(["core", "update-index"])
result = run_command(["board", "list"])
assert result.ok

# check that the CLI do no crash if an invalid discovery is installed
# (for example if the installation fails midway).
# https://github.com/arduino/arduino-cli/issues/1669
tool_dir = os.path.join(data_dir, "packages", "builtin", "tools", "serial-discovery")
for file_to_delete in glob.glob(tool_dir + "/*/*"):
os.remove(file_to_delete)

result = run_command(["board", "list"])
assert result.ok
assert "builtin:serial-discovery" in result.stderr


def test_board_listall(run_command):
assert run_command(["update"])
assert run_command(["core", "install", "arduino:[email protected]"])
Expand Down

0 comments on commit 5332ffd

Please sign in to comment.