title: Can an Owin Middleware return a response earlier than the Invoke method returns?
body: <p>I have the following middleware code:</p>
  
  <pre><code>public class UoWMiddleware : OwinMiddleware
  {
      readonly IUoW uow;
      public UoWMiddleware(OwinMiddleware next, IUoW uow) : base(next)
      {
          this.uow = uow;
      }
  
      public override async Task Invoke(IOwinContext context)
      {
          try
          {
              await Next.Invoke(context);
          }
          catch
          {
              uow.RollBack();
              throw;
          }
          finally
          {
              if (uow.Status == Base.SharedDomain.UoWStatus.Running)
              {
                  var response = context.Response;
                  if (response.StatusCode &lt; 400)
                  {
                      Thread.Sleep(1000);
                      uow.Commit();
                  }
                  else
                      uow.RollBack();
              }
          }
      }
  }
  </code></pre>
  
  <p>Occasionally we observe that the response returns to client before calling uow.Commit() via fiddler. For example we put a break point to uow.Commit and we see the response is returned to client despite that we are on the breakpoint waiting. This is somewhat unexpected. I would think the response will strictly return after the Invoke method ends. Am I missing something?</p>