-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Delay pipeline disposal when still in use #1579
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d1fa3b9
Delay pipeline disposal when still in use
martintmk b5aa3c7
cleanup
martintmk 121daaa
Fix code coverage
martintmk 7134e42
PR comments
martintmk e9f69cf
fixes
martintmk 489a995
fixes
martintmk 95b699e
kill mutant
martintmk 984724f
add new test
martintmk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/Polly.Core/Utils/Pipeline/ExecutionTrackingComponent.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| namespace Polly.Utils.Pipeline; | ||
|
|
||
| internal sealed class ExecutionTrackingComponent : PipelineComponent | ||
| { | ||
| public static readonly TimeSpan Timeout = TimeSpan.FromSeconds(30); | ||
|
|
||
| public static readonly TimeSpan SleepDelay = TimeSpan.FromSeconds(1); | ||
|
|
||
| private readonly TimeProvider _timeProvider; | ||
| private int _pendingExecutions; | ||
|
|
||
| public ExecutionTrackingComponent(PipelineComponent component, TimeProvider timeProvider) | ||
| { | ||
| Component = component; | ||
| _timeProvider = timeProvider; | ||
| } | ||
|
|
||
| public PipelineComponent Component { get; } | ||
|
|
||
| internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>( | ||
| Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, | ||
| ResilienceContext context, | ||
| TState state) | ||
| { | ||
| Interlocked.Increment(ref _pendingExecutions); | ||
|
|
||
| try | ||
| { | ||
| return await Component.ExecuteCore(callback, context, state).ConfigureAwait(context.ContinueOnCapturedContext); | ||
| } | ||
| finally | ||
| { | ||
| Interlocked.Decrement(ref _pendingExecutions); | ||
| } | ||
| } | ||
|
|
||
| public override async ValueTask DisposeAsync() | ||
| { | ||
| var start = _timeProvider.GetTimestamp(); | ||
| var stopwatch = Stopwatch.StartNew(); | ||
|
|
||
| // We don't want to introduce locks or any synchronization primitives to main execution path | ||
| // so we will do "dummy" retries until there are no more executions. | ||
| while (Interlocked.CompareExchange(ref _pendingExecutions, 0, 0) > 0) | ||
| { | ||
| await _timeProvider.Delay(SleepDelay).ConfigureAwait(false); | ||
|
|
||
| if (_timeProvider.GetElapsedTime(start) > Timeout) | ||
| { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| await Component.DisposeAsync().ConfigureAwait(false); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
test/Polly.Core.Tests/Utils/Pipeline/ExecutionTrackingComponentTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| using System; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.Time.Testing; | ||
| using Polly.Utils.Pipeline; | ||
|
|
||
| namespace Polly.Core.Tests.Utils.Pipeline; | ||
|
|
||
| public class ExecutionTrackingComponentTests | ||
martincostello marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| private readonly FakeTimeProvider _timeProvider = new(); | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_PendingOperations_Delayed() | ||
| { | ||
| using var assert = new ManualResetEvent(false); | ||
| using var executing = new ManualResetEvent(false); | ||
|
|
||
| await using var inner = new Inner | ||
| { | ||
| OnExecute = () => | ||
| { | ||
| executing.Set(); | ||
| assert.WaitOne(); | ||
| } | ||
| }; | ||
|
|
||
| var component = new ExecutionTrackingComponent(inner, _timeProvider); | ||
| var execution = Task.Run(() => new ResiliencePipeline(component, Polly.Utils.DisposeBehavior.Allow).Execute(() => { })); | ||
| executing.WaitOne(); | ||
|
|
||
| var disposeTask = component.DisposeAsync().AsTask(); | ||
| _timeProvider.Advance(ExecutionTrackingComponent.SleepDelay); | ||
| inner.Disposed.Should().BeFalse(); | ||
| assert.Set(); | ||
|
|
||
| _timeProvider.Advance(ExecutionTrackingComponent.SleepDelay); | ||
| await execution; | ||
|
|
||
| _timeProvider.Advance(ExecutionTrackingComponent.SleepDelay); | ||
| await disposeTask; | ||
|
|
||
| inner.Disposed.Should().BeTrue(); | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_Timeout_Ok() | ||
| { | ||
| using var assert = new ManualResetEvent(false); | ||
| using var executing = new ManualResetEvent(false); | ||
|
|
||
| await using var inner = new Inner | ||
| { | ||
| OnExecute = () => | ||
| { | ||
| executing.Set(); | ||
| assert.WaitOne(); | ||
| } | ||
| }; | ||
|
|
||
| var component = new ExecutionTrackingComponent(inner, _timeProvider); | ||
| var execution = Task.Run(() => new ResiliencePipeline(component, Polly.Utils.DisposeBehavior.Allow).Execute(() => { })); | ||
| executing.WaitOne(); | ||
|
|
||
| var disposeTask = component.DisposeAsync().AsTask(); | ||
| inner.Disposed.Should().BeFalse(); | ||
| _timeProvider.Advance(ExecutionTrackingComponent.Timeout - TimeSpan.FromSeconds(1)); | ||
| inner.Disposed.Should().BeFalse(); | ||
| _timeProvider.Advance(TimeSpan.FromSeconds(1)); | ||
| _timeProvider.Advance(TimeSpan.FromSeconds(1)); | ||
| await disposeTask; | ||
| inner.Disposed.Should().BeTrue(); | ||
|
|
||
| assert.Set(); | ||
| await execution; | ||
| } | ||
|
|
||
| [Fact] | ||
| public async Task DisposeAsync_WhenRunningMultipleTasks_Ok() | ||
| { | ||
| var tasks = new ConcurrentQueue<ManualResetEvent>(); | ||
| await using var inner = new Inner | ||
| { | ||
| OnExecute = () => | ||
| { | ||
| var ev = new ManualResetEvent(false); | ||
| tasks.Enqueue(ev); | ||
| ev.WaitOne(); | ||
| } | ||
| }; | ||
|
|
||
| var component = new ExecutionTrackingComponent(inner, TimeProvider.System); | ||
| var pipeline = new ResiliencePipeline(component, Polly.Utils.DisposeBehavior.Allow); | ||
|
|
||
| for (int i = 0; i < 10; i++) | ||
| { | ||
| _ = Task.Run(() => pipeline.Execute(() => { })); | ||
| } | ||
|
|
||
| while (tasks.Count != 10) | ||
| { | ||
| await Task.Delay(1); | ||
| } | ||
|
|
||
| var disposeTask = component.DisposeAsync().AsTask(); | ||
|
|
||
| while (tasks.Count > 1) | ||
| { | ||
| tasks.TryDequeue(out var ev).Should().BeTrue(); | ||
| ev!.Set(); | ||
| ev.Dispose(); | ||
| disposeTask.Wait(1).Should().BeFalse(); | ||
| inner.Disposed.Should().BeFalse(); | ||
| } | ||
|
|
||
| // last one | ||
| tasks.TryDequeue(out var last).Should().BeTrue(); | ||
| last!.Set(); | ||
| last.Dispose(); | ||
| await disposeTask; | ||
| inner.Disposed.Should().BeTrue(); | ||
| } | ||
|
|
||
| private class Inner : PipelineComponent | ||
| { | ||
| public bool Disposed { get; private set; } | ||
|
|
||
| public override ValueTask DisposeAsync() | ||
| { | ||
| Disposed = true; | ||
| return default; | ||
| } | ||
|
|
||
| public Action OnExecute { get; set; } = () => { }; | ||
|
|
||
| internal override async ValueTask<Outcome<TResult>> ExecuteCore<TResult, TState>(Func<ResilienceContext, TState, ValueTask<Outcome<TResult>>> callback, ResilienceContext context, TState state) | ||
| { | ||
| OnExecute(); | ||
|
|
||
| if (Disposed) | ||
| { | ||
| throw new ObjectDisposedException("dummy"); | ||
| } | ||
|
|
||
| return await callback(context, state); | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.