Bug 25402 - Connection Reset by Peer Exception
Summary: Connection Reset by Peer Exception
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.6.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2014-12-15 14:00 UTC by John Miller [MSFT]
Modified: 2016-07-31 16:40 UTC (History)
9 users (show)

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 for Bug 25402 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:

Description John Miller [MSFT] 2014-12-15 14:00:29 UTC

   The attached private sample project gets an exception occasionally when downloading data using WebRequest.

**Steps to Reproduce:**

   See private info in comment below. 

**Actual Results:**

   An exception is thrown in Socket_2_1.cs public int EndSend (IAsyncResult result)
   "Connection Reset by Peer"

>System.Net.Sockets.Socket.EndSend (result={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Net.Sockets.NetworkStream.EndWrite (ar={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.RecordProtocol.EndSendRecord (asyncResult={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (contentType=Mono.Security.Protocol.Tls.ContentType.Alert, recordData={byte[2]}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (alert={Mono.Security.Protocol.Tls.Alert}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (description=Mono.Security.Protocol.Tls.AlertDescription.CloseNotify) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.SslStreamBase.Dispose (disposing=true) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>Mono.Security.Protocol.Tls.SslClientStream.Dispose (disposing=true) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.IO.Stream.Close () in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.IO/Stream.cs:113
>Mono.Security.Protocol.Tls.SslStreamBase.Close () in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Net.WebConnection.Close (sendNext=false) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Net.WebConnectionGroup.TryRecycle (maxIdleTime={00:01:40}, idleSince={1/1/0001 12:00:00 AM}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Net.ServicePoint.CheckAvailableForRecycling (outIdleSince={1/1/0001 12:00:00 AM}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Net.ServicePoint.IdleTimerCallback (obj=(null)) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/
>System.Threading.Timer.Scheduler.TimerCB (o={System.Threading.Timer}) in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/Timer.cs:317

**Expected Results:**

   No Exception.

**Build Date & Platform:**

   === Xamarin Studio ===

Version 5.7 (build 646)
Installation UUID: e01c3049-a2d2-4e0a-aad8-afe6fb627c4d
	Mono 3.12.0 ((detached/dc0ce1d)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 312000066

=== Apple Developer Tools ===

Xcode 6.1.1 (6611)
Build 6A2008a

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 142934d
Build date: 2014-12-09 10:40:45-0500

=== Build Information ===

Release ID: 507000646
Git revision: 4574f534cc8c822ac1a4b2f31450250afb6511d3
Build date: 2014-12-09 16:28:52-05
Xamarin addins: c3c87b666323c163f0ad02618d1753533cf0a4f9

=== Operating System ===

Mac OS X 10.10.0

**Additional Information:**

   The project provided by the customer will reproduce this error, but it did take a few attempts. Eventually the stack trace from the crash will be related to Protobuf.net not being able to deserialize the response, however I think the underlying issue is someone in the network stack. 
   I have gotten a few stack traces trying to track this down, please see the attached Trace.txt for them. 
   The protobuf exception is "Sub-message not read correctly", but I am not sure that is actually related to the problem. I think this exception in Protobuf would be expected considering the webclient response threw an exception.
Comment 3 Miguel de Icaza [MSFT] 2014-12-19 14:14:49 UTC
The stack trace is not very useful, because it is normal for that to throw in that location (and it is being caught in that place WebConnection.cs:1184).   So what you are seeing is just the normal behavior.

What we need is the actual description of the problem when the application is executed, ignoring the changes that the debugging execution environment does (in this case, setting those flags for catching all exceptions).
Comment 4 John Miller [MSFT] 2014-12-19 18:05:18 UTC

Sorry it was unclear. Here is what the customer is reporting

"Random requests are coming back with a 'Connection Reset by Peer' exception that is timing after a lengthy wait. (around 60 seconds)."

Its unclear why these exceptions happen and why the webrequest timeout. I was able to reproduce this per my private comments above.
Comment 9 Miguel de Icaza [MSFT] 2015-01-16 12:54:16 UTC
Hello John,

We do not have any updates.   Did you get a simpler test case created?

One idea that I had was that the user could replace the raw use of HttpWebRequest, with HttpClient, and use the CFNetwork transport.

If they are not tied to HttpWebRequest, this has many advantages, for example, it automatically turns on the radio on phones.
Comment 10 John Miller [MSFT] 2015-01-19 13:55:01 UTC
Thanks Miguel,

No simpler test case is available. The project provided does reproduce this issue.
Comment 11 Dennis Welu 2015-02-06 12:20:37 UTC
Fwiw, I've been trying to run down this same exception that happens "randomly" based on outward appearances. And I see it on both iOS and Android. The short stack I have from Xam Insights shows it on the receiving/reading end:

  at System.Net.Sockets.Socket.EndReceive (IAsyncResult result) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.NetworkStream.EndRead (IAsyncResult ar) [0x00000] in <filename unknown>:0
Comment 12 Laura Zarioiu 2015-04-01 05:08:13 UTC
I am already using HttpClient and I do receive this error. It's random and appears only in the iOS application, not in the Windows app. Din changing HttpWebRequest to HttpClient work for anyone ?
Comment 13 fred 2015-05-04 16:31:09 UTC
i am also getting this exception. i am using HttpClient