Bug 23421 - App crashes intermittently when calling NSUrlSessionTask
Summary: App crashes intermittently when calling NSUrlSessionTask
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.2.0
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2014-09-26 15:12 UTC by Kent Green [MSFT]
Modified: 2014-09-29 12:52 UTC (History)
7 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 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:

Description Kent Green [MSFT] 2014-09-26 15:12:07 UTC
From priority case: 

One of our customers noted that their application sometimes crashes when there are multiple tasks created (about 25). 

The following error occurs:
Unhandled managed exception: Unable to cast object of type 'MonoTouch.Foundation.NSUrlSessionTask' (Objective-C type: '__NSCFBackgroundDownloadTask') to type 'MonoTouch.Foundation.NSUrlSessionDownloadTask'.
Additional information:
	Selector: URLSession:downloadTask:didFinishDownloadingToURL:
	Method: BackgroundServicePoc.Implementation.Synchronization.WebApiDownloadDelegate:DidFinishDownloading (MonoTouch.Foundation.NSUrlSession,MonoTouch.Foundation.NSUrlSessionDownloadTask,MonoTouch.Foundation.NSUrl)

---To reproduce the error---
1. Run the sample app
2. Click the button
3. Sometimes the app will start the downloading tasks and run normally, other times it will hang. You can see the progress of the app in the application output window. When it hangs, if you press the "continue execution button" the above noted exception is thrown.

(Note: Even though it doesn't occur 100% of the time, in my tests it still occurs *very* frequently, requiring only a couple attempts, (less than 5, usually 1 or 2) to reliably reproduce the crash.)

--Build information--
=== Xamarin Studio ===

Version 5.5 (build 198)
Installation UUID: 8ef63a7c-1b18-40de-a334-7f78777fcb55
	Mono 3.10.0 ((detached/ac51002)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000016

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 973f8d4
Build date: 2014-09-17 17:40:20-0400

=== Xamarin.Android ===

Version: (Business Edition)
Android SDK: /Users/kentgreen/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.1   (API level 7)
		2.2   (API level 8)
		2.3   (API level 10)
		3.1   (API level 12)
		4.0   (API level 14)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
Java SDK: /usr
java version "1.6.0_65"
Java(TM) SE Runtime Environment (build 1.6.0_65-b14-466.1-11M4716)
Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-466.1, mixed mode)

=== Xamarin.Mac ===

Version: (Business Edition)

=== Build Information ===

Release ID: 505000198
Git revision: 7495942eb76d6b80c460ddd61f2b94cba1a97fa2
Build date: 2014-09-18 09:50:12-04
Xamarin addins: c571b625445d60f2c8b189b309a6ffc87386caed

=== Operating System ===

Mac OS X 10.9.4
Darwin Kents-MacBook-Pro.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

---Additional Notes---
Since the sample is from a customer's case, I will reattach it momentarily as a private attachment.
Comment 3 Brendan Zagaeski (Xamarin Team, assistant) 2014-09-26 16:18:53 UTC
One additional tip for step 1:

1. Run the sample app *in the Debug configuration, with the Xamarin Studio or Visual Studio debugger attached*.

I found it easier to reproduce the problem when I had the debugger attached.

Running it on an iPad simulator (iOS 7 or iOS 8) works fine to reproduce the problem, but I was also able to reproduce on a physical iPhone (iOS 7.1.2) after modifying the storyboard to make the button visible on the smaller screen.
Comment 5 Rolf Bjarne Kvinge [MSFT] 2014-09-29 07:41:09 UTC

maccore/master: 3d306d698de9759c5530f514393f1e835c7d5b41
monotouch/master: 5a90f0e86b30974bae3f78fe386c53cbf1a8fb6a
Comment 6 Rolf Bjarne Kvinge [MSFT] 2014-09-29 11:36:07 UTC
Backported to 8.2

maccore/master: a6a6435976527f93311193616c2e059fdd75b008
monotouch/master: e934614f2e5a410acb3d62a2994c27a3e770a91e