title: "finally" not executed because of Async in "try"?
body: <p>I have a bool (<code>b</code>) which appears only twice in my code (besides its declaration):</p>
  
  <pre><code>try 
  {
      b = true;
      //code including await SomeAsync();
  }
  catch {  }
  finally { b = false; }
  </code></pre>
  
  <p>yet sometimes the try block is <em><strong>started</strong></em> with <code>b</code> being <code>true</code> (-<em>before</em> <code>b = true</code>) which should never be happening because the declaration leaves it <code>false</code>, as does the previous <code>finally</code>. At some point, this code is executed in a loop, which in some cases is iterating quickly and <code>SomeAsync()</code> is trying to use too much memory. I assume that is throwing an exception of a type that <em>can</em> "break" a <code>finally</code>. (<code>b</code> is always <code>false</code> as expected when there's only a normal amount of data for <code>SomeAsync()</code> to process.) </p>
  
  <p>I've tried verifying what Visual Studio shows me with <code>Debug.WriteLine()</code> both after the <code>try</code> and after the <code>finally</code>, and also by appending to a string a different character in those places, but then the <code>finally</code> <em>was</em> executed. So I assume that the slow delay was enough to prevent the exception.</p>
  
  <p>Is this really possible? (Any ideas on how to verify it or fix it so that the finally always runs?)</p>
  
  <p>(finally blocks <em>can</em> fail - cases: <a href="https://stackoverflow.com/questions/111597/conditions-when-finally-does-not-execute-in-a-net-try-finally-block">Conditions when finally does not execute in a .net try..finally block</a> )</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>A good point was raised in a comment and an answer - that this code might be executed several times concurrently during the <code>awaits</code>. Unfortunately, there's another detail (that those answers made me aware is pertinent) - after all iterations - the state is that <code>b</code> is <code>true</code>. <em>That</em> is not explained by concurrency. I have also added an <code>if (b) return;</code>  before the <code>try</code> block (to avoid calling the <code>Async</code> while the previous is running). Still getting left with a <code>true</code> <code>b</code> after all iterations are done.</p>