Bug 18846 - NSUrlSessionDataDelegate completionHandler callback throws ArgumentNullException
Summary: NSUrlSessionDataDelegate completionHandler callback throws ArgumentNullException
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: 7.2.1
Hardware: PC Mac OS
: Normal normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
: 20447 ()
Depends on:
Blocks:
 
Reported: 2014-04-07 18:33 UTC by John Miller [MSFT]
Modified: 2014-10-15 10:14 UTC (History)
5 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 John Miller [MSFT] 2014-04-07 18:33:06 UTC
From Case:
There appears to be a bug in the marshaling of URLSession:task:didReceiveChallenge:completionHandler where the completion handler can't be called with a null NSURLCredential, but that is what you are supposed to provide based on the Apple docs:
https://developer.apple.com/library/ios/technotes/tn2232/_index.html#//apple_ref/doc/uid/DTS40012884-CH1-SECNSURLSESSION

Example Code:
https://gist.github.com/therealjohn/10069333
Comment 1 Sebastien Pouliot 2014-04-08 08:14:18 UTC
An `ArgumentNullException` means it comes from managed code.

However your sample code is incomplete and we do not see what is `completionHandler`. Neither do we have the stack trace associated with the `ArgumentNullException`.

Please attach a complete, self-contained test case along with a full stack trace of the exception thrown.
Comment 2 John Miller [MSFT] 2014-04-08 15:04:16 UTC
StackTrace:
Unhandled Exception:
System.ArgumentNullException: Argument cannot be null.
Parameter name: arg2
  at MonoTouch.ObjCRuntime.Trampolines+NIDActionArity2V4.Invoke (NSUrlSessionAuthChallengeDisposition arg1, MonoTouch.Foundation.NSUrlCredential arg2) [0x00006] in /Developer/MonoTouch/Source/monotouch/src/build/compat/ObjCRuntime/Trampolines.g.cs:3883 
  at ModernHttpClient.NSUrlSessionHandler+DataTaskDelegate.DidReceiveChallenge (MonoTouch.Foundation.NSUrlSession session, MonoTouch.Foundation.NSUrlSessionTask task, MonoTouch.Foundation.NSUrlAuthenticationChallenge challenge, System.Action`2 completionHandler) [0x0003b] in /Users/John/Projects/modernhttpclient/src/ModernHttpClient.iOS/NSUrlSessionHandler.cs:115 
2014-04-08 12:02:28.604 PlaygroundiOS[4480:e03] Unhandled managed exception: Argument cannot be null.
Parameter name: arg2 (System.ArgumentNullException)
  at MonoTouch.ObjCRuntime.Trampolines+NIDActionArity2V4.Invoke (NSUrlSessionAuthChallengeDisposition arg1, MonoTouch.Foundation.NSUrlCredential arg2) [0x00006] in /Developer/MonoTouch/Source/monotouch/src/build/compat/ObjCRuntime/Trampolines.g.cs:3883 
  at ModernHttpClient.NSUrlSessionHandler+DataTaskDelegate.DidReceiveChallenge (MonoTouch.Foundation.NSUrlSession session, MonoTouch.Foundation.NSUrlSessionTask task, MonoTouch.Foundation.NSUrlAuthenticationChallenge challenge, System.Action`2 completionHandler) [0x0003b] in /Users/John/Projects/modernhttpclient/src/ModernHttpClient.iOS/NSUrlSessionHandler.cs:115 

Repro Project:

1. git clone http://github.com/paulcbetts/modernhttpclient
2. git checkout self-signed-cert-xam-bug
3. Run Playground.iOS
4. Hit "Download some stuff"
5. Exception
Comment 3 Sebastien Pouliot 2014-04-15 08:52:19 UTC
@John whenever you supply the requested information please switch the bug status from NEEDINFO to NEW (that was we'll spot it faster).

The inner exception is:

System.Runtime.CompilerServices.AsyncVoidMethodBuilder.AnonymousMethod__0 (l={System.ArgumentNullException: Argument cannot be null.
Parameter name: configuration
  at MonoTouch.Foundation.NSUrlSession.FromWeakConfiguration (MonoTouch.Foundation.NSUrlSessionConfiguration configuration, MonoTouch.Foundation.NSObject weakDelegate, MonoTouch.Foundation.NSOperationQueue delegateQueue) [0x00006] in /Developer/MonoTouch/Source/monotouch/src/build/compat/Foundation/NSUrlSession.g.cs:677 
  at MonoTouch.Foundation.NSUrlSession.FromConfiguration (MonoTouch.Foundation.NSUrlSessionConfiguration configuration, MonoTouch.Foundation.NSUrlSessionDelegate sessionDelegate, MonoTouch.Foundation.NSOperationQueue delegateQueue) [0x00000] in /Developer/MonoTouch/Source/monotouch/src/build/compat/Foundation/NSUrlSession.g.cs:669 
  at ModernHttpClient.NSUrlSessionHandler..ctor (CertificateVerification certVerification) [0x00026] in /Users/sebastienpouliot/git/modernhttpclient/src/ModernHttpClient.iOS/NSUrlSessionHandler.cs:46 
  at ModernHttpClient.NSUrlSessionHandler..ctor () [0x00000] in /Users/sebastienpouliot/git/modernhttpclient/src/ModernHttpClient.iOS/NSUrlSessionHandler.cs:41 
  at Playground.iOS.Playground_iOSViewController+<doIt>c__async0.MoveNext () [0x00029] in /Users/sebastienpouliot/git/modernhttpclient/src/Playground.iOS/Playground_iOSViewController.cs:44 }) in /Developer/MonoTouch/Source/mono/mcs/class/corlib/System.Runtime.CompilerServices/AsyncVoidMethodBuilder.cs:79


becasue `null` is given the `configuration` parameter of `sessionWithConfiguration:delegate:delegateQueue:`. Other parameters are documented [1] to accept `nil` but not this one. I'll do a test to confirm if it's ok (or not).

[1] https://developer.apple.com/library/ios/documentation/Foundation/Reference/NSURLSession_class/Introduction/Introduction.html#//apple_ref/occ/clm/NSURLSession/sessionWithConfiguration:delegate:delegateQueue:
Comment 4 Sebastien Pouliot 2014-04-15 08:59:50 UTC
Last comment (#3) is incorrect. The application options defaults to iOS 6.0 and uses some API only available to iOS 7 - that's why the configuration object was null. IOW that ANR is normal.
Comment 7 Miguel de Icaza [MSFT] 2014-04-15 10:36:13 UTC
Sebastien, the fix is correct.

This only means that if we get a null from unmanaged code, going back to managed, we would pass a null as a parameter to that function.

We have no way of expressing this differently with Actions<> at this point.   We could attempt to use a custom delegate, and look up the attributes from that one, but it would be a rare case where unmanaged code would pass a null without a reason, we would at worst pass a null, and then throw a NullReferenceException.

Let us apply the patch.
Comment 8 Sebastien Pouliot 2014-04-15 11:53:33 UTC
generator fixed in master 283991dd1892c2d626d380cda48bd62b3b66aa63

Will look at the crasher later...
Comment 9 Rolf Bjarne Kvinge [MSFT] 2014-04-22 10:48:29 UTC
This might be related to bug #17499.
Comment 10 Sebastien Pouliot 2014-06-08 12:26:54 UTC
*** Bug 20447 has been marked as a duplicate of this bug. ***
Comment 11 Sebastien Pouliot 2014-10-15 10:14:16 UTC
The main issue was fixed (comment #8) and I can't duplicate the crash from comment #6 anymore. Closing.