WhiteSites Blog

fixing The remote host closed the connection. The error code is 0x80070057

Posted on Sep 11, 2009 by Paul White

I have a particular error that has been haunting me for weeks now.  Its nothing too serious. For the affected site, it gets maybe 50 errors / day like this over the course of 35K impressions.  But not knowing the cause is pissing me off. 

Here is the full error message

TargetSite:


Void ExecuteInternal(System.Web.IHttpHandler, System.IO.TextWriter, Boolean, Boolean, System.Web.VirtualPath, System.Web.VirtualPath, System.String, System.Exception, System.String)

Base Message:


The remote host closed the connection. The error code is 0x80070057.

Message:


Error executing child request for photo.aspx.

Base:


System.Web.HttpException: The remote host closed the connection. The error code is 0x80070057.
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.Flush()
at System.Web.HttpWriter.Write(String s)
at System.Web.UI.HtmlTextWriter.Write(String s)
at ASP.photo_aspx.__Render__control1(HtmlTextWriter __w, Control parameterContainer)
at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)
at System.Web.UI.Control.RenderChildren(HtmlTextWriter writer)
at System.Web.UI.Page.Render(HtmlTextWriter writer)
at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter)
at System.Web.UI.Control.RenderControl(HtmlTextWriter writer)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Investigating the Cause


After looking at my stats Server it would appear that this error happens on pages that people tend to click through very quickly, such as a photo gallery where you are clicking next picture very quickly.

The guys at Eggheadcafe found the root cause and solution
It has to do with the viewstate not fully loading before moving on to the next page.

Upon inspecting one of the pages on my site that was getting this error I found that the viewstate was taking up more than half of the source code of the page.  Over 20K for the viewstate.

What is ViewState?


ViewState is a way for the server to keep track of the values you use for web controls.  Its useful for submit forms where you need to submit data on postback, but not for things like Label Controls or Place Holders.

Solution


Disable the ViewState on your static web controls like Labels. If the Label's value is not going to change on postback then it should have its viewstate disabled.

<asp:Label ID="mainmenu" runat="server" />
Should look like this
<asp:Label ID="mainmenu" runat="server" EnableViewState="false"/>

Of course this means you need to go through every page in your application and disable the viewstate on every control that requires it.

But if your page doesn't have any controls that need to have viewstate enabled you can just add the attribute to the page directive to disable the viewstate on all controls

So change this
<%@ Page  Language="C#"  %>
to this
<%@ Page  Language="C#" EnableViewState="False" %>

UPDATE 9/14/2009



Since implimenting this on one of my larger sites, the errors have almost completely disappeared.  It seems the error is caused by slow downs in your asp.net application.  The Viewstate causes additional overhead that does not directly slow down your site per say, but it does greatly increase the size of your pages, which in turn negatively affects your visitors with slower internet connetions ( dial up, Low grade DSL, GPRS ). 

However considering the IPs of the users that were having this problem ( some where cable modem ) I thought that there had to be another reason for the slow down.  My theory is that the DNS RBL I use that is run by stop forum spam could be under heavy load, and would therefore slow down the DNS RBL lookups, and in turn slow down the page requests.  Usually DNS lookups happend extremely fast, but considering my website was pinging their RBL on every application request, this could potentially cause some slow downs.  I disabled the code in my global.asax file that was performing the DSL RBL lookup every time Application_BeginRequest was called.  Since doing this the errors have almost completely stopped.  However I still think there is something else causing this, as I have not heard of anyone else having trouble with Stop Forum Spam's RBL.  It seems that if I recycle the App Pool, the errors stop for a period of several hours.

At this point I am not going to claim I have found the direct cause or a solution, but maybe this info might help others who are also dealing with the same situation.

UPDATE 9/21/2009


After emailing with a guy from Microsoft. I discovered that what I found on another forum ( to set Buffering to false on the page level ) was incorrect.  I removed this statement from my code so it could go back to the default ( Buffering=true ).  Since this I haven't received this error once and this was almost a week ago.  I think one of the causes was the RBL slowing things down, and the ViewState on my ASP:Label Controls.  Remember to disable viewstate on anything that will not need to be preserved on postback..

Permalink
119057 Visitors
119057 Views

Categories associated with fixing The remote host closed the connection. The error code is 0x80070057

Discussion

Paul | Sep 12, 2009 4:47 PM
Just a quick update I am still getting this error, even though its not happening as frequently as it was.  I will keep you all posted
Anonymous | Feb 17, 2010 2:55 PM
Thanks for your detailed explanation and thorough investigation, it is much appreciated.
Jeremy | May 4, 2010 10:39 AM
In our case, I can reproduce the problem perfectly by
1. debugging my page
2. stopping execution on a Response.Redirect()
3. Closing the browser that initiated the request
4. Let the execution continue

