Bug 19334

Summary: BeginWrite failure at System.Net.Sockets.NetworkStream.BeginWrite
Product: iOS Reporter: asp_net <waschbecken>
Component: Xamarin.iOS.dllAssignee: Martin Baulig <martin.baulig>
Status: RESOLVED FIXED    
Severity: normal CC: alex, betai.koffi, ddunkin, dschwegler, grant.lodge, heavycrag, johannes.rudolph, john.miller, j.rademaker, kaolick, kpickard, ltorje, luigisag, lukaslipka, marcb1284, martin.potter, miguel, mono-bugs+monotouch, nberardi, pr0vieh, rolf, ryan, sbarouline, sebastien, srinivasgadda, sudharani, tajinders, tom.philpot
Priority: Normal    
Version: 7.2.1   
Target Milestone: Untriaged   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description asp_net 2014-04-28 06:55:31 UTC
I am facing randomly errors when connecting trough HTTPS to my server. The certificate has been issued by PositiveSSL, so it is not self-signed.

Stacktrace:

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 \n  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 \n  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 \n  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 \n  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 \n  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0 

Version:

Version: 7.2.1.31
Comment 1 Sebastien Pouliot 2014-04-28 08:26:19 UTC
We'll need more information (the above does not provide the exception type that was thrown) and likely a test case.

Note: Certificate validation errors should not be 'random' - it should pass or fail, so it's likely something else.
Comment 2 Tom Philpot 2014-05-07 09:06:45 UTC
We're running Mono 3.4.0 and we're seeing the same issue on Mac

IOException: BeginWrite failure in System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state)
    System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state):0
    Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord(ContentType contentType, Byte[] recordData, AsyncCallback callback, Object state):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendRecord(ContentType contentType, Byte[] recordData):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendAlert(Alert alert):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendAlert(AlertDescription description):0
    Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker(IAsyncResult result):0

If we find a repro test case, we'll add more info.
Comment 3 Tom Philpot 2014-05-07 12:51:57 UTC
See https://github.com/mono/mono/pull/1023 which may help diagnose this issue.
Comment 4 Tom Philpot 2014-05-12 13:35:17 UTC
Updated stack trace with the inner exception:

IOException: BeginWrite failure in System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state) SocketException: The socket is not connected in System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, AsyncCallback callback, Object state)
    System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state):0
    Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord(ContentType contentType, Byte[] recordData, AsyncCallback callback, Object state):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendRecord(ContentType contentType, Byte[] recordData):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendAlert(Alert alert):0
    Mono.Security.Protocol.Tls.RecordProtocol.SendAlert(AlertDescription description):0
    Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker(IAsyncResult result):0

SocketException: The socket is not connected in System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, AsyncCallback callback, Object state)
    System.Net.Sockets.Socket.BeginSend(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, AsyncCallback callback, Object state):0
    System.Net.Sockets.NetworkStream.BeginWrite(Byte[] buffer, Int32 offset, Int32 size, AsyncCallback callback, Object state):0
Comment 5 Tom Philpot 2014-05-29 00:38:28 UTC
We added a patch to our fork that we're testing internally. Adding it here in case others want to try it out:

https://github.com/LogosBible/mono/commit/911c4a5c528cc16894888d968787effb3f16b675
Comment 9 Miguel de Icaza [MSFT] 2014-07-02 19:04:28 UTC
Tom,

Were you guys able to gather any more information from the crash you are experiencing, or get a reproducible test case?
Comment 10 Tom Philpot 2014-07-02 19:54:55 UTC
Miguel,

