Search code examples
c#.netado.netasync-awaitglimpse

Glimpse ADO "ExecuteReaderAsync()" does not respond


When use Glimpse ADO ExecuteReaderAsync() did not return any response forever:

var factory = System.Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient");
var conn = factory.CreateConnection();
conn.ConnectionString = "YourConnectionString";
conn.Open();

var cmd = conn.CreateCommand();
cmd.CommandText = "Select 1";                 // Correct SQL is not required. If use SQL "Foo" brings same result.

var result = cmd.ExecuteReaderAsync().Result; // NG. Responce did not return forever.
// var result = cmd.ExecuteReader();          // OK. Responce is return soon.

I Use GlimpseAdo, so factory.CreateConnection() creates an instance of GlimpseDbConnection. Then I execute SQL async via GlimpseDbCommand and wait query result async.

But cmd.ExecuteReaderAsync().Result did not return any response forever.
Wait over 10 minutes, but not happen timeout. I paused debug on VisualStudio, debugging statement is on var result = cmd.ExecuteReaderAsync().Result;.

I think... this probrems caused by async.
Do you know why and where response is gone?

Thanks for your help !

Used:

.Net Framework 4.5.2
Glimpse.Core 1.8.6
Glimpse ADO 1.7.3
SQL Server 2008 R2


Solution

  • This:

    var result = cmd.ExecuteReaderAsync().Result;
    

    Is causing your code to deadlock. I'm assuming you're running an app which passes around a synchronization context (UI, ASP.NET, Universal App or such). This is why you shouldn't block on async code

    To get around this, you'll have to make your method async Task and use await instead of .Result:

    var result = await cmd.ExecuteReaderAsync();
    

    You can also see Trying to call Async method synchronously. It waits on Task.Result forever and deadlock even after using ConfigureAwait(false) in Asp.Net flow for more.