Repository navigation
fix(bridge): enforce TaskCompletionSource handshake gate to prevent connection race #3110
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
Closed
HarshalPatel1972
wants to merge
5
commits into
StackExchange:main
from
HarshalPatel1972:fix/connection-state-race
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
81c9788
fix(bridge): enforce TaskCompletionSource handshake gate to prevent c…
HarshalPatel1972 0a71a91
Remove unused Xunit.Abstractions using directive
mgravell 8511786
Update ConnectionRaceTests.cs
mgravell 8b9f829
Update ConnectionRaceTests.cs
mgravell b53c407
Fix formatting in ConnectionRaceTests.cs
mgravell 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
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,73 @@ | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace StackExchange.Redis.Tests; | ||
|
|
||
| public class ConnectionRaceTests(ITestOutputHelper output) : TestBase(output) | ||
| { | ||
| [Fact] | ||
| public async Task HandshakeCompletionGatePreventsUnauthenticatedPayloads() | ||
| { | ||
| #if RELEASE | ||
| Assert.Skip("Ignoring in CI, due to threading"); | ||
| #endif | ||
| // Simulate severe thread pool exhaustion using ThreadPool.SetMinThreads(1, 1); | ||
| ThreadPool.GetMinThreads(out int workerThreads, out int completionPortThreads); | ||
| ThreadPool.SetMinThreads(1, 1); | ||
| try | ||
| { | ||
| var options = new ConfigurationOptions | ||
| { | ||
| EndPoints = { { TestConfig.Current.PrimaryServer, TestConfig.Current.PrimaryPort } }, | ||
| AbortOnConnectFail = false, | ||
| AllowAdmin = true, | ||
| }; | ||
|
|
||
| await using var conn = await ConnectionMultiplexer.ConnectAsync(options); | ||
| var db = conn.GetDatabase(); | ||
| var server = conn.GetServer(TestConfig.Current.PrimaryServer, TestConfig.Current.PrimaryPort); | ||
|
|
||
| // Trigger an asynchronous connection reset loop | ||
| var resetLoop = Task.Run(async () => | ||
| { | ||
| for (int i = 0; i < 5; i++) | ||
| { | ||
| server.SimulateConnectionFailure(SimulatedFailureType.All); | ||
| await Task.Delay(10); | ||
| } | ||
| }); | ||
|
|
||
| // Concurrently launch 100 parallel Tasks trying to dispatch rapid PingAsync() operations. | ||
| int taskCount = 100; | ||
| var tasks = new Task[taskCount]; | ||
| for (int i = 0; i < taskCount; i++) | ||
| { | ||
| tasks[i] = Task.Run(async () => | ||
| { | ||
| for (int j = 0; j < 50; j++) | ||
| { | ||
| try | ||
| { | ||
| await db.PingAsync().ConfigureAwait(false); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| // Assert that no commands throw a NOAUTH or protocol corruption exception | ||
| Assert.DoesNotContain("NOAUTH", ex.Message); | ||
| Assert.DoesNotContain("protocol", ex.Message.ToLowerInvariant()); | ||
| } | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| await Task.WhenAll(tasks); | ||
| await resetLoop; | ||
| } | ||
| finally | ||
| { | ||
| ThreadPool.SetMinThreads(workerThreads, completionPortThreads); | ||
| } | ||
| } | ||
| } | ||
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 isn't a great idea in a CI setup; I understand the intent, but... this might be something we simply can't do in this context due to parallelism concerns, plus the impact on the overall runtime