Bug 19673 - Error getting response stream (ReadDone2): ReceiveFailure
Summary: Error getting response stream (ReadDone2): ReceiveFailure
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 4.12.4
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Marek Habersack
URL:
Depends on:
Blocks:
 
Reported: 2014-05-12 02:35 UTC by info
Modified: 2016-04-18 19:14 UTC (History)
13 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 FIXED

Description info 2014-05-12 02:35:55 UTC
Starting with Xamarin.Android v4.12 we are getting the exception "Error getting response stream (ReadDone2): ReceiveFailure" when connecting to some SSL protected servers. The same code did work on older Versions of Xamarin.Android and also works with the latest versions of Xamarin.iOS and Xamarin.Mac. I created the following minimal sample to demonstrate the problem. Just paste the method in a default app and call it with VALID PayPal credentials. The error will not show up when called with invalid PayPal credentials:

void PayPalTest(string sUserEMail, string sPassword)
{
    CookieContainer vCookieContainer = new CookieContainer();

    // GET login page, filling in various cookies.

    HttpWebRequest aRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/de/cgi-bin/webscr?cmd=_login-run");
    aRequest.CookieContainer = vCookieContainer;
    HttpWebResponse aResponse = (HttpWebResponse)aRequest.GetResponse();
    aResponse.Close();

    // POST login. User e-mail and password are assumed to not need any HTML encoding.

    aRequest = (HttpWebRequest)WebRequest.Create("https://www.paypal.com/de/cgi-bin/webscr?cmd=_login-submit");
    aRequest.CookieContainer = vCookieContainer;
    aRequest.Method = "POST";

    string sPostData = "login_email=" + sUserEMail + "&login_password=" + sPassword;
    byte[] vbData = Encoding.UTF8.GetBytes(sPostData);
    using (Stream aStream = aRequest.GetRequestStream())
    {
        aStream.Write(vbData, 0, vbData.Length);
        aStream.Close();
    }

    try
    {
        // This is where the
        // "Error getting response stream (ReadDone2): ReceiveFailure"
        // exception occurs
        aResponse = (HttpWebResponse)aRequest.GetResponse();
    }
    catch (Exception x)
    {
        Toast.MakeText(this, x.Message, ToastLength.Long).Show();
    }

    aResponse.Close();
}
Comment 2 stkaratz 2015-02-27 06:02:00 UTC
Is there any update to this? I'm getting the exact same on Xamarin.Android 4.20.0.28
Comment 3 frederik.smolders 2015-03-03 08:41:23 UTC
I'm using Restsharp in an Xamarin iOS + Android project, and I'm experiencing the same error in both iOS and Android versions. And that's without using SSL...
Comment 4 wkoncki 2015-07-10 07:10:56 UTC
For all those poor people fighting with this error as I did, here comes the solution. First of all you are all bad programmers as I am and you made a mistake. URL for posting data should always end with "/". This makes link posted in this report example not 
https://www.paypal.com/de/cgi-bin/webscr?cmd=_login-run
but
https://www.paypal.com/de/cgi-bin/webscr/?cmd=_login-run
You see that / near webscr? This is the reason why it all failed. You are welcome.

Now few words why it fails. After you call wrong URL, server returns 301 status code response. That is "permanently moved" redirect response. POST in httpclient does not expect this type of response and it falls apart completely returning this strange error.

One more word of advice is recommendation to use modern http client rather then pure http client. Modern http client returns less "generic" error messages that ultimately directed me to above solution.
Comment 5 Leo Mahon 2015-08-03 07:14:12 UTC
Like Frederik I am using Restsharp, when you choose an endpoint in RestSharp you have to end with "/" otherwise it does not work. If wkoncki is right then that needs fixing in RestSharp.
Comment 6 Till Lorentzen 2015-08-17 04:54:20 UTC
Hi!
I am not sure if this lines of mono code are correct:
https://github.com/mono/mono/blob/master/mcs/class/System/System.Net/WebConnection.cs#L533-L536
Why is "0" an invalid result? If I am understanding this documentation (https://msdn.microsoft.com/de-de/library/system.io.stream.endread(v=vs.110).aspx) correctly, it is a valid value and could happen if Stream.EndRead is called and accidentally is called at the end of the stream with no more buffered read data left in stream. And this seems to be a valid situation.

The simplest way to patch the error would be to erase the marked lines of code, but I am not sure if that would break calls after this check or if a simple "return" instead of throwing an exception would be the correct way to handle this. I tried to figure out which unit test case is used to validate the functionality of this class but couldn't find it. Could you please give me an advice where I have got to look for it.

Best regards

Till
Comment 7 Leo Mahon 2015-08-17 05:46:10 UTC
I added it as an issue on RestSharp but if you are right Till then it may be a problem with mono.
https://github.com/restsharp/RestSharp/issues/731
Comment 8 Marek Habersack 2015-08-25 07:46:44 UTC
This error no longer occurs with neither Mono/master nor XA/master (to become the next XA release). Tested with code supplied by OP.
Comment 9 mgwalm 2015-09-15 03:37:29 UTC
what do you mean by Xamarin.Android v4.12 .

do you mean Xamarin.Android.Support.v4.12.??
Comment 10 Till Lorentzen 2015-10-27 11:28:05 UTC
@Marek Thanks a lot for the fix!

Which linux mono release do we have to use? Is it released and packaged already? Which git commit must be included in a release for the fix?
Comment 11 Marek Habersack 2015-10-27 14:16:00 UTC
@till, 4.x will do :)
Comment 12 mgwalm 2015-10-27 17:44:53 UTC
I don't think its fixed.

