Bug 54806 - NullReferenceException at System.Threading.CancellationTokenSource.CreateLinkedTokenSource in Release build
Summary: NullReferenceException at System.Threading.CancellationTokenSource.CreateLink...
Status: CONFIRMED
Alias: None
Product: iOS
Classification: Xamarin
Component: Mono runtime / AOT compiler ()
Version: XI 10.0 (iOS10)
Hardware: All Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Zoltan Varga
URL:
Depends on:
Blocks:
 
Reported: 2017-04-07 21:15 UTC by Mus
Modified: 2018-01-16 19:56 UTC (History)
8 users (show)

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


Attachments
Sample to reproduce the bug (1.17 MB, application/x-zip-compressed)
2017-04-25 16:24 UTC, Mus
Details


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 for Bug 54806 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Mus 2017-04-07 21:15:29 UTC
### Description :
Getting a System.NullReferenceException when calling :
public Task<HttpResponseMessage> PostAsync (string requestUri, HttpContent content, CancellationToken cancellationToken); of System.Net.Http.

Only happens when building in Release mode.
We couldn’t produce a sample to reproduce yet.
see logs section below

Thanks



### Version 

Xamarin Studio Community
Version 6.3 (build 863)
Installation UUID: b003d992-91c8-4f81-b11b-313a0ab34f05
Runtime:
	Mono 4.8.0 (mono-4.8.0-branch/9d74414) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000524

NuGet
Version: 3.5.0.0

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

Xamarin Inspector
Not Installed

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin.iOS
Version: 10.8.0.175 (Xamarin Studio Community)
Hash: a04678c2
Branch: d15-1
Build date: 2017-03-28 14:05:38-0400

Xamarin.Android
Version: 7.2.0.7 (Xamarin Studio Community)
Android SDK: /Users/buildmac/Library/Android/sdk
	Supported Android versions:
		4.4    (API level 19)
		4.4.87 (API level 20)
		5.0    (API level 21)
		5.1    (API level 22)
		6.0    (API level 23)

