Bug 23700 - App using SocketAsyncEventArgs sometimes crash with ObjectDisposedException, stacktrace starts in DispatcherCB
Summary: App using SocketAsyncEventArgs sometimes crash with ObjectDisposedException, ...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 8.2.0
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Martin Baulig
URL:
Depends on:
Blocks:
 
Reported: 2014-10-09 08:23 UTC by Michal Dobrodenka
Modified: 2014-10-28 12:54 UTC (History)
4 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 Michal Dobrodenka 2014-10-09 08:23:55 UTC
ObjectDisposedException in mono class libraries:

graphSystem.Net.Sockets.Socket.EndConnect (result={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.2.0.193/src/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1274
System.Net.Sockets.SocketAsyncEventArgs.ConnectCallback (ares={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.2.0.193/src/mono/mcs/class/System/System.Net.Sockets/SocketAsyncEventArgs.cs:260
System.Net.Sockets.SocketAsyncEventArgs.DispatcherCB (ares={System.Net.Sockets.Socket.SocketAsyncResult}) in ///Library/Frameworks/Xamarin.iOS.framework/Versions/8.2.0.193/src/mono/mcs/class/System/System.Net.Sockets/SocketAsyncEventArgs.cs:234


Just these 3 lines in stacktrace.

Crashing is random. Sometimes it crashes every time I run the app (in simulator or iPhone), sometimes only once per day. Since Stack trace does not begin from my code, it's not easy to make a workaround.

The same socket code is used in WP7,8 & Android app without problems.
Comment 1 Udham Singh 2014-10-09 09:04:27 UTC
I can't reproduce this, we may have to see if a developer can figure out what's going on from the stack trace. If you have a chance, it might also be helpful to include build information and logs ( symbolicated + IDE).

You can get build information and logs from here:

Build Information : Xamarin Studio => About Xamarin Studio => Show Details => Copy Information

Symbolicate logs: Xcode => Window => Devices => Select Device and copy the logs.

IDE logs:  Xamarin Studio => Help => Open Log Directory => Choose the latest IDE logs.
Comment 2 Michal Dobrodenka 2014-10-09 09:27:17 UTC
Build information:

Xamarin Studio
Version 5.6.1 (build 4)
Installation UUID: cc4d0c4c-9d4a-4324-97f2-a53c0e746318
Runtime:
	Mono 3.10.0 ((detached/633e444)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000020

Apple Developer Tools
Xcode 6.0.1 (6528)
Build 6A317

Xamarin.Mac
Version:

Xamarin.iOS
Version: 8.2.0.193 (Business Edition)
Hash: 959c1e4
Branch: 
Build date: 2014-10-03 00:25:37-0400

Xamarin.Android
Not Installed

Build Information
Release ID: 506010004
Git revision: b7129a142994cb144c5455eba7060712ba6c02cf
Build date: 2014-10-07 21:04:12-04

Operating System
Mac OS X 10.9.4
Darwin BzingMac.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64


Tried it also from windows/mac build host, with the same result.
Comment 3 Michal Dobrodenka 2014-10-09 09:27:46 UTC
When problem occur again, I will try to get all other info.
Comment 4 Michal Dobrodenka 2014-10-09 09:53:42 UTC
When looking at mono sources, it seems that it could happen when connection is lost during Socket Connecting, maybe just missing ObjectDisposedException catching in void ConnectCallback (IAsyncResult ares) in SocketAsyncEventArgs.cs 

Look for example in DisconnectCallback exception handling:

		try {
				curSocket.EndDisconnect (ares);
			} catch (SocketException ex) {
				SocketError = ex.SocketErrorCode;
			} catch (ObjectDisposedException) {
				SocketError = SocketError.OperationAborted;
			} finally {
				OnCompleted (this);
			}

just adding

catch (ObjectDisposedException) {
				SocketError = SocketError.OperationAborted;
			}

into void ConnectCallback (IAsyncResult ares) exception catches would probably fix this bug.

But not sure.
Comment 5 Michal Dobrodenka 2014-10-09 11:13:57 UTC
Commit [link](https://github.com/mono/mono/commit/0c55c45450d4ad9af15acf931b31eed5f594db71)

did this, before this commit - all exceptions were caught

after this, all other exceptions crashes the app. This commit fixes bug [link](https://bugzilla.xamarin.com/show_bug.cgi?id=21017) but introduces (I think) this bug.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2014-10-13 06:56:20 UTC
Martin, can you have a look at this? The commit mentioned in comment #5 mentions bug #21017, which was assigned to you.
Comment 7 Alexis Christoforides 2014-10-28 12:54:53 UTC
Michael, I merged your pull request since Socket can definitely throw ObjectDisposedExceptions according to code and contract.

https://github.com/mono/mono/commit/e0dab7749a4cfd42e9f8d05b14f1944f802c42ef

Thanks!