Im still getting it. im calling a WCF service using REST. 
Im sendind some large images, under 100K. The transport is streamed.

Im getting the error after the 5 or 6th stream, sometimes I dont get it at all.

So ive broken it up into 4k chunks and am sending the chunks separately and then re-building them back up on the server. The problem still occurs every now and then.

the problem only started around the end of july, the release of VS2015 and a new release of Xamarin.

It looks as if the service or the client is disconnecting before all the streams are seent. It could be related to the network being busy, im not sure.

my environment is:
Microsoft Visual Studio Professional 2015
Version 14.0.23107.0 D14REL
Microsoft .NET Framework
Version 4.6.00081

Installed Version: Professional

Visual Basic 2015   00322-40000-00000-AA608
Microsoft Visual Basic 2015

Visual C# 2015   00322-40000-00000-AA608
Microsoft Visual C# 2015

Visual C++ 2015   00322-40000-00000-AA608
Microsoft Visual C++ 2015

Windows Phone SDK 8.0 - ENU   00322-40000-00000-AA608
Windows Phone SDK 8.0 - ENU

Application Insights Tools for Visual Studio Package   1.0
Application Insights Tools for Visual Studio

ASP.NET and Web Tools   14.0.20626.0
ASP.NET and Web Tools

ASP.NET Web Frameworks and Tools 2013   5.2.30624.0
For additional information, visit http://www.asp.net/

Common Azure Tools   1.5
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

GenerateUnitTest   1.0
Generates unit test code for methods in classes under test.

GitHub.VisualStudio   1.0
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

Indent Guides   14
Indent Guides

Adds visual guides at each indentation level.

JetBrains ReSharper Ultimate 2015.1.1   Build 102.0.20150521.123255
JetBrains ReSharper Ultimate package for Microsoft Visual Studio. For more information about ReSharper Ultimate, visit http://www.jetbrains.com/resharper. Copyright © 2015 JetBrains, Inc.

Microsoft Azure Mobile Services Tools   1.4
Microsoft Azure Mobile Services Tools

NuGet Package Manager   3.2.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

Office Developer Tools for Visual Studio 2015 ENU   14.0.23025
Microsoft Office Developer Tools for Visual Studio 2015 ENU

Open Command Line   2.0.157
Opens a command line at the root of the project. Support for all consoles such as CMD, PowerShell, Bash etc.

PreEmptive Analytics Visualizer   1.2
Microsoft Visual Studio extension to visualize aggregated summaries from the PreEmptive Analytics product.

SQL Server Compact & SQLite Toolbox   4.3.0
SQL Server Compact & SQLite Toolbox adds scripting, import, export, rename, query execution and much more to SQL Server Compact & SQLite Data Connections.

SQL Server Data Tools   14.0.50616.0
Microsoft SQL Server Data Tools

Visual Studio Spell Checker   VSSpellChecker
An editor extension that checks the spelling of comments, strings, and plain text as you type.

Visual Studio Tools for Universal Windows Apps   14.0.23309.00 d14oob
The Visual Studio Tools for Universal Windows apps allow you to build a single universal app experience that can reach every device running Windows 10: phone, tablet, PC, and more. It includes the Microsoft Windows 10 Software Development Kit.

Workflow Manager Tools 1.0   1.0
This package contains the necessary Visual Studio integration components for Workflow Manager.

