diff --git a/async_retry.go b/async_retry.go index a20f1eb..d85c177 100644 --- a/async_retry.go +++ b/async_retry.go @@ -94,9 +94,9 @@ func (a *asyncRetry) call(ctx context.Context, f RetryableFunc, config *Config) return retry.Do( func() error { if config.timeout > 0 { - var timeoutCancel context.CancelFunc - ctx, timeoutCancel = context.WithTimeout(ctx, config.timeout) + timeoutCtx, timeoutCancel := context.WithTimeout(ctx, config.timeout) defer timeoutCancel() + return f(timeoutCtx) } return f(ctx) }, diff --git a/async_retry_test.go b/async_retry_test.go index 70ef6c0..7611f1a 100644 --- a/async_retry_test.go +++ b/async_retry_test.go @@ -124,6 +124,13 @@ func Test_asyncRetry_Do(t *testing.T) { name: "Timeout set correctly for each try", args: args{ f: func(ctx context.Context) error { + select { + case <-ctx.Done(): + // Check ctx passed from async-retry is not closed at the start of f() processing. + return fmt.Errorf("context already closed") + default: + } + counter++ select { case <-ctx.Done(): @@ -140,7 +147,7 @@ func Test_asyncRetry_Do(t *testing.T) { }, opts: []Option{ Delay(1 * time.Millisecond), - Timeout(10 * time.Millisecond), + Timeout(1 * time.Second), Attempts(5), }, },