Martin Potter (cc'd on the case) hacked our fork on github (see previous comment) to work around crash so we didn't spend any more time on the original case.
Comment 11 Miguel de Icaza [MSFT] 2014-07-03 10:10:53 UTC
Tom, Martin, can you guys share the work around that you used?
Comment 12 Tom Philpot 2014-07-03 10:29:22 UTC
The work around is here: https://github.com/LogosBible/mono/commit/911c4a5c528cc16894888d968787effb3f16b675

Sure, do you just need this us to make this patch available under the appropriate license? (MIT/X11, I presume?). If so, do you want that permission in this case or would you prefer it on the mono-devel list?
Comment 13 Rolf Bjarne Kvinge [MSFT] 2014-07-14 11:32:50 UTC
@Tom, the best is usually to make the MIT/X11 notice in the commit message, and then issue a pull request. It would also be useful to have this bug # in the commit message somewhere.

Note that I have not reviewed the patch for correctness.
Comment 14 Lukas Lipka 2014-10-17 11:28:24 UTC
I am seeing the same issue in our app that results in random crashes:

	at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (Mono.Security.Protocol.Tls.ContentType,byte[],System.AsyncCallback,object) <0x00058>
	at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (Mono.Security.Protocol.Tls.ContentType,byte[]) <0x00024>
	at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert) <0x00078>
	at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.AlertDescription) <0x0003c>
	at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (System.IAsyncResult) <0x00ea8>
	at (wrapper runtime-invoke) object.runtime_invoke_dynamic (intptr,intptr,intptr,intptr) <0x000c8>
Comment 15 Tom Philpot 2014-10-17 12:43:09 UTC
Added a PR here: https://github.com/mono/mono/pull/1351
Comment 16 mrwithersea 2014-10-27 12:10:29 UTC
This is also affecting our app, it's not a consistent error and so we are unable to reproduce it but with Hockey logs we have discovered the error has occurred 25 times in the last 7 days in our live production environment. Could really do with some help on this Xamarin
Comment 17 Jacco Rademaker 2014-10-31 06:29:21 UTC
Our app also has this inconsistent behavior. It is not frequent, but it does occur.
Sometimes on app start up, sometimes after people are using our app for 5 minutes or more.
Comment 18 Lukas Lipka 2014-11-29 13:58:29 UTC
Bump, this has caused 470 crashes in the latest version of our iOS app.
Comment 19 kaolick 2014-12-16 03:59:58 UTC
We have the same problem in our published app. This is our stack trace from Xamarin.Insights:

System.IO.IOExceptionBeginWrite failure

Stacktrace

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 20 Srinivas G 2015-01-05 02:50:16 UTC
Yes, I too had the same issue

StackTrace:

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0 

Does anyone found a solution to fix it?
Comment 21 Lucian Torje 2015-01-12 03:01:50 UTC
Same thing happens on android:

Phone Model: VS980 4G
Android Version: 4.4.2
Board: MSM8974
ID: KOT49I.VS98026A
Total Internal memory: 126984192
Available Internal memory: 84127744


Stack:

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 

  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 

  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 

  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 

  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 

  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 22 Ryan 2015-01-13 04:06:29 UTC
I had this too, showed up on Xamarin Insights.  iPhone 5s

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 23 Martin Baulig 2015-01-15 06:05:04 UTC
All I can see from these stack traces is that the actual error has been "eaten" by the exception handling code.

This patch should fix that and provide a meaningful exception message: https://github.com/mono/mono/commit/95eff2495093ccd20ad2db7df3f07a835d580e5e
Comment 24 Miguel de Icaza [MSFT] 2015-01-15 16:07:40 UTC
Hello guys,

We have prepared a package of MonoTouch that contains the diagnostic information that would allow us to take the next step into figuring out what is happening here.

What you need to do is:

(a) Install the package below
(b) Rebuild your app
(c) Run the app to the point where this happens
(d) Provide us with the console logs and any exception logs that you get.

http://files.xamarin.com/~rolf/monotouch-8.6.1.6.pkg
Comment 25 Lukas Lipka 2015-01-15 16:57:39 UTC
Hey Miguel,

Thanks for finally looking into this!However the problem (at least for us) is that we were so far unable to replicate this ourselves. We only observe this problem in production on a user base of 250k where we usually get like 200 crashes over a period of a week and a half.

I don't know if there is an option to run this in production and then just observe if any new exceptions pop up in Hockeyapp or if that would even be useful?

