-
Notifications
You must be signed in to change notification settings - Fork 316
Fix flaky connection pool tests for FIFO ordering #3751
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -307,16 +307,16 @@ out DbConnectionInternal? internalConnection | |
| Assert.NotNull(internalConnection); | ||
| } | ||
|
|
||
| // Use multiple ManualResetEventSlim to ensure proper ordering | ||
| using ManualResetEventSlim firstTaskReady = new(false); | ||
| using ManualResetEventSlim secondTaskReady = new(false); | ||
| using ManualResetEventSlim startRequests = new(false); | ||
| // Use TaskCompletionSource for coordination to avoid mixing async/await with native synchronization | ||
| TaskCompletionSource<bool> firstTaskReady = new(); | ||
| TaskCompletionSource<bool> secondTaskReady = new(); | ||
| TaskCompletionSource<bool> startRequests = new(); | ||
|
|
||
| // Act | ||
| var recycledTask = Task.Run(() => | ||
| var recycledTask = Task.Run(async () => | ||
| { | ||
| firstTaskReady.Set(); | ||
| startRequests.Wait(); | ||
| firstTaskReady.SetResult(true); | ||
| await startRequests.Task; | ||
| pool.TryGetConnection( | ||
| new SqlConnection("Timeout=5000"), | ||
| null, | ||
|
|
@@ -326,12 +326,14 @@ out DbConnectionInternal? recycledConnection | |
| return recycledConnection; | ||
| }); | ||
|
|
||
| var failedTask = Task.Run(() => | ||
| var failedTask = Task.Run(async () => | ||
| { | ||
| secondTaskReady.Set(); | ||
| startRequests.Wait(); | ||
| // Add a small delay to ensure this request comes after the first | ||
| Thread.Sleep(50); | ||
| secondTaskReady.SetResult(true); | ||
| await startRequests.Task; | ||
| // Add a small delay to ensure this request comes after the first. | ||
| // This is necessary because the channel-based pool queues requests in FIFO order, | ||
| // and we need to guarantee the order for this test to be deterministic. | ||
| await Task.Delay(50); | ||
|
||
| pool.TryGetConnection( | ||
| new SqlConnection("Timeout=1"), | ||
| null, | ||
|
|
@@ -342,16 +344,18 @@ out DbConnectionInternal? failedConnection | |
| }); | ||
|
|
||
| // Wait for both tasks to be ready before starting the requests | ||
| firstTaskReady.Wait(); | ||
| secondTaskReady.Wait(); | ||
| await firstTaskReady.Task; | ||
| await secondTaskReady.Task; | ||
|
|
||
| // Use SpinWait to ensure both tasks are actually waiting | ||
| SpinWait.SpinUntil(() => false, 100); | ||
| // Allow both tasks to reach their wait state before proceeding | ||
| await Task.Delay(100); | ||
|
|
||
| // Start both requests | ||
| startRequests.Set(); | ||
| startRequests.SetResult(true); | ||
|
|
||
| // Give time for both requests to be queued | ||
| // Give time for both requests to be queued. | ||
| // This delay ensures that both TryGetConnection calls have been made and are waiting in the channel | ||
| // before we return the connection, which is necessary to test FIFO ordering. | ||
| await Task.Delay(200); | ||
|
|
||
| // Return the connection which should satisfy the first queued request | ||
|
|
@@ -401,7 +405,9 @@ out DbConnectionInternal? internalConnection | |
| out DbConnectionInternal? recycledConnection | ||
| ); | ||
|
|
||
| // Ensure sufficient time for the recycled connection request to be fully queued | ||
| // Ensure sufficient time for the recycled connection request to be fully queued. | ||
| // This delay is necessary because the channel-based pool queues async requests, | ||
| // and we need to guarantee the first request is in the queue before the second one. | ||
| await Task.Delay(200); | ||
|
|
||
| var exceeded2 = pool.TryGetConnection( | ||
|
|
@@ -411,7 +417,8 @@ out DbConnectionInternal? recycledConnection | |
| out DbConnectionInternal? failedConnection | ||
| ); | ||
|
|
||
| // Ensure the second request is also queued | ||
| // Ensure the second request is also queued before returning the connection. | ||
| // This guarantees that both requests are waiting in FIFO order. | ||
| await Task.Delay(100); | ||
|
|
||
| pool.ReturnInternalConnection(firstConnection!, firstOwningConnection); | ||
|
|
||
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.
I think making these async introduced a deadlock. In some conditions, they'll hang on to threads and prevent future async operations from going through. I'm going to revert these changes other than the SpinWait -> Thread.Sleep()
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.
It'd be nice to get a better understanding of why the task completion source causes deadlocks in this case. I feel like this might be masking a bigger issue, or a lack of understanding of how the mechanisms are actually working.