SDK Tools Version: 25.1.2
SDK Platform Tools Version: 24.0.0
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.7.0_71"
Java(TM) SE Runtime Environment (build 1.7.0_71-b14)
Java HotSpot(TM) 64-Bit Server VM (build 24.71-b01, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin.Mac
Version: 3.2.0.175 (Xamarin Studio Community)

Build Information
Release ID: 603000863
Git revision: a2163670efe259c85cd8f335d95b175068fbbe2a
Build date: 2017-04-03 14:33:15-04
Xamarin addins: 2045d688ea1420e0381b473360ca62a763eb7d04
Build lane: monodevelop-lion-d15-1

Operating System
Mac OS X 10.11.6
Darwin IIT-A-005.local 15.6.0 Darwin Kernel Version 15.6.0
    Thu Jun 23 18:25:34 PDT 2016
    root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64






### Logs :
System.NullReferenceException: Object reference not set to an instance of an object
  at System.Threading.CancellationTokenSource.CreateLinkedTokenSource (System.Threading.CancellationToken token1, System.Threading.CancellationToken token2) <0x101232fd0 + 0x00058> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Net.Http.HttpClient+<SendAsyncWorker>c__async0.MoveNext () [0x00038] in <3afa4c02dd374bf7aa8b63dcf50f5aee>:0 
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () <0x1011de5ac + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
…
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () <0x1011de5ac + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  …
— End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () <0x1011de5ac + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  …
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () <0x1011de5ac + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  …
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter`1[TResult].GetResult () <0x1011de2a8 + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  …
--- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (System.Threading.Tasks.Task task) <0x1011de068 + 0x000d4> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (System.Threading.Tasks.Task task) <0x1011ddf90 + 0x000c7> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0 
  at System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1+ConfiguredTaskAwaiter[TResult].GetResult () <0x1011de5ac + 0x0003b> in <54e129cad3ee4c61a6b46c3680ddc69a#1d0cab0638faa997eddecce23a438777>:0
Comment 1 Manuel de la Peña [MSFT] 2017-04-10 10:48:21 UTC
Can you at least provide some sample code on how you are performing the post call? If you disable Linking in release mode, does the application work? Is this happening with the HttpClient? If that is the case, what handler are you using?
Comment 2 Mus 2017-04-25 16:21:43 UTC
Hi Manuel

Sorry for our late reply, we finally managed to provide a sample.

The problematic call can be found in WebApiService.cs class. GetMyIpAsync method causes the NullReferenceException, BUT removing the parameter “clientType” prevents this exception.

Problem happens only in Release (any Linker’s Behaviour) and only on a real device.

Note. a sample is attached to the Bug

Regards.
Comment 3 Mus 2017-04-25 16:24:36 UTC
Created attachment 21770 [details]
Sample to reproduce the bug

The problematic call can be found in WebApiService.cs class. GetMyIpAsync method causes the NullReferenceException, BUT removing the parameter “clientType” prevents this exception.

Problem happens only in Release (any Linker’s Behaviour) and only on a real device.
Comment 4 Jo 2017-05-04 20:06:39 UTC
I reproduced the issue with the provided sample in Release mode on a real device.

- Disabling the linker didn't fix the issue.
- Setting MtouchUseLllvm to false in the csproj file does fix the issue.

(I tried to re-enable LLVM optimizer and disable the "Thumb-2 instruction set to see if it has any impact, but as soon as I re-enable the LLVM optimization, the issue reappear)
Comment 5 Rolf Bjarne Kvinge [MSFT] 2017-05-05 08:00:25 UTC
@Manuel, can you have a look at this and confirm it?

@Jo/@Mus: do you know if this ever worked?
Comment 6 Victor 2017-05-08 20:00:11 UTC
Hello @Rolf,
We are using an older Xamarin version (see below) on one of our machine and we are able to reproduce this issue. 
We can't really tell if this ever worked because some code changes on our hand could have highlighted this exception. So it could have been there before but was not thrown at this point.

Version : 

Xamarin Studio Enterprise
Version 6.1.5 (build 0)
Installation UUID: 2fed080a-55bf-49f2-a150-bae6c390a189
Runtime:
	Mono 4.6.2 (mono-4.6.0-branch/ac9e222) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406020016

NuGet
Version: 3.4.3.0

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

Xamarin.Android
Version: 7.0.2.42 (Visual Studio Enterprise)
Android SDK: ../Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)
		6.0   (API level 23)

SDK Tools Version: 24.4.1
SDK Platform Tools Version: 23.0.1
SDK Build Tools Version: 23.0.1

Java SDK: /usr
java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin Android Player
Not Installed

Apple Developer Tools
Xcode 8.2.1 (11766.1)
Build 8C1002

Xamarin.Mac
Version: 2.10.0.120 (Visual Studio Enterprise)

Xamarin.iOS
Version: 10.3.1.8 (Visual Studio Enterprise)
Hash: 7beaef4
Branch: cycle8-xi
Build date: 2016-12-20 02:58:14-0500

Build Information
Release ID: 601050000
Git revision: 7494718e127af9eaec45a3bd6282d3da927488bd
Build date: 2017-01-17 10:31:01-05
Xamarin addins: c92d0626d347aaa02839689eaac2961d24c9f446
Build lane: monodevelop-lion-cycle8

Operating System
Mac OS X 10.12.4
Darwin xxxx.local 16.5.0 Darwin Kernel Version 16.5.0
    Fri Mar  3 16:52:33 PST 2017
    root:xnu-3789.51.2~3/RELEASE_X86_64 x86_64
Comment 7 Rolf Bjarne Kvinge [MSFT] 2017-05-09 06:19:21 UTC
@Victor, thanks.

@Manuel, can you have a look at the sample and try to confirm this?
Comment 8 Vincent Dondain [MSFT] 2017-06-17 00:04:30 UTC
Hi,

I tried to reproduce the bug with the following environment: https://gist.github.com/VincentDondain/23e041a6c4432ba4d540036864a2063d (alpha)

I'm getting this screen: https://www.dropbox.com/s/trbxz0j3bdcstzd/IMG_E0248.JPG?dl=0

No System.NullReferenceException at all when deploying on device in release mode.

However I do get the NullReferenceException when using Mono 4.8.1.0 and Xamarin.iOS 10.8.0.175.

I believe this is fixed upstream with the latest versions of the product.

@victor please confirm that it works with alpha, if it doesn't feel free to reopen the bug.

Thanks.
Comment 9 Victor 2017-12-14 19:54:08 UTC
Hi @Rofl, @Vincent,
We were indeed not able to reproduce using the environment Vincent mentioned in comment 8. But we now are able to reproduce it again using the provided sample and the following environment (latest VS for mac, Xamarin.iOS, etc...) :



=== Visual Studio Community 2017 for Mac ===

Version 7.3 (build 799)
Installation UUID: 4321eee9-e7d7-4b07-b1c1-7182ec9ebd8f
Runtime:
	Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 504010007

=== NuGet ===

Version: 4.3.1.4445

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	2.0.0
	1.1.1
	1.0.4
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Versions:
	2.0.0
	1.0.3
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

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

=== Apple Developer Tools ===

Xcode 9.2 (13772)
Build 9C40b

=== Xamarin.iOS ===

Version: 11.6.1.2 (Visual Studio Community)
Hash: 6857dfcc
Branch: xcode9.2
Build date: 2017-12-04 21:20:41-0500

=== Xamarin.Android ===

Version: 8.1.0.24 (Visual Studio Community)
Android SDK: /Users/buildmac/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.4   (API level 19)
		7.1   (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.5
SDK Build Tools Version: 25.0.3

Java SDK: /usr
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

=== Xamarin.Mac ===

Version: 4.0.0.214 (Visual Studio Community)

=== Xamarin Inspector ===

Version: 1.3.2
Hash: 461f09a
Branch: 1.3-release
Build date: Tue, 03 Oct 2017 18:26:57 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 703000799
Git revision: df590420ea5bc681994baefff2eabdaad889fba9
Build date: 2017-12-05 09:23:45-05
Xamarin addins: 51068d6eaf3b6fb4fbc72b33cb869b0ebff04ec5
Build lane: monodevelop-lion-d15-5

=== Operating System ===

Mac OS X 10.12.6
Darwin 16.7.0 Darwin Kernel Version 16.7.0
    Thu Jun 15 17:36:27 PDT 2017
    root:xnu-3789.70.16~2/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

NuGet Package Management Extensions 0.12.6
Internet of Things (IoT) development (Preview) 7.1


Thank you for your time.
Victor
Comment 10 Mus 2017-12-14 22:25:26 UTC
Hi, 
We still have the same issue could u please take care again of it

Thanks

Regards
Comment 11 Manuel de la Peña [MSFT] 2017-12-15 15:30:33 UTC
@Mus, can you please provide the environment you are using? The easies way would be using the About VS dialog and using the copy button.
Comment 12 Victor 2017-12-15 15:42:00 UTC
Hi @Manuel,
@Mus is using the same environment as I do, please see comment 9.
Thanks
Comment 13 Timothy Risi 2018-01-16 19:56:00 UTC
I'm seeing the Null Reference Exception testing on current stable:

Visual Studio Community 2017 for Mac
Version 7.3.3 (build 5)
Installation UUID: d2cfbc1e-4362-4817-b667-71b1b254b1fa
Runtime:
	Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 504010007

NuGet
Version: 4.3.1.4445

.NET Core
Runtime: /usr/local/share/dotnet/dotnet
Runtime Version: 2.0.0
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Version: 2.0.0
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks

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

Apple Developer Tools
Xcode 9.2 (13768)
Build 9C34b

Xamarin.iOS
Version: 11.6.1.3 (Visual Studio Community)
Hash: f70a1348
Branch: xcode9.2
Build date: 2017-12-18 14:47:16-0500

Xamarin.Android
Version: 8.1.3.0 (Visual Studio Community)
Android SDK: /Users/Tim/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 24.0.0
SDK Build Tools Version: 23.0.2

Java SDK: /usr
java version "1.8.0_151"
Java(TM) SE Runtime Environment (build 1.8.0_151-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.151-b12, mixed mode)

Android Designer EPL code available here:
https://github.com/xamarin/AndroidDesigner.EPL

Xamarin.Mac
Version: 4.0.0.215 (Visual Studio Community)

Xamarin Inspector
Version: 1.3.2
Hash: 461f09a
Branch: 1.3-release
Build date: Tue, 03 Oct 2017 18:26:57 GMT
Client compatibility: 1

Build Information
Release ID: 703030005
Git revision: b1c2982e201e71ef758866c9ade05f253a8c6f47
Build date: 2017-12-21 11:04:40-05
Xamarin addins: f397ddfbacfb39e60c9cc8d9e410f73faf8c2cbc
Build lane: monodevelop-lion-d15-5

Operating System
Mac OS X 10.13.2
Darwin 17.3.0 Darwin Kernel Version 17.3.0
    Thu Nov  9 18:09:22 PST 2017
    root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64

Enabled user installed addins
Internet of Things (IoT) development (Preview) 7.1