@@ -21,7 +21,6 @@ import (
21
21
"io"
22
22
"path/filepath"
23
23
"strings"
24
- "sync"
25
24
"time"
26
25
27
26
"github.com/arduino/arduino-cli/arduino"
@@ -219,17 +218,11 @@ func runProgramAction(pme *packagemanager.Explorer,
219
218
if watch != nil {
220
219
// Run port detector
221
220
uploadCompletedCtx , cancel := context .WithCancel (context .Background ())
222
- var newUploadPort * rpc.Port
223
- var wg sync.WaitGroup
224
- wg .Add (1 )
225
- go func () {
226
- newUploadPort = detectUploadPort (port , watch , uploadCompletedCtx )
227
- wg .Done ()
228
- }()
221
+ newUploadPort := f .NewFuture [* rpc.Port ]()
222
+ go detectUploadPort (port , watch , uploadCompletedCtx , newUploadPort )
229
223
uploadCompleted = func () * rpc.Port {
230
224
cancel ()
231
- wg .Wait ()
232
- return newUploadPort
225
+ return newUploadPort .Await ()
233
226
}
234
227
defer uploadCompleted () // defer in case of exit on error (ensures goroutine completion)
235
228
}
@@ -522,13 +515,15 @@ func runProgramAction(pme *packagemanager.Explorer,
522
515
return uploadCompleted (), nil
523
516
}
524
517
525
- func detectUploadPort (uploadPort * rpc.Port , watch <- chan * rpc.BoardListWatchResponse , uploadCtx context.Context ) * rpc.Port {
518
+ func detectUploadPort (uploadPort * rpc.Port , watch <- chan * rpc.BoardListWatchResponse , uploadCtx context.Context , result f. Future [ * rpc.Port ]) {
526
519
log := logrus .WithField ("task" , "port_detection" )
527
520
log .Tracef ("Detecting new board port after upload" )
528
521
522
+ var candidate * rpc.Port
529
523
defer func () {
530
524
// On exit, discard all events until the watcher is closed
531
525
go f .DiscardCh (watch )
526
+ result .Send (candidate )
532
527
}()
533
528
534
529
// Ignore all events during the upload
@@ -537,7 +532,7 @@ func detectUploadPort(uploadPort *rpc.Port, watch <-chan *rpc.BoardListWatchResp
537
532
case ev , ok := <- watch :
538
533
if ! ok {
539
534
log .Error ("Upload port detection failed, watcher closed" )
540
- return nil
535
+ return
541
536
}
542
537
log .WithField ("event" , ev ).Trace ("Ignored watcher event before upload" )
543
538
continue
@@ -550,13 +545,12 @@ func detectUploadPort(uploadPort *rpc.Port, watch <-chan *rpc.BoardListWatchResp
550
545
// Pick the first port that is detected after the upload
551
546
desiredHwID := uploadPort .HardwareId
552
547
timeout := time .After (5 * time .Second )
553
- var candidate * rpc.Port
554
548
for {
555
549
select {
556
550
case ev , ok := <- watch :
557
551
if ! ok {
558
552
log .Error ("Upload port detection failed, watcher closed" )
559
- return candidate
553
+ return
560
554
}
561
555
if ev .EventType == "remove" && candidate != nil {
562
556
if candidate .Equals (ev .Port .GetPort ()) {
@@ -580,10 +574,10 @@ func detectUploadPort(uploadPort *rpc.Port, watch <-chan *rpc.BoardListWatchResp
580
574
}
581
575
582
576
log .Trace ("Found new upload port!" )
583
- return candidate
577
+ return
584
578
case <- timeout :
585
579
log .Trace ("Timeout waiting for candidate port" )
586
- return candidate
580
+ return
587
581
}
588
582
}
589
583
}
0 commit comments