@@ -125,9 +125,9 @@ func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessag
125125 closeAndReportError := func (err error ) {
126126 disc .statusMutex .Lock ()
127127 disc .incomingMessagesError = err
128- disc .statusMutex .Unlock ()
129128 disc .stopSync ()
130129 disc .killProcess ()
130+ disc .statusMutex .Unlock ()
131131 close (outChan )
132132 if err != nil {
133133 disc .logger .Errorf ("Stopped decode loop: %v" , err )
@@ -138,11 +138,7 @@ func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessag
138138
139139 for {
140140 var msg discoveryMessage
141- if err := decoder .Decode (& msg ); errors .Is (err , io .EOF ) {
142- // This is fine :flames: we exit gracefully
143- closeAndReportError (nil )
144- return
145- } else if err != nil {
141+ if err := decoder .Decode (& msg ); err != nil {
146142 closeAndReportError (err )
147143 return
148144 }
@@ -184,7 +180,10 @@ func (disc *Client) waitMessage(timeout time.Duration) (*discoveryMessage, error
184180 select {
185181 case msg := <- disc .incomingMessagesChan :
186182 if msg == nil {
187- return nil , disc .incomingMessagesError
183+ disc .statusMutex .Lock ()
184+ err := disc .incomingMessagesError
185+ disc .statusMutex .Unlock ()
186+ return nil , err
188187 }
189188 return msg , nil
190189 case <- time .After (timeout ):
@@ -239,9 +238,6 @@ func (disc *Client) runProcess() error {
239238}
240239
241240func (disc * Client ) killProcess () {
242- disc .statusMutex .Lock ()
243- defer disc .statusMutex .Unlock ()
244-
245241 disc .logger .Debugf ("Killing discovery process" )
246242 if process := disc .process ; process != nil {
247243 disc .process = nil
@@ -270,7 +266,9 @@ func (disc *Client) Run() (err error) {
270266 if err == nil {
271267 return
272268 }
269+ disc .statusMutex .Lock ()
273270 disc .killProcess ()
271+ disc .statusMutex .Unlock ()
274272 }()
275273
276274 if err = disc .sendCommand ("HELLO 1 \" arduino-cli " + disc .userAgent + "\" \n " ); err != nil {
@@ -287,8 +285,6 @@ func (disc *Client) Run() (err error) {
287285 } else if msg .ProtocolVersion > 1 {
288286 return fmt .Errorf ("protocol version not supported: requested 1, got %d" , msg .ProtocolVersion )
289287 }
290- disc .statusMutex .Lock ()
291- defer disc .statusMutex .Unlock ()
292288 return nil
293289}
294290
@@ -307,8 +303,6 @@ func (disc *Client) Start() error {
307303 } else if strings .ToUpper (msg .Message ) != "OK" {
308304 return fmt .Errorf ("communication out of sync, expected 'OK', received '%s'" , msg .Message )
309305 }
310- disc .statusMutex .Lock ()
311- defer disc .statusMutex .Unlock ()
312306 return nil
313307}
314308
@@ -348,8 +342,10 @@ func (disc *Client) Quit() {
348342 if _ , err := disc .waitMessage (time .Second * 5 ); err != nil {
349343 disc .logger .Errorf ("Quitting discovery: %s" , err )
350344 }
345+ disc .statusMutex .Lock ()
351346 disc .stopSync ()
352347 disc .killProcess ()
348+ disc .statusMutex .Unlock ()
353349}
354350
355351// List executes an enumeration of the ports and returns a list of the available
@@ -377,9 +373,6 @@ func (disc *Client) List() ([]*Port, error) {
377373// The event channel must be consumed as quickly as possible since it may block the
378374// discovery if it becomes full. The channel size is configurable.
379375func (disc * Client ) StartSync (size int ) (<- chan * Event , error ) {
380- disc .statusMutex .Lock ()
381- defer disc .statusMutex .Unlock ()
382-
383376 if err := disc .sendCommand ("START_SYNC\n " ); err != nil {
384377 return nil , err
385378 }
@@ -395,6 +388,8 @@ func (disc *Client) StartSync(size int) (<-chan *Event, error) {
395388 }
396389
397390 // In case there is already an existing event channel in use we close it before creating a new one.
391+ disc .statusMutex .Lock ()
392+ defer disc .statusMutex .Unlock ()
398393 disc .stopSync ()
399394 c := make (chan * Event , size )
400395 disc .eventChan = c
0 commit comments