Skip to content

App crash due to NullReferenceException in NSUrlSessionHandlerDelegate.SetResponse #9132

Closed

Description

@StefAnglr

Steps to Reproduce

Unknown. It happened to me once while debugging, and we have several crash reports in App Center that look to be the same issue, but I haven't seen a consistent reproduction. This is in an iOS app running on iOS 13.

Expected Behavior

NSUrlSessionHandlerDelegate.SetResponse doesn't crash the app with a NullReferenceException

Actual Behavior

NSUrlSessionHandlerDelegate.SetResponse crashes the app with a NullReferenceException. inflight.CancellationTokenSource was null on this line:

if (inflight.CancellationTokenSource.Token.IsCancellationRequested)

Environment

=== Visual Studio Community 2019 for Mac ===

Version 8.6.5 (build 23)
Installation UUID: bca81352-ad6e-4861-a3e7-cec8b4172cb6
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 610000104

=== Mono Framework MDK ===

Runtime:
	Mono 6.10.0.104 (2019-12/5d03a6fe116) (64-bit)
	Package version: 610000104

=== Roslyn (Language Service) ===

3.6.0-3.20210.9+4eafdcb1bcbd8d3573f2ba6065e56d9b9ce4f8a3

=== NuGet ===

Version: 5.6.0.6591

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/3.1.301/Sdks
SDK Versions:
	3.1.301
	3.1.300
	3.1.201
	3.1.200
	3.1.102
	3.1.101
	3.1.100
	3.0.101
	3.0.100
	2.2.300
	2.2.103
	2.1.505
	2.1.500
	2.1.302
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.10.0/lib/mono/msbuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	3.1.5
	3.1.4
	3.1.3
	3.1.2
	3.1.1
	3.1.0
	3.0.1
	3.0.0
	2.2.5
	2.2.3
	2.2.1
	2.1.19
	2.1.18
	2.1.17
	2.1.16
	2.1.15
	2.1.14
	2.1.13
	2.1.9
	2.1.6
	2.1.2

=== Xamarin.Profiler ===

Version: 1.6.12.29
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 10.3.1.4 (Visual Studio Community)
Commit: xamarin-android/d16-6/3a10de9
Android SDK: /Users/stefanglr/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		6.0 (API level 23)
		7.1 (API level 25)
		8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.5
SDK Build Tools Version: 28.0.3

Build Information: 
Mono: 165f4b0
Java.Interop: xamarin/java.interop/d16-6@2cab35c
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.31.1@49232bc
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-6@bfb66f3

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/stefanglr/Library/Developer/Xamarin/jdk/microsoft_dist_openjdk_1.8.0.25
1.8.0-25
Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Android SDK Manager ===

Version: 16.6.0.50
Hash: 5901879
Branch: remotes/origin/d16-6
Build date: 2020-06-10 22:42:50 UTC

=== Android Device Manager ===

Version: 16.6.0.96
Hash: 6e8b80b
Branch: remotes/origin/d16-6
Build date: 2020-06-10 22:43:28 UTC

=== Apple Developer Tools ===

Xcode 11.5 (16139)
Build 11E608c

=== Xamarin.Mac ===

Version: 6.18.2.1 (Visual Studio Community)
Hash: 29c4ea731
Branch: d16-6
Build date: 2020-05-26 17:03:04-0400

=== Xamarin.iOS ===

Version: 13.18.2.1 (Visual Studio Community)
Hash: 29c4ea731
Branch: d16-6
Build date: 2020-05-26 17:03:05-0400

=== Xamarin Designer ===

Version: 16.6.0.329
Hash: d4f8bcd13
Branch: remotes/origin/d16-6
Build date: 2020-04-24 02:16:02 UTC

=== Build Information ===

Release ID: 806050023
Git revision: 5289d413b99fddfc20b4ecf3e445ccb822213427
Build date: 2020-06-18 12:08:30-04
Build branch: release-8.6
Xamarin extensions: 5289d413b99fddfc20b4ecf3e445ccb822213427

=== Operating System ===

Mac OS X 10.15.5
Darwin 19.5.0 Darwin Kernel Version 19.5.0
    Thu Apr 30 18:25:59 PDT 2020
    root:xnu-6153.121.1~7/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

VsVim 2.8.0.7

