Skip to content

Commit 328d175

Browse files
committed
fix(machine): improve context cancellation handling during watch and sync operations
1 parent 83256b5 commit 328d175

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

‎internal/corrosion/subscribe.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ func (c *APIClient) resubscribeWithBackoffFn(id string) func(context.Context, ui
290290
return nil
291291
}
292292
return func(ctx context.Context, fromChange uint64) (*Subscription, error) {
293+
boff := backoff.WithContext(c.newResubBackoff(), ctx)
293294
return backoff.RetryWithData(func() (*Subscription, error) {
294295
sub, err := c.ResubscribeContext(ctx, id, fromChange)
295296
if err != nil {
@@ -300,11 +301,14 @@ func (c *APIClient) resubscribeWithBackoffFn(id string) func(context.Context, ui
300301
"id", id, "from_change", fromChange)
301302
return nil, backoff.Permanent(fmt.Errorf("resubscribe to %s: %w", id, err))
302303
}
303-
slog.Debug("Failed to resubscribe to Corrosion query. Retrying with backoff.",
304-
"id", id, "from_change", fromChange, "err", err)
304+
// Don't log retries triggered by context cancellation, the backoff will stop immediately.
305+
if ctx.Err() == nil {
306+
slog.Debug("Failed to resubscribe to Corrosion query. Retrying with backoff.",
307+
"id", id, "from_change", fromChange, "err", err)
308+
}
305309
}
306310
return sub, err
307-
}, c.newResubBackoff())
311+
}, boff)
308312
}
309313
}
310314

‎internal/machine/cluster.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ func (cc *clusterController) syncDockerContainers(ctx context.Context) error {
620620
return nil
621621
}
622622
if err := backoff.Retry(watchAndSync, boff); err != nil {
623-
if errors.Is(err, context.Canceled) {
623+
if ctx.Err() != nil {
624624
return nil
625625
}
626626
return fmt.Errorf("watch and sync containers to cluster store: %w", err)

‎internal/machine/docker/controller.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func (c *Controller) WatchAndSyncContainers(ctx context.Context) error {
139139
return fmt.Errorf("sync containers to cluster store: %w", err)
140140
}
141141
case err := <-errCh:
142-
if errors.Is(err, context.Canceled) {
142+
if ctx.Err() != nil {
143143
return nil
144144
}
145145
return fmt.Errorf("receive Docker event: %w", err)

0 commit comments

Comments
 (0)