Bug 15666 - HttpWebResponse stream ends prematurely instead of throwing an exception
Summary: HttpWebResponse stream ends prematurely instead of throwing an exception
Status: RESOLVED INVALID
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.10.0.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2013-10-24 15:59 UTC by Oran Dennison
Modified: 2017-08-07 20:37 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED INVALID

Description Oran Dennison 2013-10-24 15:59:42 UTC
When reading from a stream returned by HttpWebResponse.GetResponseStream(), the Read() call is returning 0 when there is an error reading from the stream, causing the caller to incorrectly assume the end of the stream was reached successfully.  The correct behavior is to throw an IOException.

This problem also exists for Xamarin.iOS.  Bug reported here: https://bugzilla.xamarin.com/show_bug.cgi?id=15665

See this forum post for more details, repro steps, and repro code.
http://forums.xamarin.com/discussion/8892/httpwebreponse-incomplete-sometimes
Comment 1 Jonathan Pryor 2013-10-24 17:18:25 UTC
> when there is an error reading from the stream

What error? Do you have a test case which consistently generates this?

WebConnectionStream.Read() doesn't swallow exceptions; the one relevant `catch` block I see rethrows the exception:

https://github.com/mono/mono/blob/master/mcs/class/System/System.Net/WebConnectionStream.cs#L321
https://github.com/mono/mono/blob/master/mcs/class/System/System.Net/WebConnectionStream.cs#L392
https://github.com/mono/mono/blob/master/mcs/class/System/System.Net/WebConnectionStream.cs#L406

What _may_ be happening is that the server is saying that the Content-Length is size X, while the actual data read is Y, Y < X. This would cause you to read fewer bytes than the Stream.Length property implied, and this possibility is mentioned on .NET forums:

http://social.msdn.microsoft.com/Forums/en-US/4deec1d9-f6e2-4f0e-90c4-5e3eb2e28b87/failed-to-read-entire-data-from-httpwebresponsegetresponsestream-?forum=ncl
Comment 2 Oran Dennison 2013-10-24 20:19:01 UTC
Sorry for the overloaded wording.  By "error" I meant a real-world network failure (which isn't being translated into a corresponding exception, hence the bug report).

The server is correctly representing the Content-Length in this case.  During successful test runs the Content-Length exactly matches the number of bytes read from the stream.  Actual bytes < Content-Length only when the network connection breaks while downloading the same exact file that had previously downloaded successfully with actual bytes == Content-Length.

Here's a shorter test case distilled from my longer forum post:

1. Copy my test code and replace the Uri string with something large such as:

(1.5 MB)
http://eoimages.gsfc.nasa.gov/images/imagerecords/74000/74393/world.topo.200407.3x5400x2700.jpg

or (20.7 MB)
http://eoimages.gsfc.nasa.gov/images/imagerecords/74000/74393/world.topo.200407.3x21600x10800.jpg

2. change the fileName line to something like this:

string fileName = "world.jpg";

3.  Run the code and break the network connection mid-download (unplug a network cable, turn off a network adapter, turn off wifi, turn on airplane mode, etc.)

Expected behavior: responseStream.Read() should throw an IOException.

Actual behavior: responseStream.Read() returns 0

I'm guessing that NetworkStream or maybe even Socket is returning 0 or -1 when it should be throwing an IOException instead.
Comment 3 Jon Douglas [MSFT] 2017-08-07 20:37:22 UTC
Thank you for taking the time to submit this report. After reviewing the description of this bug, we believe it no longer affects the current version of Xamarin.Android. If you are still experiencing the issue after updating your packages, please reopen this report with an attached reproduction.