@@ -390,21 +390,17 @@ func runProgramAction(pme *packagemanager.Explorer,
390390 uploadProperties .Set ("build.project_name" , sketchName )
391391 }
392392
393- // By default do not return any new port...
394- uploadCompleted := func () * rpc.Port { return nil }
395- // ...but if there is an expected port change then run the detector...
396- if uploadProperties .GetBoolean ("upload.wait_for_upload_port" ) && watch != nil {
397- uploadCompletedCtx , cancel := context .WithCancel (context .Background ())
398- newUploadPort := f .NewFuture [* rpc.Port ]()
399- go detectUploadPort (port , watch , uploadCompletedCtx , newUploadPort )
400- uploadCompleted = func () * rpc.Port {
401- cancel ()
402- return newUploadPort .Await ()
403- }
393+ // This context is kept alive for the entire duration of the upload
394+ uploadCtx , uploadCompleted := context .WithCancel (context .Background ())
395+ defer uploadCompleted ()
404396
405- // Ensures goroutines completion in case of exit on error
406- defer uploadCompleted ()
397+ // By default do not return any new port but if there is an
398+ // expected port change then run the detector.
399+ updatedUploadPort := f .NewFuture [* rpc.Port ]()
400+ if uploadProperties .GetBoolean ("upload.wait_for_upload_port" ) && watch != nil {
401+ go detectUploadPort (port , watch , uploadCtx , updatedUploadPort )
407402 } else {
403+ updatedUploadPort .Send (nil )
408404 go f .DiscardCh (watch )
409405 }
410406
@@ -520,8 +516,10 @@ func runProgramAction(pme *packagemanager.Explorer,
520516 }
521517 }
522518
519+ uploadCompleted ()
523520 logrus .Tracef ("Upload successful" )
524- return uploadCompleted (), nil
521+
522+ return updatedUploadPort .Await (), nil
525523}
526524
527525func detectUploadPort (uploadPort * rpc.Port , watch <- chan * rpc.BoardListWatchResponse , uploadCtx context.Context , result f.Future [* rpc.Port ]) {
0 commit comments