-
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -281,7 +281,6 @@ out DbConnectionInternal? recycledConnection | |||||
| } | ||||||
|
|
||||||
| [Fact] | ||||||
| [ActiveIssue("https://github.com/dotnet/SqlClient/issues/3730")] | ||||||
| public async Task GetConnectionMaxPoolSize_ShouldRespectOrderOfRequest() | ||||||
| { | ||||||
| // Arrange | ||||||
|
|
@@ -308,29 +307,31 @@ out DbConnectionInternal? internalConnection | |||||
| Assert.NotNull(internalConnection); | ||||||
| } | ||||||
|
|
||||||
| // Use ManualResetEventSlim to synchronize the tasks | ||||||
| // and force the request queueing order. | ||||||
| using ManualResetEventSlim mresQueueOrder = new(); | ||||||
| using CountdownEvent allRequestsQueued = new(2); | ||||||
| // Use multiple ManualResetEventSlim to ensure proper ordering | ||||||
| using ManualResetEventSlim firstTaskReady = new(false); | ||||||
| using ManualResetEventSlim secondTaskReady = new(false); | ||||||
| using ManualResetEventSlim startRequests = new(false); | ||||||
|
|
||||||
| // Act | ||||||
| var recycledTask = Task.Run(() => | ||||||
| { | ||||||
| mresQueueOrder.Set(); | ||||||
| allRequestsQueued.Signal(); | ||||||
| firstTaskReady.Set(); | ||||||
| startRequests.Wait(); | ||||||
| pool.TryGetConnection( | ||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||||||
| new SqlConnection(""), | ||||||
| new SqlConnection("Timeout=5000"), | ||||||
| null, | ||||||
| new DbConnectionOptions("", null), | ||||||
| out DbConnectionInternal? recycledConnection | ||||||
| ); | ||||||
| return recycledConnection; | ||||||
| }); | ||||||
|
|
||||||
| var failedTask = Task.Run(() => | ||||||
| { | ||||||
| // Force this request to be second in the queue. | ||||||
| mresQueueOrder.Wait(); | ||||||
| allRequestsQueued.Signal(); | ||||||
| secondTaskReady.Set(); | ||||||
| startRequests.Wait(); | ||||||
| // Add a small delay to ensure this request comes after the first | ||||||
| Thread.Sleep(50); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This sleep is a bit fishy, too. (Well ... to me all thread.sleep in tests is fishy). I guess the goal is to make sure the tasks aren't "raced" so much as run one after the other. It probably works, but it seems like a patch. |
||||||
| pool.TryGetConnection( | ||||||
| new SqlConnection("Timeout=1"), | ||||||
| null, | ||||||
|
|
@@ -340,7 +341,20 @@ out DbConnectionInternal? failedConnection | |||||
| return failedConnection; | ||||||
| }); | ||||||
|
|
||||||
| allRequestsQueued.Wait(); | ||||||
| // Wait for both tasks to be ready before starting the requests | ||||||
| firstTaskReady.Wait(); | ||||||
| secondTaskReady.Wait(); | ||||||
|
|
||||||
| // Use SpinWait to ensure both tasks are actually waiting | ||||||
|
||||||
| // Use SpinWait to ensure both tasks are actually waiting | |
| // Wait briefly to ensure both tasks are waiting on startRequests |
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.
Mixing async/await and native synchronization code seems a bit ... smelly to me.
Have we considered using TaskCompletionSource instead so we could use async objects for synchronization?