"The remote host closed the connection" in Response.OutputStream.Write

24

This code streams large files to our users:

                // Open the file.
            iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
                        System.IO.FileAccess.Read, System.IO.FileShare.Read);


            // Total bytes to read:
            dataToRead = iStream.Length;

            // Read the bytes.
            while (dataToRead > 0)
            {
                // Verify that the client is connected.
                if (Response.IsClientConnected)
                {
                    // Read the data in buffer.
                    length = iStream.Read(buffer, 0, 10000);

                    // Write the data to the current output stream.
                    Response.OutputStream.Write(buffer, 0, length);

                    // Flush the data to the HTML output.
                    Response.Flush();

                    buffer = new Byte[10000];
                    dataToRead = dataToRead - length;
                }
                else
                {
                    //prevent infinite loop if user disconnects
                    dataToRead = -1;
                }
            }

Every once and a while we recieve this exception:

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

Here is the full stack trace:

Stack Trace:
   at System.Web.Hosting.ISAPIWorkerRequestInProcForIIS6.FlushCore(Byte[] status, Byte[] header, Int32 keepConnected, Int32 totalBodySize, Int32 numBodyFragments, IntPtr[] bodyFragments, Int32[] bodyFragmentLengths, Int32 doneWithSession, Int32 finalStatus, Boolean& async)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushCachedResponse(Boolean isFinal)
   at System.Web.Hosting.ISAPIWorkerRequest.FlushResponse(Boolean finalFlush)
   at System.Web.HttpResponse.Flush(Boolean finalFlush)
   at System.Web.HttpResponse.Flush()
   at System.Web.HttpWriter.WriteFromStream(Byte[] data, Int32 offset, Int32 size)
   at System.Web.HttpResponseStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at BIS.DocumentBus.Controls.DocumentViewer.StreamFile(String filepath)

We have never had evidence that users are having trouble downloading our files and plan to simply ignore this exception.

Any idea what the source of this problem is? Is it safe to ignore?

c#
asp.net
streaming
asked on Stack Overflow May 6, 2009 by spaetzel

4 Answers

19

That exception means that the client downloading the file broke the connection before the file had finished downloading. i.e. The client navigated to another page, or just closed the browser.

I might try moving the if (Response.IsClientConnected) line after your iStream.Read. Even if you did that, I think there still might be a chance to receive this error if the connection is broken while the OutputStream.Write method is still working.

answered on Stack Overflow May 6, 2009 by David • edited Jan 30, 2013 by Dor Cohen
12

There are a few different possible causes of this. I can think of three:

One is filling the buffer with close to 2GB of data, but this shouldn't be the case here, since you are flushing regularly.

Another is indeed that described in the answer you previously accepted. It's very hard to reproduce, so I wouldn't assume it was necessarily wrong.

Another possible case, and the one I would bet on, is that the executionTimeout is exceeded, which would cause a ThreadAbortException at first, but this could in turn cause the failure of Flush() which would turn into the exception noted

answered on Stack Overflow Sep 16, 2010 by Jon Hanna
4

Increase executionTimeout in httpRuntime element of web.config.

If user is downloading large file on slow connection, request will eventually timeout.

answered on Stack Overflow Sep 20, 2010 by Tomas Voracek
2

I am posting this answer because it might be help others and save some important time.

In my case Response.Buffer = true in download method (on very first statement) solved the issue.

Thanks

answered on Stack Overflow Nov 3, 2013 by immayankmodi

User contributions licensed under CC BY-SA 3.0