I may try stress testing our network calls and see if I can replicate it locally, but so far I have been unable to do so and have no idea under what conditions this crash even occurs.
Comment 26 Dave Dunkin 2015-01-15 16:59:14 UTC
I'm in the same boat as Lukas. We see this in crash reports from users in the field, but can't reproduce it internally.
Comment 27 Ryan 2015-01-16 04:15:19 UTC
Same here i'm afraid - I just saw it once on Xamarin Insights after the launch of our new app this week, from perhaps nearly 1000 installs. 

The actual exception was "System.IO.IOException BeginWrite failure" followed by the stack trace I posted above. 

It happened on the launch/welcome screen which doesn't even perform any network calls. The only thing I can think of is perhaps it was something to do with storing shared preferences as the user is asked to opt in/out of both local notifications (standard iOS dialog) and Analytics just beforehand.
Comment 28 Miguel de Icaza [MSFT] 2015-01-16 12:56:08 UTC
Ryan,

This is beyond odd.   Because this exception is thrown by the SslStream/Tls stack.
Comment 29 Martin Baulig 2015-01-16 13:28:49 UTC
This is really odd.

However, you should get a better stack trace with a more meaningful error message with the package from comment #24.
Comment 30 Lukas Lipka 2015-01-24 03:59:20 UTC
We switched to Hockeyapp recently and it seems it reports more info:

Info: *** Terminating app due to uncaught exception 'System.IO.IOException: BeginWrite failure', reason: 'System.IO.IOException: BeginWrite failure ---> System.ObjectDisposedException: The object was used after being disposed. at System.Net.Sockets.Socket.BeginSend (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0 '
Comment 31 kpickard@flightdocs.com 2015-01-28 09:53:51 UTC
I just got the same crash report from our production app via hockey as @Lukas Lipka.  We are also using HTTPS.  

Just a guess but perhaps there is some race condition where a Socket instance is re-used after it has been disposed but before it has been removed from the collection of Sockets eligible for re-use?

