Repository navigation
[6.1.9 Cherry-pick] Fix managed SNI MARS receive teardown race #4949
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
Merged
+842
−16
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e6f1ca6
Cherry-pick of #4680 requires manual resolution
github-actions[bot] edabc8f
Fix managed SNI MARS receive teardown race (#4680)
cheenamalhotra 8b19cac
[6.1] Adapt MARS teardown tests to parser namespace refactor
paulmedynski a841355
Fix managed SNI regression prerequisites on Unix (#4949)
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
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
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
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
194 changes: 194 additions & 0 deletions
194
src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/AsyncTest/MarsReceiveTeardownTest.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,194 @@ | ||
| // 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. | ||
|
|
||
| #if NET | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.Collections.Concurrent; | ||
| using System.IO; | ||
| using System.Threading.Tasks; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.Data.SqlClient.ManualTesting.Tests | ||
| { | ||
| /// <summary> | ||
| /// Exercises GH#4679 with a streaming MARS session and a connection-terminating error | ||
| /// on a second session. Requires a test SQL Server and a sysadmin login; each case | ||
| /// writes a severity-20 error to the server log. | ||
| /// </summary> | ||
| [Trait("Set", "1")] | ||
| public sealed class MarsReceiveTeardownTest | ||
| { | ||
| /// <summary> | ||
| /// Physical teardown must report normal command errors rather than faulting an | ||
| /// unobserved MARS receive continuation, with or without connection pooling. | ||
| /// Each delay is a separate case rather than a long-running stress loop. | ||
| /// </summary> | ||
| [ConditionalTheory(typeof(MarsReceiveTeardownTest), nameof(CanRun))] | ||
| [InlineData(false, false, 1)] | ||
| [InlineData(false, true, 1)] | ||
| [InlineData(true, false, 1)] | ||
| [InlineData(true, true, 1)] | ||
| [InlineData(false, false, 29)] | ||
| [InlineData(false, true, 29)] | ||
| [InlineData(true, false, 29)] | ||
| [InlineData(true, true, 29)] | ||
| public async Task FatalError_WhileMarsReaderStreams_DoesNotFaultReceivePump(bool async, bool pooling, int delay) | ||
| { | ||
| string connectionString = new SqlConnectionStringBuilder(DataTestUtility.TCPConnectionString) | ||
| { | ||
| MultipleActiveResultSets = true, | ||
| Pooling = pooling, | ||
| ConnectRetryCount = 0, | ||
| ApplicationName = nameof(MarsReceiveTeardownTest), | ||
| }.ConnectionString; | ||
| ConcurrentQueue<Exception> unobserved = new(); | ||
| EventHandler<UnobservedTaskExceptionEventArgs> handler = (_, args) => | ||
| { | ||
| foreach (Exception exception in args.Exception.Flatten().InnerExceptions) | ||
| { | ||
| if (exception.StackTrace?.Contains("ManagedSni.SniMarsConnection") == true) | ||
| { | ||
| unobserved.Enqueue(exception); | ||
| args.SetObserved(); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| TaskScheduler.UnobservedTaskException += handler; | ||
| try | ||
| { | ||
| await TerminateStreamingConnection(connectionString, async, delay); | ||
|
|
||
| for (int attempt = 0; attempt < 10; attempt++) | ||
| { | ||
| CollectReceiveContinuations(); | ||
| await Task.Delay(50); | ||
| } | ||
|
|
||
| Assert.Empty(unobserved); | ||
| } | ||
| finally | ||
| { | ||
| TaskScheduler.UnobservedTaskException -= handler; | ||
| using SqlConnection poolKey = new(connectionString); | ||
| SqlConnection.ClearPool(poolKey); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Requires managed SNI and a non-Azure SQL Server login allowed to raise fatal errors. | ||
| /// </summary> | ||
| /// <returns>Whether the configured login is a sysadmin on a supported test server.</returns> | ||
| public static bool CanRun() | ||
| { | ||
| if (!DataTestUtility.AreConnStringsSetup() || | ||
| !DataTestUtility.IsNotAzureServer() || !DataTestUtility.IsUsingManagedSNI()) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| using SqlConnection connection = new(DataTestUtility.TCPConnectionString); | ||
| using SqlCommand command = new("SELECT IS_SRVROLEMEMBER('sysadmin')", connection); | ||
| connection.Open(); | ||
| return command.ExecuteScalar() is int member && member == 1; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Starts two MARS sessions, terminates only their connection, and observes all | ||
| /// application tasks so any unobserved exception belongs to the driver's receive pump. | ||
| /// </summary> | ||
| /// <param name="connectionString">Connection string for the test server.</param> | ||
| /// <param name="async">Whether to use asynchronous command and reader APIs.</param> | ||
| /// <param name="delay">Delay before terminating the streaming connection, in milliseconds.</param> | ||
| /// <returns>A task that completes after both sessions and the connection are cleaned up.</returns> | ||
| private static async Task TerminateStreamingConnection(string connectionString, bool async, int delay) | ||
| { | ||
| SqlConnection connection = new(connectionString); | ||
| SqlDataReader? reader = null; | ||
| Task<Exception?>? consumer = null; | ||
| using SqlCommand stream = new( | ||
| "SELECT TOP (200000) a.object_id, b.name, REPLICATE('x', 200) AS pad " + | ||
| "FROM sys.all_objects a CROSS JOIN sys.all_columns b", connection); | ||
| stream.CommandTimeout = 60; | ||
| try | ||
| { | ||
| if (async) | ||
| { | ||
| await connection.OpenAsync(); | ||
| } | ||
| else | ||
| { | ||
| connection.Open(); | ||
| } | ||
|
|
||
| reader = async ? await stream.ExecuteReaderAsync() : stream.ExecuteReader(); | ||
| Assert.True(async ? await reader.ReadAsync() : reader.Read()); | ||
| consumer = Task.Run(() => Record.ExceptionAsync(async () => | ||
| { | ||
| while (async ? await reader.ReadAsync() : reader.Read()) | ||
| { | ||
| } | ||
| })); | ||
| await Task.Delay(delay); | ||
|
|
||
| using SqlCommand terminate = new( | ||
| "RAISERROR('SqlClient MARS receive teardown regression', 20, 1) WITH LOG", connection); | ||
| SqlException error; | ||
| if (async) | ||
| { | ||
| error = await Assert.ThrowsAsync<SqlException>(() => terminate.ExecuteNonQueryAsync()); | ||
| } | ||
| else | ||
| { | ||
| error = Assert.Throws<SqlException>(() => terminate.ExecuteNonQuery()); | ||
| } | ||
| Assert.True(error.Class >= 20, "The second session must terminate the physical connection."); | ||
| } | ||
| finally | ||
| { | ||
| Exception? closeError; | ||
| Exception? readerError; | ||
| try | ||
| { | ||
| Exception? consumeError = consumer is null | ||
| ? null | ||
| : await consumer.WaitAsync(TimeSpan.FromSeconds(65)); | ||
| AssertExpectedTeardownError(consumeError); | ||
| } | ||
| finally | ||
| { | ||
| closeError = Record.Exception(connection.Dispose); | ||
| readerError = reader is null ? null : Record.Exception(reader.Dispose); | ||
| } | ||
| AssertExpectedTeardownError(closeError); | ||
| AssertExpectedTeardownError(readerError); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Allows expected failures of commands on a broken connection, but rejects programming errors. | ||
| /// </summary> | ||
| /// <param name="exception">The observed application-side teardown exception, if any.</param> | ||
| private static void AssertExpectedTeardownError(Exception? exception) | ||
| { | ||
| Assert.True(exception is null or SqlException or InvalidOperationException or IOException, | ||
| $"Unexpected application-side teardown error: {exception}"); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Finalizes abandoned continuation tasks so their failures reach the event handler. | ||
| /// </summary> | ||
| private static void CollectReceiveContinuations() | ||
| { | ||
| GC.Collect(); | ||
| GC.WaitForPendingFinalizers(); | ||
| GC.Collect(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
Oops, something went wrong.
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.