Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 916
spaetzel Avatar asked May 06 '09 19:05

spaetzel


2 Answers

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.

like image 194
David Avatar answered Sep 22 '22 06:09

David


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

like image 41
Jon Hanna Avatar answered Sep 18 '22 06:09

Jon Hanna