title: How can I verify a deadlock in my async to sync method and HttpClient.PostAsync calls?
body: <p>I'm having a deadlock in my program but I can't figure out why and how to fix it.</p>
  
  <p>Short version:</p>
  
  <p>An <code>async</code> to <code>sync</code> method locks waiting for an async call, which calls another, and so on until a <code>HttpClient.PostAsync</code> call. The http post call never happens, at least it doesn't arrives in the server (which I own too). No errors in the client. My main questions are at the end of this SO question. Thanks!</p>
  
  <p>Longer version:</p>
  
  <p>I know that it is very easy to come up with deadlocks and we must be careful. I'm trying to adhere to the advices of 1. Propagate the async down to all the methods and 2. Use <code>ConfigureAwait(false)</code> whenever possible/make sense. (I think the main advocate of this is Stephen Cleary as his blog and answers in many SO which I have read many of them). </p>
  
  <p>Well, my program...
  I cannot post a full sample code here unfortunately, it's rather long; I describe it as a sync method <code>A</code> that calls an async method <code>B</code>, which calls another async, which calls another async, etc.
  Eventually the last async call is <code>HttpClient.PostAsync</code>. </p>
  
  <p>Program stop's at the <code>await HttpClient.PostAsync</code>. Also, the Http post never fires (I own the target server and in this case it never receives a request). (I double checked the uri etc.).</p>
  
  <p>As a result, the synchronous method locks waiting for <code>B</code>'s async task to complete which never happens. <code>A</code> looks like this:</p>
  
  <pre><code>public Byte[] A()
  {
  var task = BAsync();
  return task.GetAwaiter().GetResult();
  }
  </code></pre>
  
  <p>I use synchronization this way as he method does not need the context back, and I saw the method suggested by Stephen in Nito is implemented using this. (I also tried <code>task.Result</code> instead with same results).</p>
  
  <p>The code of the http post call is:</p>
  
  <pre><code>private static async Task&lt;HttpResponseMessage&gt; PostAsync(Uri serverUri, HttpContent httpContent)
  {
  var client = new HttpClient();
  return await client.PostAsync(serverUri, httpContent).ConfigureAwait(false);
  }
  </code></pre>
  
  <p>The app's UI freezes. It's a WP8 app. In this aspect, the buttons do not react to <code>Click</code> events but <code>ScrollViews</code> still work. This is topic for another question but I prefer to include this detail in the question.</p>
  
  <p>I'm not even sure I have a deadlock, or maybe is a HttpClient bug? (I don't think so, the same method is working in other workflows in the same app). </p>
  
  <p>I tried to verify for deadlock and used VS's threads debugger window but only the main thread seems to be active. I also tried with all other sort of debugger windows but nothing gave me a hint.</p>
  
  <p>My main questions are:</p>
  
  <ol>
  <li>Am I having a deadlock? </li>
  <li>How can I verify, maybe using VS, that I do have a deadlock? </li>
  <li>Is it at <code>Client.PostAsync</code>? If yes why and how to fix it?</li>
  <li>If not, any ideas please.</li>
  </ol>
  
  <hr>
  
  <p>Edit: Method <code>A</code> gets called using reflection with <code>MethodInfo.Invoke</code>. I believe now this is part of the problem.</p>