Skip to content

Commit cc38790

Browse files
committed
Rationalized the usage of the disc.lock
1 parent 0369df2 commit cc38790

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

Diff for: client.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -238,9 +238,6 @@ func (disc *Client) runProcess() error {
238238
}
239239

240240
func (disc *Client) killProcess() {
241-
disc.statusMutex.Lock()
242-
defer disc.statusMutex.Unlock()
243-
244241
disc.logger.Debugf("Killing discovery process")
245242
if process := disc.process; process != nil {
246243
disc.process = nil
@@ -269,7 +266,9 @@ func (disc *Client) Run() (err error) {
269266
if err == nil {
270267
return
271268
}
269+
disc.statusMutex.Lock()
272270
disc.killProcess()
271+
disc.statusMutex.Unlock()
273272
}()
274273

275274
if err = disc.sendCommand("HELLO 1 \"arduino-cli " + disc.userAgent + "\"\n"); err != nil {
@@ -343,8 +342,10 @@ func (disc *Client) Quit() {
343342
if _, err := disc.waitMessage(time.Second * 5); err != nil {
344343
disc.logger.Errorf("Quitting discovery: %s", err)
345344
}
345+
disc.statusMutex.Lock()
346346
disc.stopSync()
347347
disc.killProcess()
348+
disc.statusMutex.Unlock()
348349
}
349350

350351
// List executes an enumeration of the ports and returns a list of the available
@@ -372,9 +373,6 @@ func (disc *Client) List() ([]*Port, error) {
372373
// The event channel must be consumed as quickly as possible since it may block the
373374
// discovery if it becomes full. The channel size is configurable.
374375
func (disc *Client) StartSync(size int) (<-chan *Event, error) {
375-
disc.statusMutex.Lock()
376-
defer disc.statusMutex.Unlock()
377-
378376
if err := disc.sendCommand("START_SYNC\n"); err != nil {
379377
return nil, err
380378
}
@@ -390,6 +388,8 @@ func (disc *Client) StartSync(size int) (<-chan *Event, error) {
390388
}
391389

392390
// 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()
393393
disc.stopSync()
394394
c := make(chan *Event, size)
395395
disc.eventChan = c

0 commit comments

Comments
 (0)