Skip to content
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

Synchronize access to System.ComponentModel.DataAnnotations.Validator #2428

Merged
merged 6 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/Polly.Core/ResiliencePipelineBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ namespace Polly;
public abstract class ResiliencePipelineBuilderBase
#pragma warning restore S1694 // An abstract class should have both abstract and concrete methods
{
// Workaround for https://github.com/dotnet/runtime/issues/110917
private static readonly object ValidatorLock = new();

private readonly List<Entry> _entries = [];
private bool _used;

Expand Down Expand Up @@ -105,7 +108,14 @@ internal void AddPipelineComponent(Func<StrategyBuilderContext, PipelineComponen
Guard.NotNull(factory);
Guard.NotNull(options);

Validator(new ResilienceValidationContext(options, $"The '{TypeNameFormatter.Format(options.GetType())}' are invalid."));
var validationContext = new ResilienceValidationContext(
options,
$"The '{TypeNameFormatter.Format(options.GetType())}' are invalid.");

lock (ValidatorLock)
{
Validator(validationContext);
martincostello marked this conversation as resolved.
Show resolved Hide resolved
}

if (_used)
{
Expand Down
48 changes: 48 additions & 0 deletions test/Polly.Core.Tests/ResiliencePipelineBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,26 @@ public void AddPipeline_CombinePipelines_Ok()
executions.Should().HaveCount(7);
}

/// <summary>
/// Test for <see href="https://github.com/App-vNext/Polly/issues/2412">validator concurrency issue</see>.
/// </summary>
[Fact]
public void AddStrategy_DoesNotValidateOptionsConcurrently()
{
var detector = new ConcurrencyDetector();

Parallel.For(
0,
2,
index =>
martincostello marked this conversation as resolved.
Show resolved Hide resolved
{
new ResiliencePipelineBuilder()
.AddStrategy(_ => new TestResilienceStrategy(), new ConcurrencyResiliencePipelineOptions(detector));
});

detector.InvokedConcurrently.Should().BeFalse();
}

[Fact]
public void Build_EnsureCorrectContext()
{
Expand Down Expand Up @@ -500,9 +520,37 @@ private class InvalidResiliencePipelineOptions : ResilienceStrategyOptions
public string? RequiredProperty { get; set; }
}

private class ConcurrencyResiliencePipelineOptions(ConcurrencyDetector detector)
: ResilienceStrategyOptions, IValidatableObject
{
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
detector.Execute();
yield break;
}
}

private class InvalidResiliencePipelineBuilder : ResiliencePipelineBuilderBase
{
[Required]
public string? RequiredProperty { get; set; }
}

private class ConcurrencyDetector
{
private int _count;

public bool InvokedConcurrently { get; private set; }

public void Execute()
{
if (Interlocked.Increment(ref _count) > 1)
{
InvokedConcurrently = true;
}

Thread.Sleep(1);
Interlocked.Decrement(ref _count);
}
}
}
Loading