Skip to content

Commit

Permalink
remove cmdDone atomic boolean, just close the cmdResult channel
Browse files Browse the repository at this point in the history
  • Loading branch information
bokwoon95 committed Nov 6, 2023
1 parent ca68542 commit b92c757
Showing 1 changed file with 3 additions and 9 deletions.
12 changes: 3 additions & 9 deletions wgo_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"unicode/utf8"

Expand Down Expand Up @@ -421,7 +420,6 @@ func (wgoCmd *WgoCmd) Run() error {
}

// Step 2: Run the command in the background.
var cmdDone int32
cmdResult := make(chan error, 1)
err = cmd.Start()
if err != nil {
Expand All @@ -430,17 +428,15 @@ func (wgoCmd *WgoCmd) Run() error {
go func() {
wg.Wait()
cmdResult <- cmd.Wait()
atomic.StoreInt32(&cmdDone, 1)
close(cmdResult)
}()

// Step 3: Wait for events in the event loop.
for {
select {
case <-wgoCmd.ctx.Done():
stop(cmd)
if atomic.LoadInt32(&cmdDone) == 0 {
<-cmdResult
}
<-cmdResult
return nil
case err := <-cmdResult:
if i == len(wgoCmd.ArgsList)-1 {
Expand Down Expand Up @@ -474,9 +470,7 @@ func (wgoCmd *WgoCmd) Run() error {
}
case <-timer.C: // Timer expired, reload commands.
stop(cmd)
// I cannot figure out why it's incorrect to wait for the
// cmd to finish here. Everytime I do, the program seems to
// freeze indefinitely.
<-cmdResult
break CMD_CHAIN
}
}
Expand Down

0 comments on commit b92c757

Please sign in to comment.