Skip to content

Commit

Permalink
politeiavoter: Fix retry loop bug.
Browse files Browse the repository at this point in the history
This commit fixes a bug that was preventing the failed vote retry loop
from exiting when a SIGINT (ctrl+c) is received. The user was being
forced to use a SIGKILL to kill the politeiavoter process.

The root cause was that the retry loop was using a time.Sleep() call
instead of using the WaitFor() call, which accepts a context and exits
when the context is canceled.
  • Loading branch information
lukebp committed Dec 2, 2021
1 parent df87720 commit 5921145
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion politeiawww/cmd/politeiavoter/politeiavoter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1674,7 +1674,7 @@ func _main() error {
switch action {
case cmdInventory, cmdTally, cmdVote:
// These commands require a connection to a dcrwallet instance. Get
// block height to validate GPRC cerds.
// block height to validate GPRC creds.
ar, err := c.wallet.Accounts(c.ctx, &pb.AccountsRequest{})
if err != nil {
return err
Expand Down
15 changes: 10 additions & 5 deletions politeiawww/cmd/politeiavoter/trickle.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,9 @@ func (p *piv) generateVoteAlarm(token, voteBit string, ctres *pb.CommittedTicket
return va, nil
}

func waitRandom(min, max byte) time.Duration {
// randomDuration returns a randomly selected Duration between the provided
// min and max (in seconds).
func randomDuration(min, max byte) time.Duration {
var (
wait []byte
err error
Expand All @@ -138,9 +140,7 @@ func waitRandom(min, max byte) time.Duration {
}
break
}
d := time.Duration(wait[0]) * time.Second
time.Sleep(d)
return d
return time.Duration(wait[0]) * time.Second
}

func (p *piv) voteTicket(ectx context.Context, bunchID, voteID, of int, va voteAlarm) error {
Expand All @@ -158,8 +158,13 @@ func (p *piv) voteTicket(ectx context.Context, bunchID, voteID, of int, va voteA
var rmsg string
if retry != 0 {
// Wait between 1 and 17 seconds
d := waitRandom(3, 17)
d := randomDuration(3, 17)
rmsg = fmt.Sprintf("retry %v (%v) ", retry, d)
err = WaitFor(ectx, d)
if err != nil {
return fmt.Errorf("%v bunch %v vote %v failed: %v",
time.Now(), bunchID, voteID, err)
}
}

fmt.Printf("%v voting bunch %v vote %v %v%v\n",
Expand Down

0 comments on commit 5921145

Please sign in to comment.