Bug 2483 - Calling CancelAsync when using DownloadDataAsync causes System.Threading.ThreadInterruptedException: Thread interrupted
Summary: Calling CancelAsync when using DownloadDataAsync causes System.Threading.Thr...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 5.0
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-12-13 13:07 UTC by Chris Hardy [MSFT]
Modified: 2012-08-20 15:44 UTC (History)
6 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 Chris Hardy [MSFT] 2011-12-13 13:07:18 UTC
When using DownloadDataAsync, calling CancelAsync causes the following exception:

Unhandled Exception: System.Threading.ThreadInterruptedException: Thread interrupted
  at (wrapper managed-to-native) System.Threading.WaitHandle:WaitOne_internal (System.Threading.WaitHandle,intptr,int,bool)
  at System.Threading.WaitHandle.WaitOne (Int32 millisecondsTimeout, Boolean exitContext) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/WaitHandle.cs:376 
  at System.Net.WebAsyncResult.WaitUntilComplete (Int32 timeout, Boolean exitContext) [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebAsyncResult.cs:164 
  at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnectionStream.cs:327 
  at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00102] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:888 
  at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x0000a] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:246 
[ERROR] FATAL UNHANDLED EXCEPTION: System.Threading.ThreadInterruptedException: Thread interrupted
  at (wrapper managed-to-native) System.Threading.WaitHandle:WaitOne_internal (System.Threading.WaitHandle,intptr,int,bool)
  at System.Threading.WaitHandle.WaitOne (Int32 millisecondsTimeout, Boolean exitContext) [0x00038] in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Threading/WaitHandle.cs:376 
  at System.Net.WebAsyncResult.WaitUntilComplete (Int32 timeout, Boolean exitContext) [0x0000d] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebAsyncResult.cs:164 
  at System.Net.WebConnectionStream.Read (System.Byte[] buffer, Int32 offset, Int32 size) [0x00018] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebConnectionStream.cs:327 
  at System.Net.WebClient.ReadAll (System.Net.WebRequest request, System.Object userToken) [0x00102] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:888 
  at System.Net.WebClient.DownloadDataCore (System.Uri address, System.Object userToken) [0x0000a] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.Net/WebClient.cs:246 

Here's a MonoTouch app that causes the problem,
https://gist.github.com/d4c87703d3453f086aa2
Comment 3 MartinG 2012-03-06 06:13:46 UTC
Problem is in the following code:

		public void DownloadDataAsync (Uri address, object userToken)
		{
...
					try {
						byte [] data = DownloadDataCore ((Uri) args [0], args [1]);
						OnDownloadDataCompleted (
							new DownloadDataCompletedEventArgs (data, null, false, args [1]));
					} catch (ThreadInterruptedException){
						OnDownloadDataCompleted (
							new DownloadDataCompletedEventArgs (null, null, true, args [1]));

/// Problem is throw statement here, which should not be
						throw;

/// 
					} catch (Exception e){
						OnDownloadDataCompleted (
							new DownloadDataCompletedEventArgs (null, e, false, args [1]));
					}
				});
...
Comment 4 Gonzalo Paniagua Javier 2012-03-06 14:40:19 UTC
Removing the rethrow will fix the problem, but it might leave the existing web request hanging there until the server closes the connection due to a timeout. As an exercise, try downloading a large file multiple concurrent times and, after canceling the requests, issue new ones.  My guess is that they will be stuck for a while.

As a quick workaround, we can remove that throw. A better option would be to use HttpWebRequest.(Begin|End) methods and HttpWebRequest.Abort to cancel the request.

The proper fix involves replacing Thread.Interrupt() with HttpWebRequest.Abort(). And, ideally, the *Async methods would really be async instead of the equivalent of new Thread { ...block here... }... But this might open Pandora's box of bugs.
Comment 5 Gonzalo Paniagua Javier 2012-03-06 20:59:00 UTC
Fixed in master/c10e388 and mono-2-10/b1994d8
Comment 6 Mike James 2012-08-20 12:10:39 UTC
I'm running Monotouch: 5.2.13 and still see this particular exception on calling webclient.canceldownload().

 I'm not sure what the target was for this bug fix, can you please let me know as I have project which relies on this function.
Comment 7 Miguel de Icaza [MSFT] 2012-08-20 15:44:32 UTC
MonoTouch 5.3.xx series.