Xamarin   3.11.837.0 (f10676f)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin.Android   5.1.5.3 (f98871a95a479f6d71b3067b7e5834d41fcb2118)
Visual Studio plugin to enable development for Xamarin.Android.

Xamarin.iOS   8.10.4.0 (6db87c53c073f4af2f5247fb738a27ea08c094fd)
Visual Studio extension to enable development for Xamarin.iOS.

Xamarin.iOS Unified Migration   1.0
Automated migration for Xamarin iOS Classic projects to Unified

Xamarin.TestCloud.Integration   1.0
Early preview of Xamarin Test Cloud integration
Comment 13 Marek Habersack 2015-10-27 18:24:35 UTC
@mgwalm, note that in comment 8 I mentioned it's fixed in *XA/master* which hasn't been released yet. The fix is going to be part of the next XA release.
Comment 14 mgwalm 2015-10-27 18:27:17 UTC
ok, thanks. by XA do you mean xamarin.android or xamarin.android.support
Comment 15 Marek Habersack 2015-10-27 18:52:51 UTC
Xamarin.Android
Comment 16 Till Lorentzen 2015-10-28 04:24:47 UTC
@Marek I am using mono 4.0.4 and this issue occurs. So could you please specify the minimum version. 4.x means 4.1 and above, or 4.0 and above?

Thanks a lot
Comment 17 Marek Habersack 2015-10-28 06:55:41 UTC
Sorry about the confusion, try 4.2 or newer
Comment 18 Till Lorentzen 2015-10-28 07:34:09 UTC
@Marek Thanks a lot for clarification. mono 4.2.1 is available via alpha channel ATM. Is it possible to backport the fix to the actual stable version 4.0.4? Could you list the commits which are relevant to fix the problem?

We were able to build patched versions on our own system to include bug fixes faster then waiting for the next xamarin release. But we noticed that the source packages are not available via the official repo any more. So could you tell us a release date for 4.2.x? Or give us a hint about the location or mechanism to get the source packages?

I am asking because https traffic isn't working at the moment and so our application is badly unusable in this state.

Thanks a lot!
Comment 19 Marek Habersack 2015-10-28 07:53:10 UTC
(In reply to comment #18)
> @Marek Thanks a lot for clarification. mono 4.2.1 is available via alpha
> channel ATM. Is it possible to backport the fix to the actual stable version
> 4.0.4? Could you list the commits which are relevant to fix the problem?
It would be hard to backport the fix since, in fact, I didn't fix it and nobody else *intentionally* fixed this bug. The code working (and most likely the breakage as well) is the result of using reference source and the multitude of changes in our runtime and class libraries required to make it work. For this reason, lamentably, I can't give you the specific commits :( I tested the issue with Mono/master (then it was the version that now is 4.2) and XA/master (which back then was also based on 4.2) and saw that it was working fine. There have been a multitude of fixes related to System.Net, TLS as well as threading (which may play some role here as well) - especially that we switched to a new threadpool implementation in the runtime to make it work better with the referencesource threadpool. So, it's next to impossible to find either the cause or the fix for this bug without a lot of time spent bisecting (and in this case it would be a very time consuming task, alas) both mono and xa in unison.

> We were able to build patched versions on our own system to include bug fixes
> faster then waiting for the next xamarin release. But we noticed that the
> source packages are not available via the official repo any more. So could you
> tell us a release date for 4.2.x? Or give us a hint about the location or
> mechanism to get the source packages?
Mono 4.2 should be released very soon, to my knowledge. There's no fixed date - it goes out of the door once it's ready for consumption. As for source packages, check:

http://www.mono-project.com/download/alpha/#download-lin
http://download.mono-project.com/sources/mono/
http://jenkins.mono-project.com/repo/debian/pool/main/m/

Or you can easily generate a tarball with the latest 4.2 sources by checking out mono from git:

https://github.com/mono/mono/tree/mono-4.2.0-branch

then make sure you have all the build dependencies installed, run autogen.sh in the top directory you've just checked out and then run 'make dist' - that will give you the tarball.

> I am asking because https traffic isn't working at the moment and so our
> application is badly unusable in this state.
I would recommend giving 4.2 a try. If you don't want to risk using the full rig, you can try replacing your System.Net.dll and, possibly, System.dll with those from 4.2 - but beware, things may break in subtle ways if you do so (or even not work at all) since there might be runtime issues involved.

> 
> Thanks a lot!
Comment 20 Till Lorentzen 2015-10-28 07:58:16 UTC
Thanks a lot for all the details! So we will give 4.2.0-branch a try.
Comment 21 Andrew Rondeau 2015-12-08 23:34:16 UTC
Could we please re-open this ticket? I'm encountering this error on Mono 4.2.1 (explicit/6dd2d0d Tue Nov 10 18:09:46 EST 2015), running on OSX.

Probably the most frustrating thing is that I do not know if this is a true "bug" in mono; or if there is a legitimate problem with the HTTP response. If the HTTP server has a problem; I wish the error was a bit more descriptive.

The error is:
System.Net.WebException: Error getting response stream (ReadDone2): ReceiveFailure ---> System.Exception:   at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) [0x00000] in <filename unknown>:0 
  at System.Net.WebConnection.ReadDone (IAsyncResult result) [0x00000] in <filename unknown>:0 
  at System.Runtime.Remoting.Messaging.AsyncResult.Invoke (System.Runtime.Remoting.Messaging.AsyncResult ) [0x00000] in <filename unknown>:0 
  at System.Runtime.Remoting.Messaging.AsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] in <filename unknown>:0 
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00000] in <filename unknown>:0 
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x00000] in <filename unknown>:0 
  at System.Net.WebConnection.HandleError (WebExceptionStatus st, System.Exception e, System.String where) <0x1178239f0 + 0x001a3> in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) <0x115f2ca10 + 0x00211> in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () <0x115f16500 + 0x0005a> in <filename unknown>:0
