title: Async method in controller is called multiple times when an exception is caught
body: <p>I am trying to troubleshoot a problem with exceptions not getting through to my ajax call properly.</p>
  
  <p>My asynchronous controller method is called multiple times, where I only send one ajax request. The response is supposed to be sent, but looking at the network tab in firefox's debug panel, I see no response; the ajax error handler function also has a readyState = 0. This happens only when an exception is thrown inside the async controller method.</p>
  
  <p>There are three/four parts to this problem:  </p>
  
  <ul>
  <li>My javascript</li>
  <li>My async controller method</li>
  <li>The async private controller method called by the latter</li>
  <li>The JsonError and JsonSuccess methods formatting the response</li>
  </ul>
  
  <p>Ajax call:</p>
  
  <pre><code>function test() {
  
      var data = {
          campagneId: 15,
          tiersNum: 2721
      };
  
      console.debug("Starting Ajax !");
  
      $.ajax({
          url: "/CampagnesMailing/SendMail",
          data: data,
          method: "POST",
          success: function (response) {
              console.debug("Sent mail successfully!");
          },
          error: function (xhr, ajaxOpt, thrownError) {
              console.error("Error!");
              console.error(xhr); //xhr.readyState is 0
              console.error(ajaxOpt);
              console.error(thrownError); //this is empty
          },
          complete: function () {
              console.debug("Finished ajax call!");
          }
      });
  }
  $("#goButton").on("click", test);
  </code></pre>
  
  <p>Async controller method called by ajax:</p>
  
  <pre><code>[HttpPost]
  public async Task&lt;ActionResult&gt; SendMail(int campagneId, int tiersNum)
  {
      try
      {
          MailMessage mail = await GetMailFor(campagneId, tiersNum); //I tried adding .ConfigureAwait(false) with no change
          return JsonSuccess();
      }
      catch (Exception e)
      {
          return JsonError(e);
      }
  }
  </code></pre>
  
  <p>Async method GetMailFor:</p>
  
  <pre><code>private async Task&lt;MailMessage&gt; GetMailFor(int campagneId, int tiersNum)
  {
      try
      {
          MailMessage mail = new MailMessage();
  
          mail.To.Add(new MailAddress("")); // This throws an ArgumentException
  
          return mail;
      }
      catch (Exception e)
      {
          throw;
      }
  }
  </code></pre>
  
  <p>JsonError / JsonSuccess:</p>
  
  <pre><code>protected JsonResult JsonSuccess()
  {
      Response.StatusCode = (int) System.Net.HttpStatusCode.OK;
      Response.StatusDescription = "SUCCESS";
      return Json(new { success = true }, JsonRequestBehavior.AllowGet);
  }
  protected JsonResult JsonError(Exception e)
  {
      Response.StatusCode = (int) System.Net.HttpStatusCode.InternalServerError;
      Response.StatusDescription = e.Message;
      //HttpContext.ApplicationInstance.CompleteRequest();
      return Json(new {e.Message}, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>When I put a breakpoint on the first line of SendMail (the one with <code>MailMessage mail = await GetMailFor(campagneId, tiersNum);</code>) and a breakpoint in the catch, I see the method is called 7 times and goes into the catch everytime, mostly with different <code>ManagedThreadId</code> values in <code>System.Threading.Thread.CurrentThread</code>.</p>
  
  <p>Oddly enough, if I replace the <code>mail.To.Add()</code> call by a <code>throw new ArgumentException("Boom");</code>, the process goes well and only one call is caught in my breakpoints.</p>
  
  <p>I had a look at <a href="https://stackoverflow.com/q/7172270/1151654">this issue</a> but adding <code>HttpContext.ApplicationInstance.CompleteRequest();</code> before <code>return Json(new {e.Message}, JsonRequestBehavior.AllowGet);</code> did not change anything.</p>
  
  <p>I only see 1 POST request in Network tab from firefox. This never gets any response.
  Please note these snippets are enough to cause the problem but I use more code in the real application (No need to warn me about async method containing no await).</p>
  
  <p>What is happening? Why is SendMail called 7 times? Why only when <code>new MailAddress()</code> throws and not when I throw manually? How can I debug such a mindblowing behavior?</p>
  
  <p><strong>EDIT:</strong> Removing try/catch from inside the <code>GetMailFor</code> method yields no change.<br>
  <strong>EDIT2:</strong> Removing any mention of <code>async</code>, <code>Task&lt;T&gt;</code> or <code>await</code> also yields no change, so this has nothing to do with an <code>async</code> problem. I'm kind of lost now because I don't know how to debug that...<br>
  <strong>EDIT3:</strong> I never enter the <code>Application_Error</code> function, but I enter <code>Application_BeginRequest</code> everytime before entering <code>SendMail</code> and <code>Application_EndRequest</code> everytime after returning <code>JsonError</code>  </p>