Why does HttpWebRequest.BeginGetResponse () end synchronously?

I am testing an ASP.NET (.NET 4) web application under high load and have found that under certain conditions it HttpWebRequest.BeginGetResponse()terminates synchronously without throwing any exceptions.

After running the following code on several ASP.NET threads under heavy load, I found "WEBREQUEST COMPLETED SYNC!" message in magazines.

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
var result = webRequest.BeginGetResponse(internalCallback, userState);
if (result.CompletedSynchronously)
{
    Trace.Error("WEBREQUEST COMPLETED SYNC!");
}

Note that:

  • If the bandwidth of the thread pool is reached, an InvalidOperationException is thrown.
  • If an error occurs during the connection, a corresponding exception is raised.

In my case there are no exceptions!

I decompiled the System.Net assembly and found that it was indeed possible under some conditions. But I did not understand what these conditions mean ( System.Net.Connection.SubmitRequest(HttpWebRequest request, bool forcedsubmit)):

if (this.m_Free && this.m_WriteDone && !forcedsubmit && (this.m_WriteList.Count == 0 || request.Pipelined && !request.HasEntityBody && (this.m_CanPipeline && this.m_Pipelining) && !this.m_IsPipelinePaused))
{
  this.m_Free = false;
  needReConnect = this.StartRequest(request, true);
  if (needReConnect == TriState.Unspecified)
  {
    flag = true;
    this.PrepareCloseConnectionSocket(ref returnResult);
    this.Close(0);
  }
}

?

+5
2

CompletedSynchronously propperty
, , . , true -, - .

EDIT: - , . request.CachePolicy = new HttpRequestCachePolicy(/*caching type*/);

+4

, , , :

- , .

- - (APM) .

.

(, # : J.Albahari B.Albahari).

+2

All Articles