In async PolicyWrap executions, Context.PolicyKey and Context.PolicyWrapKey may reset to outer values in the PolicyWrap too early
See original GitHub issueSummary:
Policykey Context property is null when its passed to onRetry delegate when using WaitAndRetryAsync + ExecuteAsync.
Perhaps i’m missing something here, although i’m not able to reproduce if I switch to a synchronous policy.
Expected behavior: after setting PoliyKey when creating policy, its then set on the Context object passed to onRetry delegate.
PS C:\Users\hamish.rose\Desktop\test> dotnet run
In on retry. PolicyKey: test, OperationKey: ExecutingMethod
Actual behaviour: PolicyKey is null on Context object passed to onRetry delegate, even though its set on the policy.
PS C:\Users\hamish.rose\Desktop\test> dotnet run
In on retry. PolicyKey: , OperationKey: ExecutingMethod
Steps / Code to reproduce the problem:
using System;
using System.Threading.Tasks;
using Polly;
using Polly.Retry;
namespace test
{
class Program
{
static async Task Main(string[] args)
{
var policy = Policy.Handle<Exception>()
.WaitAndRetryAsync(
1,
(x, y) => TimeSpan.FromSeconds(5),
(ex, timespan, context) => Console.WriteLine($"In on retry. PolicyKey: {context.PolicyKey}, OperationKey: {context.OperationKey}"))
.WithPolicyKey("test") as RetryPolicy;
await policy.ExecuteAsync(async context => await Task.Run(() => throw new Exception()), new Context("ExecutingMethod"));
}
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
v6.1.1 Milestone
In async PolicyWrap executions, Context.PolicyKey and Context.PolicyWrapKey may reset to outer values in the PolicyWrap too early bug ready.
Read more >Polly - Please use asynchronous-defined policies when ...
You are combining synchronous and asynchronous execution here, hence the error message. This is because of the last line of code:
Read more >Using Execution Context in Polly
RetryAsync (1, onRetryAsync: async (e, i) => await AuthHelper.RefreshAuthorization(httpClient)); var httpResponseMessage = await ...
Read more >Polly 7.2.4
Async continuations and retries by default do not run on a captured synchronization context. To change this, use . ExecuteAsync(...) overloads taking a...
Read more >Authoring a proactive Polly policy (Custom policies Part II)
In this article we'll build our first custom Polly policy: a proactive Policy to capture execution timings. Polly polices fall into two ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fix merged to master. Will be live as soon as v6.1.1 is released to nuget
@hamish-rose Thanks for the confirmation!