Skip to content

Commit 87780d7

Browse files
authored
Sync board list to avoid race conditions (#308)
* sync board list to avoid race conditions * properly unlock mutex
1 parent a6fdfea commit 87780d7

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

Diff for: arduino/discovery/discovery.go

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"io"
2424
"os/exec"
2525
"strings"
26+
"sync"
2627
"time"
2728

2829
"github.com/arduino/arduino-cli/arduino/cores"
@@ -33,6 +34,7 @@ import (
3334

3435
// Discovery is an instance of a discovery tool
3536
type Discovery struct {
37+
sync.Mutex
3638
ID string
3739
in io.WriteCloser
3840
out io.ReadCloser
@@ -88,6 +90,11 @@ func (d *Discovery) Start() error {
8890

8991
// List retrieve the port list from this discovery
9092
func (d *Discovery) List() ([]*BoardPort, error) {
93+
// ensure the connection to the discoverer is unique to avoid messing up
94+
// the messages exchanged
95+
d.Lock()
96+
defer d.Unlock()
97+
9198
if _, err := d.in.Write([]byte("LIST\n")); err != nil {
9299
return nil, fmt.Errorf("sending LIST command to discovery: %s", err)
93100
}

0 commit comments

Comments
 (0)