Skip to content

Instantly share code, notes, and snippets.

@cheenamalhotra
Last active September 19, 2022 18:55
Show Gist options
  • Save cheenamalhotra/94d501e093a0392520fe2095c4bb7491 to your computer and use it in GitHub Desktop.
Save cheenamalhotra/94d501e093a0392520fe2095c4bb7491 to your computer and use it in GitHub Desktop.
Reproduce async deadlock when attention signals cannot be sent due to network failure.
using Microsoft.Data.SqlClient;
namespace AsyncCallbackDeadlockTest
{
internal class Program
{
public static void Main(string[] args)
{
RunTest("Server=tcp:localhost; Integrated Security=true;");
}
public static void RunTest(string connString)
{
try
{
using var cn = new SqlConnection(connString);
cn.Open();
// Repro steps:
// 1. Set "TdsParserStateObject._enforceTimeoutDelay" to true (introduce timeout trigger delays for rare yet possible situations)
// 2. Set breakpoint before "SendAttention() in TdsParserStateObject" writes a packet,
// 3. Terminate the TCP connection using TCPView or any other tool before attention packet is written.
// 4. Notice the apps hangs indefinitely.
QueryTimeout(cn).ConfigureAwait(false).GetAwaiter().GetResult();
}
catch (Exception e)
{
Console.WriteLine(e.Message + e.StackTrace);
}
}
private static async Task QueryTimeout(SqlConnection cn)
{
try
{
var sql = $"WAITFOR DELAY '00:00:02';select 1 as Id;"; // 2 seconds delay followed with select query.
var command = new SqlCommand(sql, cn)
{
CommandTimeout = 1
};
await command.ExecuteReaderAsync().ConfigureAwait(false);
}
catch (Exception e)
{
Console.WriteLine($"Exception occurred: {e.Message}");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment