Skip to content

Commit

Permalink
fix(host): defer cancel context in loop block
Browse files Browse the repository at this point in the history
  • Loading branch information
zexi committed Jan 11, 2025
1 parent 87e2ab3 commit 4bdb85c
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions pkg/util/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,11 @@ func (c crictl) RemovePod(ctx context.Context, podId string) error {
interval := 5 * time.Second
errs := []error{}
for tries := 0; tries < maxTries; tries++ {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
err := c.removePod(ctx, podId)
err := func() error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.removePod(ctx, podId)
}()
if err == nil {
return nil
}
Expand Down Expand Up @@ -361,10 +363,11 @@ func (c crictl) stopContainerWithRetry(ctx context.Context, ctrId string, timeou
interval := 5 * time.Second
errs := []error{}
for tries := 0; tries < maxTries; tries++ {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()

err := c.stopContainer(ctx, ctrId, timeout)
err := func() error {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
return c.stopContainer(ctx, ctrId, timeout)
}()
if err == nil {
return nil
}
Expand Down

0 comments on commit 4bdb85c

Please sign in to comment.