Under the hood - when .NET calls the ExplicitFlush(), there is no longer a client connection to flush to...so you get the remote host closed the connection error.

The problem is, this is being checked by Response.IsClientConnected (or a lower level version of this) before ExplicitFlush() is called.  So it seems to me that the .Net Framework has a bug. I installed a HotFix for IIS7 that was supposed to address this - but it didn't help.

I've reduced the # of errors be doing a Response.IsClientConnected BEFORE the Response.Redirect() - but even in this case - when dealing with forum spammers and their bots - it's just a race between your code executing and them disconnecting and moving on to the next input box.

Anyways - if anyone can get any further on this - I'd love to hear it.
Mayank Parmar | Sep 14, 2010 2:34 AM
Hi,

I have just web request handler and only .ashx file and no html then also I am facing this issue:
System.Web.HttpException: The remote host closed the connection. The error code is 0x80070057. at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush() at System.Web.HttpResponse.Flush(Boolean finalFlush) at WebSync.UploadHandler.ProcessRequest(HttpContext context) in D:Mayank_WorkspaceProjectsDavid WoodringSource codeWebSyncUpload.ashx.vb:line 84 at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)


Regards,
Allen | Oct 19, 2010 6:50 PM
Hi, I get this error as well, but I'm using a handler, not a web page...thoughts?
Christer | Oct 31, 2010 11:09 PM
Finally, I can reproduce my error and I have a solution.

It?s not a very big thing because it doesn?t show for visitors but is very irritating to see it every day.

Thanks

Bret | Dec 9, 2010 12:07 PM
This is an exception, not an error--there's a difference. It just means that the user closed the page (or their browser) before you finished sending it to them. Nothing mysterious about that.
Geoff | Jun 9, 2012 2:24 PM
This error can also be caused by not closing the stream or the response from a web request.  It also will definitely happen in code that does asynchronous web requests that do not close either the response stream or the web response -- in this case, it will usually happen the 26th time it is called.  Basically, whats happening is this: ASP.Net gives your app pool a connection pool of 25 outbound connections.  Each time you make a call, you use a connection.  When you make asynch calls, ASP.Net has no way of knowing when your're done a given connection, so they don't get returned to the pool until you call close on either the stream or the response.  Once all of the connections are being used, there are no more connections available and you get this (misleading) error message.    It also won't happen until the timeout period elapses, which is strange but nonetheless true. 

If you have code that does close the stream and/or the response and you are still getting this error, then you need to increase the size of  the http connection pool.  I.E., if your request takes 500 ms to complete and you are trying to do more than 50 requests per second then you will run out.  

To increase the size of the connection pool, add the following to your web.config file:
 <system.net>
    <connectionManagement>
      <add address="*" maxconnection="40"/>
    </connectionManagement>
  </system.net>

Or, if you want to reserve connections for a specific address:
 <system.net>
    <connectionManagement>
      <add address="http://MyWebResource/..." maxconnection="40"/>
    </connectionManagement>
  </system.net>

HTH
Suresh | Nov 4, 2014 3:59 AM

I am getting exception when invoking the webservices methods in production enviornment and its not happening consistently. I could not reproduce the issue in dev. enviornment. Can any one can help on it.

 

System.Web.HttpException (0x800704CD): The remote host closed the connection. The error code is 0x800704CD.
   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
   at System.Web.Services.Protocols.SoapExtensionStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
   at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
   at System.Xml.XmlTextEncoder.WriteStringFragment(String str, Int32 offset, Int32 count, Char[] helperBuffer)
   at System.Xml.XmlTextEncoder.Write(String text)
   at System.Xml.XmlTextWriter.WriteString(String text)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlElement.WriteContentTo(XmlWriter w)
   at System.Xml.XmlElement.WriteTo(XmlWriter w)
   at System.Xml.XmlDocument.Save(XmlWriter w)
   at Microsoft.Web.Services2.Messaging.SoapPlainFormatter.Microsoft.Web.Services2.Messaging.ISoapFormatter.Serialize(SoapEnvelope envelope, Stream stream)
   at Microsoft.Web.Services2.WebServicesExtension.AfterSerializeServer(SoapServerMessage message)

 

WSE050: The following exception was encountered: System.Web.HttpException (0x80070057): The remote host closed the connection. The error code is 0x80070057.
   at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
   at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpResponse.End()
   at Microsoft.Web.Services2.WebServicesExtension.BeforeDeserializeServer(SoapServerMessage message).

name
Email Needed to confirm comment, but not made public.
Website
 
 
When you Post your Comment, you'll be sent a confirmation link. Once you click this link your thoughts will be made public.. Posts that are considered spam will be deleted, Please keep your thoughts and links relavent to this Article