reconciler: fix restart behavior

Currently RunWithRestart() loop may not exit when execFn returns an
error unrelated to its context cancellation. Add an additional check for
this case.
This commit is contained in:
Kenneth Jenkins 2023-11-15 12:44:07 -08:00
parent 3c2dc5e0a2
commit a4cc89a139

View file

@ -2,14 +2,15 @@ package reconciler
import (
"context"
"errors"
"fmt"
"sync"
)
// RunWithRestart executes execFn.
// The execution would be restarted, by means of canceling the context provided to execFn, each time restartFn quits.
// The execution would be restarted, by means of canceling the context provided to execFn, each time restartFn returns.
// the error returned by restartFn is purely informational and does not affect the execution; may be nil.
// the loop is stopped when the context provided to RunWithRestart is canceled or a genuine error is returned by execFn, not caused by the context.
// the loop is stopped when the context provided to RunWithRestart is canceled or execFn returns an error unrelated to its context cancellation.
func RunWithRestart(
ctx context.Context,
execFn func(context.Context) error,
@ -64,7 +65,7 @@ func restartWithContext(
var err error
for ctx := range contexts {
err = execFn(ctx)
if ctx.Err() == nil {
if ctx.Err() == nil || !errors.Is(err, ctx.Err()) {
return err
}
}