Debugging a Library's Asynchronous method invocation and its returned value

etl2016

Active member
Joined
Jun 29, 2016
Messages
39
Programming Experience
3-5
hi,

Is there a way to correctly debug an asynchronous interface?

I am invoking an Asynchronous library function StringGetAsync that is offered by StackExchange.Redis. It takes an array of string as input and feeds back an array of RedisValue [] which effectively is byte[] type.

The below program is returning an array of nulls, and to know the reason - when 3 breakpoints are put one on await line, one on Sleep and one on return statement, after the first breakpoint is executed, the program is exiting with 0 as exit status (possibly success), not letting to examine 2nd and 3rd breakpoints. There is an Add-to-Watch setup with an expression arryOfValuesReceived[0].ToString() to observe the returned array's individual elements, but, the program is exiting after first breakpoint.

May I know the possible reasons for this behaviour or any means to capture and look into returned array, to debug further? thank you

C#:
private static async Task<RedisValue[]> GetMeValuesForMyKeys  (DataTable table)
{
var batch = new List<RedisKey> (100);

for (i = 0; i<table.Rows.Count; i++)
{
DataRow row = table.Rows[i];
batch.Add(row["keyColumn"].ToString () );
}

RedisValue[] arryOfValuesReceived = await db.StringGetAsync(batch.ToArray() );
Thread.Sleep(10000); // introduced to identify whether above or below statement is the rootcause, and it is noticed that above is the cause
return arryOfValuesReceived
}
 
That's expected behavior. It' because your first breakpoint is hit right before you do the await on line 11. And what happens is that while the StringGetAsync() waiting for a response, the execution returns to the caller. If the caller terminates, then there is no returning back into the method to wait for the response to comeback.

It maybe worth reviewing how async/await works. There used to be a more succinct diagram, but the explanation used to be lighter as well. The new write up looks to be more detailed:
 
There are a couple of threads here from a user working with Manasys Jazz to make web services access to a COBOL style mainframes. One of the threads asks about how to make an async call synchronous so that he could also debug. The short answer is to not use async/await and synchronously wait for the .Result, but I can understand how this is not really debugging the async call, but rather just forcing it to be synchronous.
 
thank you, to debug - I had put ReadKey() in the calling method and noticed the phenomenon of control flow. This time, the breakpoints moved correctly. The library method StringGetAsync offers its functionality in an asynchronous manner, so, I need to arrange my calling module accordingly, though my requirement is a sequential one. StringGet is the Synchronous counterpart API, but, its not giving the performance, as it handles row by row. For this reason, am going with StringGetAsync, because it can take huge arrays as input in one go and fetch the corresponding values very fast. thank you.
 
You calling module should look something like:
C#:
var values = await GetMeValuesForMyKeys(dataTable);
When the await returns, it should have the values populated.
 
Back
Top Bottom