Skip to content

Commit 3a71131

Browse files
authored
Make the serial discovery execution atomic (#365)
* make the serial discovery execution atomic * increase timeout to 10s * lower debug level
1 parent d550b66 commit 3a71131

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

Diff for: commands/board/list.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/arduino/arduino-cli/commands"
2828
rpc "github.com/arduino/arduino-cli/rpc/commands"
2929
"github.com/pkg/errors"
30+
"github.com/sirupsen/logrus"
3031
)
3132

3233
var (
@@ -87,11 +88,6 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
8788
return nil, errors.Wrap(err, "unable to instance serial-discovery")
8889
}
8990

90-
if err := serialDiscovery.Start(); err != nil {
91-
return nil, errors.Wrap(err, "unable to start serial-discovery")
92-
}
93-
defer serialDiscovery.Close()
94-
9591
ports, err := serialDiscovery.List()
9692
if err != nil {
9793
return nil, errors.Wrap(err, "error getting port list from serial-discovery")
@@ -102,6 +98,7 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
10298
b := []*rpc.BoardListItem{}
10399

104100
// first query installed cores through the Package Manager
101+
logrus.Debug("Querying installed cores for board identification...")
105102
for _, board := range pm.IdentifyBoard(port.IdentificationPrefs) {
106103
b = append(b, &rpc.BoardListItem{
107104
Name: board.Name(),
@@ -112,12 +109,14 @@ func List(instanceID int32) ([]*rpc.DetectedPort, error) {
112109
// if installed cores didn't recognize the board, try querying
113110
// the builder API
114111
if len(b) == 0 {
112+
logrus.Debug("Querying builder API for board identification...")
115113
url := fmt.Sprintf("https://builder.arduino.cc/v3/boards/byVidPid/%s/%s",
116114
port.IdentificationPrefs.Get("vid"),
117115
port.IdentificationPrefs.Get("pid"))
118116
items, err := apiByVidPid(url)
119117
if err == ErrNotFound {
120118
// the board couldn't be detected, keep going with the next port
119+
logrus.Debug("Board not recognized")
121120
continue
122121
} else if err != nil {
123122
// this is bad, bail out

Diff for: commands/bundled_tools_serial_discovery.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func NewBuiltinSerialDiscovery(pm *packagemanager.PackageManager) (*SerialDiscov
153153
}
154154

155155
// Start starts the specified discovery
156-
func (d *SerialDiscovery) Start() error {
156+
func (d *SerialDiscovery) start() error {
157157
if in, err := d.cmd.StdinPipe(); err == nil {
158158
d.in = in
159159
} else {
@@ -181,8 +181,8 @@ func (d *SerialDiscovery) List() ([]*BoardPort, error) {
181181
d.Lock()
182182
defer d.Unlock()
183183

184-
if d.cmd.Process == nil {
185-
return nil, fmt.Errorf("discovery hasn't started")
184+
if err := d.start(); err != nil {
185+
return nil, fmt.Errorf("discovery hasn't started: %v", err)
186186
}
187187

188188
if _, err := d.in.Write([]byte("LIST\n")); err != nil {
@@ -194,9 +194,9 @@ func (d *SerialDiscovery) List() ([]*BoardPort, error) {
194194
go func() {
195195
select {
196196
case <-done:
197-
case <-time.After(2000 * time.Millisecond):
197+
case <-time.After(10 * time.Second):
198198
timeout = true
199-
d.Close()
199+
d.close()
200200
}
201201
}()
202202
if err := d.outJSON.Decode(&event); err != nil {
@@ -206,11 +206,11 @@ func (d *SerialDiscovery) List() ([]*BoardPort, error) {
206206
return nil, fmt.Errorf("decoding LIST command: %s", err)
207207
}
208208
done <- true
209-
return event.Ports, nil
209+
return event.Ports, d.close()
210210
}
211211

212212
// Close stops the Discovery and free the resources
213-
func (d *SerialDiscovery) Close() error {
213+
func (d *SerialDiscovery) close() error {
214214
_, _ = d.in.Write([]byte("QUIT\n"))
215215
_ = d.in.Close()
216216
_ = d.out.Close()

0 commit comments

Comments
 (0)