-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Fix race condition in FeatureReferences<T>.Fetch #66533
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
Open
eocrawford
wants to merge
2
commits into
dotnet:main
Choose a base branch
from
eocrawford:fix/feature-references-fetch-race
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+160
−8
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
150 changes: 150 additions & 0 deletions
150
src/Extensions/Features/test/FeatureReferencesFetchTests.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,150 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Threading; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.AspNetCore.Http.Features; | ||
|
|
||
| public class FeatureReferencesFetchTests | ||
| { | ||
| private interface ITestFeature | ||
| { | ||
| string Name { get; } | ||
| } | ||
|
|
||
| private class TestFeature : ITestFeature | ||
| { | ||
| public string Name { get; set; } = "default"; | ||
| } | ||
|
|
||
| private struct TestCache | ||
| { | ||
| public ITestFeature? Feature; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Regression test for https://github.com/dotnet/aspnetcore/issues/42040. | ||
| /// Concurrent calls to Fetch while the feature collection revision changes | ||
| /// must not return null. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Fetch_ConcurrentRevisionChange_DoesNotReturnNull() | ||
| { | ||
| const int threadCount = 8; | ||
| const int iterations = 200_000; | ||
|
|
||
| var collection = new FeatureCollection(); | ||
| collection.Set<ITestFeature>(new TestFeature { Name = "initial" }); | ||
|
|
||
| var refs = new FeatureReferences<TestCache>(collection); | ||
| Func<IFeatureCollection, TestFeature> factory = _ => new TestFeature { Name = "created" }; | ||
|
|
||
| var barrier = new Barrier(threadCount); | ||
| var exceptions = new Exception?[threadCount]; | ||
|
|
||
| var threads = new Thread[threadCount]; | ||
| for (var t = 0; t < threadCount; t++) | ||
| { | ||
| var threadIndex = t; | ||
| threads[t] = new Thread(() => | ||
| { | ||
| barrier.SignalAndWait(); | ||
| try | ||
| { | ||
| for (var i = 0; i < iterations; i++) | ||
| { | ||
| // Half the threads bump the revision by setting a feature, | ||
| // the other half read via Fetch. | ||
| if (threadIndex % 2 == 0) | ||
| { | ||
| collection.Set<ITestFeature>(new TestFeature { Name = $"v{i}" }); | ||
| } | ||
| else | ||
| { | ||
| var result = refs.Fetch(ref refs.Cache.Feature, factory); | ||
| Assert.NotNull(result); | ||
| } | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| exceptions[threadIndex] = ex; | ||
| } | ||
| }); | ||
| threads[t].Start(); | ||
| } | ||
|
|
||
| for (var t = 0; t < threadCount; t++) | ||
| { | ||
| threads[t].Join(); | ||
| } | ||
|
|
||
| foreach (var ex in exceptions) | ||
| { | ||
| if (ex is not null) | ||
| { | ||
| throw new AggregateException("Fetch returned null or threw during concurrent access", ex); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Fetch returns the cached value on the fast path (revision unchanged). | ||
| /// </summary> | ||
| [Fact] | ||
| public void Fetch_RevisionUnchanged_ReturnsCachedValue() | ||
| { | ||
| var collection = new FeatureCollection(); | ||
| collection.Set<ITestFeature>(new TestFeature { Name = "original" }); | ||
|
|
||
| var refs = new FeatureReferences<TestCache>(collection); | ||
| Func<IFeatureCollection, TestFeature> factory = _ => new TestFeature { Name = "should not be used" }; | ||
|
|
||
| var first = refs.Fetch(ref refs.Cache.Feature, factory); | ||
| var second = refs.Fetch(ref refs.Cache.Feature, factory); | ||
|
|
||
| Assert.Same(first, second); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Fetch refreshes the cache when the collection revision changes. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Fetch_RevisionChanged_RefreshesCache() | ||
| { | ||
| var collection = new FeatureCollection(); | ||
| collection.Set<ITestFeature>(new TestFeature { Name = "v1" }); | ||
|
|
||
| var refs = new FeatureReferences<TestCache>(collection); | ||
| Func<IFeatureCollection, TestFeature> factory = _ => new TestFeature { Name = "factory" }; | ||
|
|
||
| var first = refs.Fetch(ref refs.Cache.Feature, factory); | ||
| Assert.Equal("v1", first!.Name); | ||
|
|
||
| // Bump revision by setting a new feature value. | ||
| collection.Set<ITestFeature>(new TestFeature { Name = "v2" }); | ||
|
|
||
| var second = refs.Fetch(ref refs.Cache.Feature, factory); | ||
| Assert.NotNull(second); | ||
| Assert.Equal("v2", second!.Name); | ||
| Assert.NotSame(first, second); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Fetch uses the factory when the feature is not in the collection. | ||
| /// </summary> | ||
| [Fact] | ||
| public void Fetch_FeatureNotInCollection_UsesFactory() | ||
| { | ||
| var collection = new FeatureCollection(); | ||
| var refs = new FeatureReferences<TestCache>(collection); | ||
| Func<IFeatureCollection, TestFeature> factory = _ => new TestFeature { Name = "from factory" }; | ||
|
|
||
| var result = refs.Fetch(ref refs.Cache.Feature, factory); | ||
|
|
||
| Assert.NotNull(result); | ||
| Assert.Equal("from factory", result!.Name); | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is addressing a very subtle concurrency/revision-invalidation path, but there’s no unit coverage around
FeatureReferences<TCache>.Fetchbehavior whenRevisionchanges while a cached feature field is already non-null. Consider adding a regression test insrc/Extensions/Features/testthat mutates aFeatureCollectionto bumpRevisionand asserts a subsequentFetchre-reads/refreshes the requested feature (and/or clears the cache) even when the cached ref starts non-null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added in this PR:
Fetch_RevisionChanged_RefreshesCacheinFeatureReferencesFetchTests.cscovers this — sets a feature, calls Fetch, bumps the revision via a new Set, calls Fetch again, and asserts the refreshed value is returned.