-
Notifications
You must be signed in to change notification settings - Fork 330
Fix TryOpenInner InvalidCast race and add unit regression tests (#3314) #4179
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
paulmedynski
wants to merge
4
commits into
main
Choose a base branch
from
dev/automation/test-issue-3314-concurrent-open-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.
+129
−5
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
84a4a78
Add tests confirming InvalidCastException race in TryOpenInner (#3314)
paulmedynski 6d92d49
Fix TryOpenInner race cast and move regression tests to UnitTests
paulmedynski 588706a
Refactor concurrent-open unit tests to use internals directly
paulmedynski b077628
Fix test naming clarity and exception snapshot consistency per Copilo…
paulmedynski 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
113 changes: 113 additions & 0 deletions
113
...ta.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlConnectionConcurrentOpenTests.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,113 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Data; | ||
| using System.Reflection; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Data.ProviderBase; | ||
| using Microsoft.Data.SqlClient.Connection; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.UnitTests.Microsoft.Data.SqlClient | ||
| { | ||
| /// <summary> | ||
| /// Regression tests for GitHub issue #3314. | ||
| /// | ||
| /// Root cause: TryOpenInner() read InnerConnection twice - once for TryOpenConnection() and | ||
| /// again for the cast to SqlConnectionInternal. Between those two reads another thread could | ||
| /// change _innerConnection to DbConnectionClosedConnecting, which is not assignable to | ||
| /// SqlConnectionInternal, causing an opaque InvalidCastException. | ||
| /// | ||
| /// Fix: InnerConnection is now captured into a local variable once; if it is not a | ||
| /// SqlConnectionInternal an InvalidOperationException with a descriptive message is thrown | ||
| /// instead of an InvalidCastException. | ||
| /// </summary> | ||
| public class SqlConnectionConcurrentOpenTests | ||
| { | ||
| private static readonly MethodInfo s_tryOpenInner = typeof(SqlConnection) | ||
| .GetMethod("TryOpenInner", BindingFlags.Instance | BindingFlags.NonPublic)!; | ||
|
|
||
| private static DbConnectionInternal GetConnectingSingleton() | ||
| { | ||
| return DbConnectionClosedConnecting.SingletonInstance; | ||
| } | ||
|
|
||
| private static void ForceInnerConnection(SqlConnection connection, DbConnectionInternal innerConnectionValue) | ||
| { | ||
| connection.SetInnerConnectionTo(innerConnectionValue); | ||
| } | ||
|
|
||
| private static bool InvokeTryOpenInner(SqlConnection connection, TaskCompletionSource<DbConnectionInternal> retry) | ||
| { | ||
| try | ||
| { | ||
| return (bool)s_tryOpenInner.Invoke(connection, [retry])!; | ||
| } | ||
| catch (TargetInvocationException tie) when (tie.InnerException != null) | ||
| { | ||
| throw tie.InnerException; | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InnerConnection_DbConnectionClosedConnecting_IsNotAssignableToSqlConnectionInternal() | ||
| { | ||
| DbConnectionInternal connectingSingleton = GetConnectingSingleton(); | ||
|
|
||
| Assert.False( | ||
| connectingSingleton is SqlConnectionInternal, | ||
| "DbConnectionClosedConnecting must NOT be assignable to SqlConnectionInternal. " + | ||
| "If it were, the race condition in #3314 would not manifest."); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void InnerConnection_InConnectingState_ReportsConnectingState() | ||
| { | ||
| DbConnectionInternal connectingSingleton = GetConnectingSingleton(); | ||
|
|
||
| var connection = new SqlConnection("Data Source=localhost"); | ||
| ForceInnerConnection(connection, connectingSingleton); | ||
|
|
||
| Assert.Equal(ConnectionState.Connecting, connection.State); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Open_WhenAlreadyConnecting_ThrowsInvalidOperation() | ||
| { | ||
| DbConnectionInternal connectingSingleton = GetConnectingSingleton(); | ||
|
|
||
| var connection = new SqlConnection("Data Source=localhost"); | ||
| ForceInnerConnection(connection, connectingSingleton); | ||
|
|
||
| Assert.Throws<InvalidOperationException>(() => connection.Open()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryOpenInner_WhenInnerConnectionRacesToNonSqlConnectionInternalState_ThrowsInvalidOperation_NotInvalidCast() | ||
| { | ||
| DbConnectionInternal initialConnectingState = GetConnectingSingleton(); | ||
| DbConnectionInternal racedNonSqlConnectionInternalState = DbConnectionOpenBusy.SingletonInstance; | ||
|
|
||
| var connection = new SqlConnection("Data Source=localhost"); | ||
| ForceInnerConnection(connection, initialConnectingState); | ||
|
|
||
| TaskCompletionSource<DbConnectionInternal> completedRetry = new(); | ||
| completedRetry.SetResult(racedNonSqlConnectionInternalState); | ||
|
|
||
| Exception ex = Assert.ThrowsAny<Exception>(() => | ||
| { | ||
| InvokeTryOpenInner(connection, completedRetry); | ||
| }); | ||
|
|
||
| Assert.True( | ||
| ex is InvalidOperationException, | ||
| $"Expected InvalidOperationException but got {ex.GetType().Name}: {ex.Message}. " + | ||
| "The fix for #3314 must throw InvalidOperationException (not InvalidCastException) " + | ||
| "when _innerConnection races to a non-SqlConnectionInternal state inside TryOpenInner."); | ||
|
|
||
| Assert.Contains("connection", ex.Message, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.