The logs of a web application running in IIS include an HttpException with an inner COMException and the message “An error occurred while communicating with the remote host. The error code is 0x80070032.
“. What’s going on and how to fix it?
Before I get into the details, here’s some bad new: this error may have many other causes. The symptoms of the error described in this article are outlined below. Other symptoms may indicate a different cause for the same exceptions. For example, here, https://github.com/SignalR/SignalR/issues/1790 and https://stackoverflow.com/questions/5564862/the-remote-host-closed-the-connection-the-error-code-is-0x800704cd are other causes for this error.
- The error occurs after each app pool recycle or server restart.
- Additionally and optionally: the error occurs periodically (e.g. each 21 minutes).
- The error comes after a request from 127.0.0.1 (additional logging must be added to see this).
- There are no corresponding IIS logs, i.e. there is no request logged at the time the error occurred.
- The application log contains this exception:
An error occurred while communicating with the remote host. The error code is 0x80070032. System.Web.HttpException at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect) at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush() at System.Web.HttpResponse.Flush(Boolean finalFlush, Boolean async) at System.Web.HttpWriter.WriteFromStream(Byte data, Int32 offset, Int32 size) at System.IO.Stream.<>c.
b__46_0(Object ) at System.Threading.Tasks.Task`1.InnerInvoke() at System.Threading.Tasks.Task.Execute() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.IO.Stream.EndWrite(IAsyncResult asyncResult) at Microsoft.Owin.Host.SystemWeb.CallStreams.OutputStream.EndWrite(IAsyncResult asyncResult) at System.Net.Http.StreamToStreamCopy.BufferWrittenCallback(IAsyncResult ar) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Owin.HttpMessageHandlerAdapter. d__33.MoveNext() The request is not supported. (Exception from HRESULT: 0x80070032) System.Runtime.InteropServices.COMException
In my case, the error occurred every 21 minutes…
…which incidentally correlated to the idle time-out configured in the web app’s application pool:
So what’s wrong? Two things are happening here:
- Periodic exceptions in application log: The app pool is configured to time-out after 20 minutes, but also to be always on (see the setting called Start Mode, it’s set to AlwaysRunning). This causes IIS to make a request to the app each time it times out.
- Exceptions when recycling the app pool: The app pool’s Start Mode setting is set to AlwaysRunning which instructs IIS to make a request to the app as soon as it gets restarted.
In both cases IIS does not wait for any response from the app. Instead, the channel is closed and this is in the end what causes the exception.
And these are the possible fixes:
- Periodic exceptions in application log: If the app is supposed to always run, then set the Idle time-out to 0 to deactivate it. Otherwise, set the Start Mode to OnDemand to allow to app to sleep without being woken up.
- Exceptions when recycling the app pool: If it’s ok for the first user request to take a bit longer to wake up the app, just set the Start Mode setting to OnDemand. If the auto-start behavior is required, then it’s a bit more complicated than that. In this case, the application logger must be edited to actively ignore that error.