Skip to content

Commit

Permalink
take 500ms to release leases even when context is canceled
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdfly authored and dangra committed Jan 25, 2023
1 parent c673cab commit a4b895e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/command/deploy/machines.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,14 @@ func (ms *machineSet) AcquireLeases(ctx context.Context, duration time.Duration)
}

func (ms *machineSet) ReleaseLeases(ctx context.Context) error {
// when context is canceled, take 500ms to attempt to release the leases
contextWasAlreadyCanceled := errors.Is(ctx.Err(), context.Canceled)
if contextWasAlreadyCanceled {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.TODO(), 500*time.Millisecond)
defer cancel()
}

results := make(chan error, len(ms.machines))
var wg sync.WaitGroup
for _, m := range ms.machines {
Expand All @@ -291,7 +299,8 @@ func (ms *machineSet) ReleaseLeases(ctx context.Context) error {
}()
hadError := false
for err := range results {
if err != nil {
contextTimedOutOrCanceled := errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled)
if err != nil && (!contextWasAlreadyCanceled || !contextTimedOutOrCanceled) {
hadError = true
terminal.Warnf("failed to release lease: %v\n", err)
}
Expand Down Expand Up @@ -482,7 +491,6 @@ func (md *machineDeployment) DeployMachinesApp(ctx context.Context) (err error)
// FIXME: consolidate all this config stuff into a md.ResolveConfig() or something like that, and deal with restartOnly there

err := md.machineSet.AcquireLeases(ctx, 120*time.Second)
// FIXME: should this use context.Background() or context.TODO(), since we want it to try even on CTRL+C?
defer md.machineSet.ReleaseLeases(ctx)
if err != nil {
return err
Expand Down

0 comments on commit a4b895e

Please sign in to comment.