Skip to content

Fix regression: do not crash on board list if a discovery is not properly installed #1814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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