Activity

  1. added
    need-info
    on Jul 20, 2020
  2. added this to the Future milestone on Jul 20, 2020
  3. whitneyschmidt commented on Jul 20, 2020

    @whitneyschmidt
    Contributor

    Thank you for your feedback!

    For us to investigate this further, could you please provide your full build logs, crash reports (if any), and small sample project. The sample is the best way for us to investigate and reproduce the issue.

    To get full build logs just set the log verbosity to diagnostic at the following locations:

    • On Visual Studio for Mac: Preferences > Projects > Build
    • On Visual Studio for Windows: Tools > Options > Projects and Solutions > Build and Run

    We look forward to hearing from you!

  4. StefAnglr commented on Jul 22, 2020

    @StefAnglr
    Author

    It's not very reproducible, so I'm not sure if a sample project would do any good - I've only seen it in person once, which allowed me to narrow down the null reference. I can contrive a simple app that uses an HttpClient with a native handler to hit an API if it's necessary in order for the issue to be investigated.

    I've attached full build logs from a local build along with crash reports from our production app. I've included one report from another NSUrlSessionHandler NRE crash, this one in DidCompleteWithError, which also calls a method on inflight.CancellationTokenSource without null checking it first, so it could be the same root cause.

    NSUrlSessionHandlerCrash.zip

  5. whitneyschmidt commented on Jul 23, 2020

    @whitneyschmidt
    Contributor

    @StefAnglr Thanks for investigating and attaching the crash logs.

    A sample project replicating the conditions of the crash would be useful. Even if we can't reproduce, it'll at least give a better idea of what conditions make the crash possible.

    For visibility:

    Xamarin Exception Stack:
    System.NullReferenceException: Object reference not set to an instance of an object.
      at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x102e34eb0 + 0x0011c> in <75e8f2cafde249969958834208668188#bc0f3fa4a04040f4e9892c3a8822ddde>:0
    
  6. whitneyschmidt commented on Aug 18, 2020

    @whitneyschmidt
    Contributor

    We have not received the requested information.

    If you are still experiencing this issue please provide all the requested information then click the Reopen Issue button.

    Thanks!

  7. StefAnglr commented on Aug 21, 2020

    @StefAnglr
    Author

    Sorry for the delay on this. Attached is a simple one-screen iOS app that uses HttpClient in a similar manner to our application. We implement timeouts using our own CancellationTokenSource due to the native HttpClient implementations throwing different platform-specific exceptions on timeouts (mainly an issue on Android).

    In the meantime, we have copied the NSUrlSessionHandler code into our own application and added the exception handling ourselves to prevent the crashes from happening again. Once that build is released to the public we may have more useful logging from those handled exceptions.

    ReproIos.zip

  8. StefAnglr commented on Aug 21, 2020

    @StefAnglr
    Author

    I don't see a "Reopen Issue" button - I'm guessing I don't have the permissions for that.

  9. mandel-macaque commented on Aug 21, 2020

    @mandel-macaque
    Contributor

    @StefAnglr strange, you should have the rights to reopen. Anyway, I'll do it.

    Regarding the issue, it looks very interesting, I have a few questions about the repo application:

    1. I noticed that the url that you rest takes a long time to reply to allow you to cancel. The time it takes is smaller than 100 seconds which is the default setting of the httpclient. If the httpclient is the one that cancels the request, do you have any issues?
    2. The sample you provide. Does it reproduce the issue? If so, I'll take a look, this is clearly an issue on our side. At least per the code in the demo, there is nothing that is wrong in your code base.
    3. Can you please confirm that this is an issue with the current version in the preview channel?

    I will get to this ASAP, do please take into consideration that we are working on the Xcode 12 changes and I might take a little longer than I would like.

    PS: @whitneyschmidt I'll take over this.

  10. added
    networking
    on Aug 21, 2020
  11. StefAnglr commented on Aug 21, 2020

    @StefAnglr
    Author

    Thanks for the reply. I understand if this is low priority due to the low reproduction rate, and I think our workaround should prevent further crashes once released, but I appreciate whatever attention you can give it.

    1. In our application, we use our CancellationTokenSource approach to cancel any UI-blocking requests after 15 seconds, and any background requests after 60 seconds, so I don't think we ever allow the HttpClient to cancel the request itself. Our user reports suggest it is a background request that is causing the crash.

    2. The sample doesn't reproduce the issue, nor can we reliably reproduce the issue in our own app; the sample just provides an example of our approach to using the HttpClient. I have only seen it happen once while debugging. Normally I wouldn't open an issue for something I couldn't reproduce, but the debugger showed the NullReferenceException being thrown within NSUrlSessionHandlerDelegate.SetResponse on the CancellationTokenSource, and the crash reports we've received from production have shown the same, along with an ObjectDisposedException on the same line, so I thought the fix might be evident without the reproduction.

    3. This is happening in the Stable channel. If it were easier to reproduce I'd try with the Preview channel and see if it was still happening there.

  12. removed
    need-info
    on Sep 14, 2020
  13. 32 remaining items

  14. alexanderdibenedetto commented on May 10, 2021

    @alexanderdibenedetto

    Any update on when this fix will be released?

  15. WilliamWatterson86 commented on May 12, 2021

    @WilliamWatterson86

    Having issues here too - a number of crashes every day - would be great to have a fix ASAP

  16. adams-family commented on May 17, 2021

    @adams-family

    +1

    Having exactly the same issue, lots of crashes daily because of this error - this needs to be fixed.

  17. tipa commented on May 31, 2021

    @tipa

    Also seeing many NRE that are very likely to be caused by this issue.
    I don't understand why this issue has been closed even though it's not been released for quite a while now...

  18. mandel-macaque commented on Jun 3, 2021

    @mandel-macaque
    Contributor

    This issue is closed because the issue has been fixed and released. The only person that has provided a Xamarin.iOS version has been @IainS1986 with version 14.14.2.5, hash 3836759 which as you can see in the source code https://github.com/xamarin/xamarin-macios/blob/3836759d4/src/Foundation/NSUrlSessionHandler.cs#L543 does not have the fix.

    The d16-9 branch is not longer the stable branch, since we moved to the d16-10 branch. @mduchev @alexanderdibenedetto @tipa the latests release of Xamarin.iOS is https://download.visualstudio.microsoft.com/download/pr/414d1675-ca0b-4b5b-8a4e-26b693883581/5e50228d1498caf85151b6d6cfec6fd9/xamarin.ios-14.20.0.1.pkg

    Please test the stable version, if the issue reproduces, do not simple add a +1, that is not giving me enough information to do any work, even though you want to help it does the opposite :(

    If the above pkgs does not have the issue resolved, please provided the full environment information and the symbolicated exception.

    @adams-family as I have stated in your issue, the version you are using is 14.8.0.3 (c51fabe) which is much older than the fix (10th of December when the fix landed on the 22 second of March).

    @IainS1986 Please update to the provided pkg, I missed your question because closed issues are muted by default, please next time feel free to @ me directly in the issue so that I can responde faster, I take the blame for not looking at conversations in closed issues.

    (Edit: updated the pkg to d16-10 which is the new stable)

  19. adams-family commented on Jun 4, 2021

    @adams-family

    @mandel-macaque Thanks for your quick response, I've posted a comment in the issue I've created.

  20. benOnside commented on Dec 21, 2021

    @benOnside

    I'm having the same issue, Xamarin.iOS version 15.2. Only affects 1% of users per week - probably one in a hundred requests. I'll see if I can make a sample project after the new year

  21. ThomasHoest commented on Dec 21, 2021

    @ThomasHoest

    Also still seeing the issues in 15.x. It is not fixed. 1% seems about right looking at our crash numbers

  22. narciszait commented on Jan 11, 2022

    @narciszait

    Had this happen to me today
    Stack trace from Visual Studio, running on iOS Simulator 15.2

    Unhandled managed exception: Object reference not set to an instance of an object (System.NullReferenceException)
      at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.SetResponse (System.Net.Http.NSUrlSessionHandler+InflightData inflight) [0x00032] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:781 
      at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidReceiveData (Foundation.NSUrlSession session, Foundation.NSUrlSessionDataTask dataTask, Foundation.NSData data) [0x00018] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:743
    
  23. ThomasHoest commented on Jan 19, 2022

    @ThomasHoest

    Still our highest cause for crashes on iOS. About 250 per day. This should be re-opened.

  24. ThomasHoest commented on Feb 3, 2022

    @ThomasHoest

    Still here. @mandel-macaque what do you need to look into this again?

    Main Global terminating exception: Object reference not set to an instance of an object stack at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x101ffd2c0 + 0x00074> in <a7229a51752a4b23a2321c4878d035f4#b2bc9717d735d13fb9be7e6434b655da>:0 System.NullReferenceException Object reference not set to an instance of an object at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x101ffd2c0 + 0x00074> in <a7229a51752a4b23a2321c4878d035f4#b2bc9717d735d13fb9be7e6434b655da>:0 | System.NullReferenceException Object reference not set to an instance of an object at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x101ffd2c0 + 0x00074> in <a7229a51752a4b23a2321c4878d035f4#b2bc9717d735d13fb9be7e6434b655da>:0

  25. divil5000 commented on Feb 3, 2022

    @divil5000

    We are getting these too, out in the wild, a few per day. Sometimes an NRE, sometimes the below:

    System.ObjectDisposedException: The CancellationTokenSource has been disposed.
    at System.Threading.CancellationTokenSource.ThrowObjectDisposedException () <0x10089bfc8 + 0x0003c> in <25bf495f7d6b4944aa395b3ab5293479#9a38be3e91be429a650558472b7ee5fb>:0
    at System.Threading.CancellationTokenSource.ThrowIfDisposed () <0x10089bf98 + 0x0002f> in <25bf495f7d6b4944aa395b3ab5293479#9a38be3e91be429a650558472b7ee5fb>:0
    at System.Threading.CancellationTokenSource.Cancel () <0x10089baf4 + 0x00013> in <25bf495f7d6b4944aa395b3ab5293479#9a38be3e91be429a650558472b7ee5fb>:0
    at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession , Foundation.NSUrlSessionTask , Foundation.NSError ) <0x100ee0564 + 0x00077> in <5b08e01cb6df409eb2fea153e6177e5d#9a38be3e91be429a650558472b7ee5fb>:0

  26. stoff99 commented on Mar 31, 2022

    @stoff99

    Same here!
    We are also many crashes reported in MSAppcenter:
    NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error)

  27. narciszait commented on Apr 27, 2022

    @narciszait

    I got these 2 lines while i was investigating a breakpoint - i believe the call timeout:

    API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate:

    and

    API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

    Not sure if these 2 lines help

  28. ghost locked as resolved and limited conversation to collaborators on May 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

networking

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions