From 92b0c4bdc871b42f4d4fc4ab6d763061bf3e071a Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Wed, 24 Jul 2019 15:52:09 +0200 Subject: [PATCH 1/2] sync board list to avoid race conditions --- arduino/discovery/discovery.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go index f97905599c4..26d5e5ab8d4 100644 --- a/arduino/discovery/discovery.go +++ b/arduino/discovery/discovery.go @@ -23,6 +23,7 @@ import ( "io" "os/exec" "strings" + "sync" "time" "github.com/arduino/arduino-cli/arduino/cores" @@ -33,6 +34,7 @@ import ( // Discovery is an instance of a discovery tool type Discovery struct { + sync.Mutex ID string in io.WriteCloser out io.ReadCloser @@ -88,6 +90,7 @@ func (d *Discovery) Start() error { // List retrieve the port list from this discovery func (d *Discovery) List() ([]*BoardPort, error) { + d.Lock() if _, err := d.in.Write([]byte("LIST\n")); err != nil { return nil, fmt.Errorf("sending LIST command to discovery: %s", err) } @@ -109,6 +112,7 @@ func (d *Discovery) List() ([]*BoardPort, error) { return nil, fmt.Errorf("decoding LIST command: %s", err) } done <- true + d.Unlock() return event.Ports, nil } From f26c45399001362534a586fa1c65b4e863e5bb50 Mon Sep 17 00:00:00 2001 From: Massimiliano Pippi Date: Thu, 25 Jul 2019 10:45:31 +0200 Subject: [PATCH 2/2] properly unlock mutex --- arduino/discovery/discovery.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/arduino/discovery/discovery.go b/arduino/discovery/discovery.go index 26d5e5ab8d4..4150a90c426 100644 --- a/arduino/discovery/discovery.go +++ b/arduino/discovery/discovery.go @@ -90,7 +90,11 @@ func (d *Discovery) Start() error { // List retrieve the port list from this discovery func (d *Discovery) List() ([]*BoardPort, error) { + // ensure the connection to the discoverer is unique to avoid messing up + // the messages exchanged d.Lock() + defer d.Unlock() + if _, err := d.in.Write([]byte("LIST\n")); err != nil { return nil, fmt.Errorf("sending LIST command to discovery: %s", err) } @@ -112,7 +116,6 @@ func (d *Discovery) List() ([]*BoardPort, error) { return nil, fmt.Errorf("decoding LIST command: %s", err) } done <- true - d.Unlock() return event.Ports, nil }