Bug 41477 - SafeSocketHandle ObjectDisposedException 'handle' exception with linked CancellationTokenSource
Summary: SafeSocketHandle ObjectDisposedException 'handle' exception with linked Cance...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: BCL Class Libraries (show other bugs)
Version: XI 9.4 (iOS 9.2)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: marcos.henrich
URL:
Depends on:
Blocks:
 
Reported: 2016-06-03 10:46 UTC by Chris Myhill
Modified: 2016-07-13 16:49 UTC (History)
5 users (show)

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


Attachments
iOS test application (5.44 MB, application/zip)
2016-06-03 10:46 UTC, Chris Myhill
Details

Description Chris Myhill 2016-06-03 10:46:04 UTC
Created attachment 16185 [details]
iOS test application

When I use a linked cancellation token to cancel a web request stream while its performing a download I am seeing a ObjectDisposedException exception from the SafeSocketHandle class.  This exception is thrown inside the mono framework and it is never raised to the calling code, hence I cannot catch it and proceed with my code as my cancelled code never receives the cancellation exception.

It seems that if I do not used linked tokens then the issue is not present, however this code represents a download process were we have the ability to cancel individual items as well as the overall download queue which are done with linked cancellation tokens.

We use the same code in a WPF application and do not see the same issue. It seems that this issue is not present in Microsoft.Net only in Mono.

I have only experienced this issue with a running iPhone or iPad applications.

I can replicate this issue quite often if I perform the cancellation just after it starts downloading the file.

Exception:

System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'handle'.

Stack:

0x22 in System.Net.Sockets.SafeSocketHandle.RegisterForBlockingSyscall at /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/SafeSocketHandle.cs:83,5	C#
 	0x1 in System.Net.Sockets.Socket.Receive_internal at /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/Socket.cs:1853,5	C#
 	0x62 in System.Net.Sockets.SocketAsyncWorker.Receive at /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/SocketAsyncWorker.cs:286,5	C#
 	0x50 in System.Net.Sockets.SocketAsyncWorker.DispatcherCB at /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/SocketAsyncWorker.cs:66,5	C#
 	0x6 in System.Net.Sockets.SocketAsyncResult.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem at /Users/builder/data/lanes/3234/d8bedd03/source/maccore/_build/Library/Frameworks/Xamarin.iOS.framework/Versions/git/src/mono/mcs/class/System/System.Net.Sockets/SocketAsyncResult.cs:307,4	C#
Comment 1 Chris Myhill 2016-06-03 15:07:06 UTC
So I played with this a little more and have come to the conclusion that the linked tokens is not the issue.  The issue is due to the Token.Register. If you look at the code you can see I am calling a Request.About in the Token.Register this is were the framework exception is coming from.  

However I would still not expect the request stream to never return in this scenario
Comment 2 Rodrigo Kumpera 2016-06-03 18:20:54 UTC
Hey Marcos,

Can you take a look at this one?
Comment 3 Chris Myhill 2016-06-14 14:07:38 UTC
Hi,

Would it be possible to get an update to this?

Thanks
Comment 4 Chris Myhill 2016-06-22 08:08:43 UTC
Hi,

Would it be possible to get an update to this?

Thanks
Comment 5 Chris Myhill 2016-06-30 12:59:55 UTC
Still waiting to hear what you guys think.  Thanks
Comment 6 marcos.henrich 2016-06-30 13:02:53 UTC
Hi Chris,

Sorry for making you wait, I am finishing another fix and I will look into this next.
Comment 7 marcos.henrich 2016-07-07 15:57:55 UTC
Thanks for the bug report.

It appears that even .NET throws an WebException some times.
I wrote a repro that was causing ObjectDisposeException on mono and WebException on .NET.

https://gist.github.com/esdrubal/ba8e1e1708303fd1ca0e0078b64369da

Mono was fixed to also throw WebException in the following pull request.
https://github.com/mono/mono/pull/3248

Until the fix lans on stable I recommend to catch ObjectDisposedException and WebException, after that only WebException will be needed.
Comment 8 marcos.henrich 2016-07-13 16:49:38 UTC
Fixed in master 725892b35c29fe919d6e58305ac241f6cbd01018
https://github.com/mono/mono/commit/725892b35c29fe919d6e58305ac241f6cbd01018

Note You need to log in before you can comment on or make changes to this bug.