Comment 22 Aymen 2016-01-18 23:41:04 UTC
The same issue is randomly occurring with me when I use the Amazon S3 rest api to get objects. I think that this ticket should be re-opened.
I'using : 
** Mono JIT compiler version 4.2.1 (Stable 4.2.1.102/6dd2d0d Thu Nov 12 09:52:44 UTC 2015)

** Linux <hostname> 4.2.0-16-generic #19-Ubuntu SMP Thu Oct 8 15:35:06 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
Comment 23 Marek Habersack 2016-01-19 08:12:17 UTC
@Andrew, @Aymen - both of you report issues with the API on "regular" machines, not mobile devices. This bug was about the Android issue on mobile devices. If you wish to improve the API error reporting, please file a bug with mono for the class libraries component, as it's not specific to Xamarin.Android in any way.

@Andrew - you might want to build mono/master (current tip or newer) as it now includes more diagnostics related to network I/O (among others). You can enable the diagnostics by placing the following variables in the application environment:

  MONO_LOG_LEVEL=debug
  MONO_LOG_MASK=io-layer

This will tell you if and what, if any, native errors occur when sending/receiving data over the network. If you can enable debug information for your app then it would be most helpful for the developer working on the bug since the stack trace would have line numbers.

@Andrew, this particular error means that 0 bytes were read from the network (https://github.com/mono/mono/blob/master/mcs/class/System/System.Net/WebConnection.cs#L479)
Comment 24 Zeioth 2016-04-18 19:14:44 UTC
I'm experiencing this bug too, randomly. Running my aplication in linux. I'm controlling it with catch(IOException), but it doesn't semms to catch it, so i can't silence it. I'm using MonoDevelop alpha repository.

System.Net.WebException: Error getting response stream (ReadDone1): ReceiveFailure ---> System.IO.IOException: Unable to read data from the transport connection: Connection reset by peer. ---> System.Net.Sockets.SocketException: Connection reset by peer
  at System.Net.Sockets.Socket.EndReceive (IAsyncResult result) <0x41fd4770 + 0x0009f> in <filename unknown>:0 
  at System.Net.Sockets.NetworkStream.EndRead (IAsyncResult asyncResult) <0x41fd43d0 + 0x0005f> in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at Mono.Security.Protocol.Tls.SslStreamBase.EndRead (IAsyncResult asyncResult) <0x4203af60 + 0x0015f> in <filename unknown>:0 
  at Mono.Net.Security.Private.LegacySslStream.EndRead (IAsyncResult asyncResult) <0x4206ffb0 + 0x0002c> in <filename unknown>:0 
  at System.Net.WebConnection.ReadDone (IAsyncResult result) <0x4206f400 + 0x00155> in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.HttpWebRequest.EndGetResponse (IAsyncResult asyncResult) <0x420313d0 + 0x0019f> in <filename unknown>:0 
  at System.Net.HttpWebRequest.GetResponse () <0x4202dac0 + 0x00053> in <filename unknown>:0 
  at SharedLibrary.Wrappers.WebUtils.WebRequestWrapper.Get (System.String url) <0x42024550 + 0x000e2> in <filename unknown>:0