Skip to content
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

Revert change that made SqlDataReader.ReadAsync() non-blocking #547

Merged
merged 10 commits into from
May 1, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -2847,6 +2847,12 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
ushort status;
int count;

// This is added back since removing it from here introduces regressions in Managed SNI.
// It forces SqlDataReader.ReadAsync() method to run synchronously,
// and will block the calling thread until data is fed from SQL Server.
// TODO Investigate better solution to support non-blocking ReadAsync().
stateObj._syncOverAsync = true;
karinazhou marked this conversation as resolved.
Show resolved Hide resolved

// status
// command
// rowcount (valid only if DONE_COUNT bit is set)
Expand Down Expand Up @@ -2946,11 +2952,10 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
}
}

// _attentionSent set by 'SendAttention'
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._attentionSent && !stateObj.HasPendingData && stateObj.HasOpenResult)
if (!stateObj.HasPendingData && stateObj.HasOpenResult)
{
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3337,11 +3337,10 @@ private bool TryProcessDone(SqlCommand cmd, SqlDataReader reader, ref RunBehavio
}
}

// _attentionSent set by 'SendAttention'
// _pendingData set by e.g. 'TdsExecuteSQLBatch'
// _hasOpenResult always set to true by 'WriteMarsHeader'
//
if (!stateObj._attentionSent && !stateObj._pendingData && stateObj._hasOpenResult)
if (!stateObj._pendingData && stateObj._hasOpenResult)
{
/*
Debug.Assert(!((sqlTransaction != null && _distributedTransaction != null) ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,106 @@ public static void AsyncCancelDoesNotWaitNP()
AsyncCancelDoesNotWait(np_connStr).Wait();
}

[CheckConnStrSetupFact]
public static void TCPAttentionPacketTestTransaction()
{
CancelFollowedByTransaction(tcp_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void NPAttentionPacketTestTransaction()
{
CancelFollowedByTransaction(np_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
public static void TCPAttentionPacketTestAlerts()
{
CancelFollowedByAlert(tcp_connStr);
}

[ConditionalFact(typeof(DataTestUtility), nameof(DataTestUtility.AreConnStringsSetup), nameof(DataTestUtility.IsNotAzureServer))]
[PlatformSpecific(TestPlatforms.Windows)]
public static void NPAttentionPacketTestAlerts()
{
CancelFollowedByAlert(np_connStr);
}

private static void CancelFollowedByTransaction(string constr)
{
using (SqlConnection connection = new SqlConnection(constr))
{
connection.Open();
using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandText = @"SELECT @@VERSION";
using (var r = cmd.ExecuteReader())
{
cmd.Cancel();
}
}
using (SqlTransaction transaction = connection.BeginTransaction())
{ }
}
}

private static void CancelFollowedByAlert(string constr)
{
var alertName = "myAlert" + Guid.NewGuid().ToString();
// Since Alert conditions are randomly generated,
// we will retry on unexpected error messages to avoid collision in pipelines.
var n = new Random().Next(1, 100);
bool retry = true;
int retryAttempt = 0;
while (retry && retryAttempt < 3)
{
try
{
using (var conn = new SqlConnection(constr))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT @@VERSION";
using (var reader = cmd.ExecuteReader())
{
cmd.Cancel(); // Sends Attention
}
}
using (SqlCommand cmd = conn.CreateCommand())
{
cmd.CommandText = $@"EXEC msdb.dbo.sp_add_alert @name=N'{alertName}',
@performance_condition = N'SQLServer:General Statistics|User Connections||>|{n}'";
cmd.ExecuteNonQuery();
cmd.CommandText = @"USE [msdb]";
cmd.ExecuteNonQuery();
cmd.CommandText = $@"/****** Object: Alert [{alertName}] Script Date: {DateTime.Now} ******/
IF EXISTS (SELECT name FROM msdb.dbo.sysalerts WHERE name = N'{alertName}')
EXEC msdb.dbo.sp_delete_alert @name=N'{alertName}'";
cmd.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
if (retryAttempt >= 3 || e.Message.Contains("The transaction operation cannot be performed"))
{
Assert.False(true, $"Retry Attempt: {retryAttempt} | Unexpected Exception occurred: {e.Message}");
}
else
{
retry = true;
retryAttempt++;
Console.WriteLine($"CancelFollowedByAlert Test retry attempt : {retryAttempt}");
Thread.Sleep(500);
continue;
}
}
retry = false;
}
}

private static void MultiThreadedCancel(string constr, bool async)
{
using (SqlConnection con = new SqlConnection(constr))
Expand Down