@@ -125,9 +125,9 @@ func (disc *Client) jsonDecodeLoop(in io.Reader, outChan chan<- *discoveryMessag
125
125
closeAndReportError := func (err error ) {
126
126
disc .statusMutex .Lock ()
127
127
disc .incomingMessagesError = err
128
- disc .statusMutex .Unlock ()
129
128
disc .stopSync ()
130
129
disc .killProcess ()
130
+ disc .statusMutex .Unlock ()
131
131
close (outChan )
132
132
if err != nil {
133
133
disc .logger .Errorf ("Stopped decode loop: %v" , err )
@@ -238,9 +238,6 @@ func (disc *Client) runProcess() error {
238
238
}
239
239
240
240
func (disc * Client ) killProcess () {
241
- disc .statusMutex .Lock ()
242
- defer disc .statusMutex .Unlock ()
243
-
244
241
disc .logger .Debugf ("Killing discovery process" )
245
242
if process := disc .process ; process != nil {
246
243
disc .process = nil
@@ -269,7 +266,9 @@ func (disc *Client) Run() (err error) {
269
266
if err == nil {
270
267
return
271
268
}
269
+ disc .statusMutex .Lock ()
272
270
disc .killProcess ()
271
+ disc .statusMutex .Unlock ()
273
272
}()
274
273
275
274
if err = disc .sendCommand ("HELLO 1 \" arduino-cli " + disc .userAgent + "\" \n " ); err != nil {
@@ -343,8 +342,10 @@ func (disc *Client) Quit() {
343
342
if _ , err := disc .waitMessage (time .Second * 5 ); err != nil {
344
343
disc .logger .Errorf ("Quitting discovery: %s" , err )
345
344
}
345
+ disc .statusMutex .Lock ()
346
346
disc .stopSync ()
347
347
disc .killProcess ()
348
+ disc .statusMutex .Unlock ()
348
349
}
349
350
350
351
// List executes an enumeration of the ports and returns a list of the available
@@ -372,9 +373,6 @@ func (disc *Client) List() ([]*Port, error) {
372
373
// The event channel must be consumed as quickly as possible since it may block the
373
374
// discovery if it becomes full. The channel size is configurable.
374
375
func (disc * Client ) StartSync (size int ) (<- chan * Event , error ) {
375
- disc .statusMutex .Lock ()
376
- defer disc .statusMutex .Unlock ()
377
-
378
376
if err := disc .sendCommand ("START_SYNC\n " ); err != nil {
379
377
return nil , err
380
378
}
@@ -390,6 +388,8 @@ func (disc *Client) StartSync(size int) (<-chan *Event, error) {
390
388
}
391
389
392
390
// 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 ()
393
393
disc .stopSync ()
394
394
c := make (chan * Event , size )
395
395
disc .eventChan = c
0 commit comments