*** Terminating app due to uncaught exception 'System.IO.IOException: BeginWrite failure', reason: 'System.IO.IOException: BeginWrite failure ---> System.ObjectDisposedException: The object was used after being disposed.
  at System.Net.Sockets.Socket.BeginSend (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0 '
Comment 32 S_CC 2015-02-02 04:39:53 UTC
I got the same crash report in xamarin insights. iPhone 5s.

System.IO.IOExceptionBeginWrite failure:

Stack Trace:

at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 33 Luigi Saggese 2015-02-03 03:48:45 UTC
I have same crash report.

at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0 


Are over 3 months that this error happens. There is a solution?
Comment 34 Miguel de Icaza [MSFT] 2015-02-03 08:57:58 UTC
Guys, please see comment #24
Comment 35 Luigi Saggese 2015-02-03 09:57:24 UTC
Hello Miguel,
this kind of issue, from my side, it's not replicable on my test environment. 
I have found this issue, same as other developers, via Xamarin Insight.

I could release this ipa, compiled via monotouch 8.6.1.6, and waiting to have new issue reported via Xamarin Insight. It could take time and i don't know if it's a stable version.

What do you think? How could we help you?
Comment 36 Luigi Saggese 2015-02-05 02:40:50 UTC
This issue https://bugzilla.xamarin.com/show_bug.cgi?id=11570 seems to be linked to this one. See stacktrace.

I don't understand why an issue (11570) dated 2013-04-04 it's not already fixed (2 years!), and this one posted 1 year ago it's in "debugging" state yet...
Comment 37 Nick Berardi 2015-02-09 20:08:44 UTC
We are seeing the same thing in our Mac App.  This was build on 3.12.0 on Mac OSX 10.9.4, 10.9.5, 10.10.0, and 10.10.1.

System.IO.IOException: BeginWrite failure ---> System.Net.Sockets.SocketException: The socket is not connected
  at System.Net.Sockets.Socket.BeginSend (System.Byte[] buffer, Int32 offset, Int32 size, SocketFlags socket_flags, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  --- End of inner exception stack trace ---
  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in <filename unknown>:0 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00000] in <filename unknown>:0
Comment 39 Luigi Saggese 2015-02-12 09:09:11 UTC
Hello Miguel,
i have deployed a version compiled with monotouch 8.6.1.6 and that is stacktrace for bug reported:

  at System.Net.Sockets.NetworkStream.BeginWrite (System.Byte[] buffer, Int32 offset, Int32 size, System.AsyncCallback callback, System.Object state) [0x0007c] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/System/System.Net.Sockets/NetworkStream.cs:252 
  at Mono.Security.Protocol.Tls.RecordProtocol.BeginSendRecord (ContentType contentType, System.Byte[] recordData, System.AsyncCallback callback, System.Object state) [0x00026] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:835 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendRecord (ContentType contentType, System.Byte[] recordData) [0x00000] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:856 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (Mono.Security.Protocol.Tls.Alert alert) [0x00027] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:703 
  at Mono.Security.Protocol.Tls.RecordProtocol.SendAlert (AlertDescription description) [0x00000] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs:674 
  at Mono.Security.Protocol.Tls.SslClientStream.NegotiateAsyncWorker (IAsyncResult result) [0x00428] in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.6.1.20/src/mono/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/SslClientStream.cs:597
Comment 40 Luigi Saggese 2015-02-16 06:45:12 UTC
Any news? How could we help?
Comment 42 Martin Baulig 2015-02-18 02:09:35 UTC
Hey Luigi,

Unfortunately, your stack trace is as useless as all the ones before as it's actually just the same thing only with line numbers added.

Could you please provide the full exception message, including the error message and any frames above NegotiateWorker() if there are any.

The error message should say something like
"Error while sending TLS Alert (<level>:<description>): <inner exception>".

This is the code which should throw this:
https://github.com/mono/mono/blob/e1299372832be3a066b4c1a778f4d3c52a6f35c2/mcs/class/Mono.Security/Mono.Security.Protocol.Tls/RecordProtocol.cs#L680

I need to know which Alert it's attempting to send and what the inner exception is.  My guess is that it's CloseNotify.  CloseNotify is expected to fail if anything went wrong with the connection and the exception is caught and ignored.  What we need to figure out is what caused the CloseNotify being sent.

Martin
Comment 45 Luigi Saggese 2015-02-19 05:44:23 UTC
Do you see my screenshot?
How we want to proceed?
Comment 46 Miguel de Icaza [MSFT] 2015-02-20 12:18:23 UTC
From our code review we believe that this exception is being thrown when the remote end has closed the connection.   So we believe that this is harmless (modulo the fact that you need to catch it).

But we would like to confirm this with a live system.   If you have a way to reproduce this, or log the site that you connected to, we would like to hear from you.  We have a special build that will log a lot of information about the exact state of the TLS connection.

That said, you can switch the HTTP stack from our implementation to the implementation that comes with the OS by using the HttpClient() API.   The HttpClient API by default uses the HttpWebRequest implementation, but you can ask it to use our CFNetwork-based provider (instructions here: http://tirania.org/monomac//archive/2013/Jun.html) or you can use the ones that comes with ModernHttpClient which is even nicer, as it will also perform transfers in the background:

https://components.xamarin.com/view/modernhttpclient
Comment 47 Luigi Saggese 2015-02-20 12:28:51 UTC
Hello Miguel,
no i don't know how to reproduce problem but i can publish to AppStore and wait time of approval if you have some build to debug it (i have already done it following comment 24...why i must do it again...)

I think this problem it's really widespread although no one can play it in your environment but Xamarin Insight there are a lot of occurrences. About 20% of my users is affected. I don't take my hand on fire that is "harmless".

What do you suggest to do?
Comment 48 Miguel de Icaza [MSFT] 2015-02-20 12:33:32 UTC
Luigi,

I should have been more explicit.   No data loss is taking place, and what is happening is that the server closes the connection (they are done) and we at that point try to send an alert notification, and this is what raises the exception.

This still must be caught.   Ideally by our code, but currently would have to be done by yours.

To verify that our reading of the code is correct, we would like to share a version of our runtime that is more verbose to make sure that we are not missing anything.

I sent you an email separately
Comment 49 Nate Cook 2015-02-23 12:34:51 UTC
Not sure if it's exactly the same issue, but could be related so sharing here: We ran into a consistent BeginWrite failure while uploading a data file on a slow connection and were able to prevent that bug by setting AllowWriteStreamBuffering on the HttpWebRequest to false. For some reason setting that allows the request to complete successfully regardless of how long it takes to finish. Of course that only works when uploading files. (You'll get errors if you set that property to false in other contexts. It requires, for example, request.ContentLength to be set as well.) Anyway, hope that information helps someone. We're looking forward to seeing this fixed in Mono itself, because we are also seeing this exception intermittently in our app in situations where we're not uploading a file. However, like the others, we're not able to consistently reproduce it in those other contexts.
Comment 50 Luigi Saggese 2015-02-24 03:56:41 UTC
Unfortunately we use  HttpClient and there are access limitation on AllowWriteStreamBuffering.
Comment 51 Johannes Rudolph 2015-02-24 04:19:55 UTC
We are experiencing BeginWriteFailure Exceptions even when we use the "trick" provided by @Nate Cook:

            var request = WebRequest.CreateHttp( new Uri( "https://example.com" ) );
            request.Method = "PUT";
            request.AllowWriteStreamBuffering = false;
            request.AllowReadStreamBuffering = false;
            request.ContentLength = length;

We've had this code in production for a couple of months now and we originally disabled buffering because we need accurate progress tracking. Uploading rather largish files too (>10mb). We have this code in an iOS and in an Android App. For some reason we get more exception reports for this bug on Android than on iOS (despite a larger iOS install base).
Comment 52 Miguel de Icaza [MSFT] 2015-02-24 10:08:15 UTC
Some updates:

@Luigi: If you are using HttpClient then you *can* switch to ModernHttpClient, which will not use this code, see the comments before about it.

@everyone: right now, many of these exceptions are being thrown by the Insights client, so likely an Insights/Insights Server issue.   We found out that apps that do *no networking* but use the Insights client are logging a lot of these.
Comment 53 Lukas Lipka 2015-02-24 10:11:01 UTC
We do not use Xamarin Insights and still get a ton of these.

But I guess Insights uses the same networking stack, so I guess it makes sense for some apps that don't do networking on their own.
Comment 54 Johannes Rudolph 2015-02-24 10:11:43 UTC
@Miguel: I'm pretty sure it's a Mono/any Https issue. We're using Crashlytics and seeing the same issues in our networking code against our own online service and others.
Comment 55 Luigi Saggese 2015-02-24 11:38:39 UTC
@Miguel: ok i will try test ModernHttpClient but this "workaround" maybe not work for @everyone...
Comment 56 Miguel de Icaza [MSFT] 2015-02-25 11:55:04 UTC
Hello guys,

We have found the reason for the problem, when there are networking problems (server terminates connection, reset connection), we were trying to send the server a TLS alert, and this send operation was not protected with a try catch.

This build addresses this, and also logs more information to see what was the state the connection was:

http://storage.bos.internalx.com/monotouch-mavericks-bug19334/03/03f07b6838d3d710441a0c3afa8ce7c8af825e65/monotouch-8.6.99.5.pkg
Comment 57 Nate Cook 2015-02-25 12:34:41 UTC
Stupid question: how does one direct a Xamarin.iOS project to use the newly installed version of monotouch? Does that happen automatically upon installing the pkg? i.e. if I just open Xamarin Studio post-install all builds will use the new version without changing any settings?
Comment 58 Miguel de Icaza [MSFT] 2015-02-25 14:18:43 UTC
Correct, that is all that is needed.
Comment 59 Grant Lodge 2015-02-26 04:25:11 UTC
@Miguel any idea when this will be Stable?
Comment 60 alex 2015-03-18 01:52:27 UTC
Any updates on this?
Comment 61 Miguel de Icaza [MSFT] 2015-03-18 12:10:10 UTC
The fix already shipped, it was part of our TLS vulnerability fix release.