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

37,992

Solution 1

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.

Solution 2

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

Solution 3

Increase executionTimeout in httpRuntime element of web.config.

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

Solution 4

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

Share:
37,992
spaetzel
Author by

spaetzel

Web developer, designer and enthusiast in Kitchener, Ontario Canada. I use the best of the web, and build my own tools when what I need doesn't exist.

Updated on November 04, 2020

Comments

  • spaetzel
    spaetzel over 3 years

    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?