Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App crash due to NullReferenceException in NSUrlSessionHandlerDelegate.DidCompleteWithError #11799

Closed
adams-family opened this issue Jun 3, 2021 · 73 comments · Fixed by #20328
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS mobcat Issues affecting the mobile cat team networking If an issue or pull request is related to networking support The issue is related to support
Milestone

Comments

@adams-family
Copy link

IMPORTANT:

I think this problem will be somewhat related to: #9132

Steps to Reproduce

I happens on iOS, latest versions (latest iOS, latest Xamarin) very often in production (tens of crashes per day on a 2k user basis), randomly. It's hard to reproduce it in the debugger.

protected HttpClient client = new HttpClient();

void Test()
{
  try
  {
     var stringContent = new StringContent(json, UnicodeEncoding.UTF8, "application/json");
     stringContent.Headers.Add("sample", "value");
     var response = await client.PostAsync("https://....", stringContent);
  }
  catch (Exception e)
  {
     // it never hits this exception handler
  }
}

Expected Behavior

Thanks to the try/catch block this code should never cause an app crash.

Actual Behavior

The app crashes. Here is an AppCenter stacktrace:

NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error)

Environment

Microsoft Visual Studio Community 2019
Version 16.8.4
VisualStudio.16.Release/16.8.4+30907.101
Microsoft .NET Framework
Version 4.8.04084

Installed Version: Community

Visual C++ 2019   00435-60000-00000-AA591
Microsoft Visual C++ 2019

ASP.NET and Web Tools 2019   16.8.557.25636
ASP.NET and Web Tools 2019

Azure App Service Tools v3.0.0   16.8.557.25636
Azure App Service Tools v3.0.0

C# Tools   3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.6 (master@34d6af2)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

IncrediBuild Build Acceleration   1.5.0.13
IncrediBuild effectively reduces compilation and development times by up to 90%.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.8.43 (00471f8)
Support for debugging Mono processes with Visual Studio.

NuGet Package Manager   5.8.1
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

Qt Visual Studio Tools   2.7.1.18
The Qt Visual Studio Tools allow developers to use the standard development environment without having to worry about any Qt-related build steps or tools.

Syncfusion ASP.NET Core Project Templates   18.1.0.42
Provides project templates for creating an ASP.NET Core Web Application using Syncfusion ASP.NET Core components.

Syncfusion ASP.NET Core Project Templates   16.1.0.32
Provides project templates for creating an ASP.NET Core Web Application using Syncfusion ASP.NET Core components.

Syncfusion ASP.NET MVC Project Templates   17.1.0.32
Provides project templates for creating ASP.NET MVC Web Applications using Syncfusion components.

Syncfusion Xamarin Project Templates   13.4.0.53
A multi-project template for building iOS, Android, and Windows apps using Syncfusion Xamarin components.

Syncfusion Xamarin Toolbox   16.2.0.19
Syncfusion Essential Studio for Xamarin Toolbox Visual Studio extension.

SyncfusionMenu Extension   1.0
SyncfusionMenu Visual Studio Extension Detailed Info

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

Visual Basic Tools   3.8.0-5.20604.10+9ed4b774d20940880de8df1ca8b07508aa01c8cd
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools   16.8.0-beta.20507.4+da6be68280c89131cdba2045525b80890401defd
Microsoft Visual F# Tools

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.8.000.261 (d16-8@bb99248)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.8.0.507 (remotes/origin/d16-8@e87b24884)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.8.112 (86385a3)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   11.1.0.26 (d16-8/a36ce73)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: 5e9cb6d
    Java.Interop: xamarin/java.interop/d16-8@79d9533
    ProGuard: Guardsquare/proguard/proguard6.2.2@ebe9000
    SQLite: xamarin/sqlite/3.32.1@1a3276b
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-8@2fb1cbc


Xamarin.iOS and Xamarin.Mac SDK   14.8.0.3 (c51fabee8)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Example Project (If Possible)

I don't think it's possible to create one because I'd need to roll it out to at least a thousand users.

@spouliot spouliot added iOS Issues affecting iOS need-info Waiting for more information before the bug can be investigated labels Jun 3, 2021
@spouliot spouliot added this to the Future milestone Jun 3, 2021
@spouliot spouliot added the bug If an issue is a bug or a pull request a bug fix label Jun 3, 2021
@spouliot
Copy link
Contributor

spouliot commented Jun 3, 2021

I think this problem will be somewhat related to: #9132

c.c. @mandel-macaque

I don't think it's possible to create one because I'd need to roll it out to at least a thousand users.

Can you provide us with a complete, symbolicated crash report ?

Sadly the stack trace above is not telling us much about what's happening.

Thanks!

@mandel-macaque
Copy link
Member

mandel-macaque commented Jun 3, 2021

@spouliot @adams-family looking at the Xamarin.iOS version:

Xamarin.iOS and Xamarin.Mac SDK 14.8.0.3 (c51fabe)

That points to commit: c51fabee8 that commits is earlier than the fix provided in #10230

If you look at the source code of the commit of the version you are using

lock (sessionHandler.inflightRequestsLock)
you can see that the fix I provided is not there. It is the same bug, but you are in a much older version which means that you do not have the fix.

The current stable release is https://download.visualstudio.microsoft.com/download/pr/414d1675-ca0b-4b5b-8a4e-26b693883581/5e50228d1498caf85151b6d6cfec6fd9/xamarin.ios-14.20.0.1.pkg

Please, make sure that you are running the latests stable and then we can re-visit the issue.

(Edit: updated pkg to d16-10 which also has the fix)

@spouliot spouliot closed this as completed Jun 3, 2021
@spouliot spouliot added support The issue is related to support and removed need-info Waiting for more information before the bug can be investigated labels Jun 3, 2021
@adams-family
Copy link
Author

Hi @mandel-macaque

I double-checked and the machine used to build the iOS version before releasing to the AppStore was this one: Xamarin.iOS 14.14.2.5.

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

Version 8.9.1 (build 34)
Installation UUID: 7d21187b-92bc-43a9-ab04-339062ddf7e1
	GTK+ 2.24.23 (Raleigh theme)
	Xamarin.Mac 6.18.0.23 (d16-6 / 088c73638)

	Package version: 612000122

=== Mono Framework MDK ===

Runtime:
	Mono 6.12.0.122 (2020-02/c621c35ffa0) (64-bit)
	Package version: 612000122

=== Roslyn (Language Service) ===

3.9.0-6.21152.10+c10f884b30737542ddd84ca889a4aad9281ce210

=== NuGet ===

Version: 5.8.0.6860

=== .NET Core SDK ===

SDK: /usr/local/share/dotnet/sdk/5.0.201/Sdks
SDK Versions:
	5.0.201
	3.1.407
MSBuild SDKs: /Applications/Visual Studio.app/Contents/Resources/lib/monodevelop/bin/MSBuild/Current/bin/Sdks

=== .NET Core Runtime ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
	5.0.4
	3.1.13

=== .NET Core 3.1 SDK ===

SDK: 3.1.407

=== Xamarin.Profiler ===

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

=== Updater ===

Version: 11

=== Apple Developer Tools ===

Xcode 12.5 (18205)
Build 12E262

=== Xamarin.Mac ===

Xamarin.Mac not installed. Can't find /Library/Frameworks/Xamarin.Mac.framework/Versions/Current/Version.

=== Xamarin.iOS ===

Version: 14.14.2.5 (Visual Studio Community)
Hash: 3836759d4
Branch: d16-9
Build date: 2021-02-10 17:56:44-0500

=== Xamarin Designer ===

Version: 16.9.0.316
Hash: bd2705417
Branch: remotes/origin/d16-9
Build date: 2021-02-24 00:16:08 UTC

=== Xamarin.Android ===

Version: 11.2.0.21 (Visual Studio Community)
Commit: xamarin-android/d16-9/93eab59
Android SDK: /Users/adam/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		None installed

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 30.0.4
SDK Build Tools Version: 30.0.2

Build Information: 
Mono: 5e9cb6d
Java.Interop: xamarin/java.interop/d16-9@d6d86b2
ProGuard: Guardsquare/proguard/v7.0.1@912d149
SQLite: xamarin/sqlite/3.34.1@daff8f4
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-9@9d8924d

=== Microsoft OpenJDK for Mobile ===

Java SDK: /Users/adam/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.9.0.22
Hash: a391de2
Branch: remotes/origin/d16-9
Build date: 2021-02-18 03:14:56 UTC

=== Android Device Manager ===

Version: 16.9.0.17
Hash: fc2b3db
Branch: remotes/origin/d16-9
Build date: 2021-02-18 03:15:18 UTC

=== Build Information ===

Release ID: 809010034
Git revision: 33bce2b7f3d45d05865cd3282f1c2cdc3a112aa0
Build date: 2021-03-04 11:21:38-05
Build branch: release-8.9
Xamarin extensions: 33bce2b7f3d45d05865cd3282f1c2cdc3a112aa0

=== Operating System ===

Mac OS X 10.16.0
Darwin 20.3.0 Darwin Kernel Version 20.3.0
    Thu Jan 21 00:07:06 PST 2021
    root:xnu-7195.81.3~1/RELEASE_X86_64 x86_64

@adams-family
Copy link
Author

adams-family commented Jun 4, 2021

@spouliot - I have exported a crashreport from MS AppCenter, symbols were uploaded earlier therefore I hope it's useful to you:

Incident Identifier: d4c93912-92f7-4860-8988-***********
CrashReporter Key:   AB6B1072-8B29-4B32-8D24-***********
Hardware Model:      iPhone12,1
Process:         application.iOS [743]
Path:            /private/var/containers/Bundle/Application/B99CA9E0-70E0-4203-9EA3-7EB8B6B892B1/application/application.iOS
Identifier:      *****
Version:         1.1.6 (184)
Code Type:       arm64
Parent Process:   [1]

Date/Time:       2021-06-03T10:08:06.999Z
Launch Time:     2021-06-01T03:28:08Z
OS Version:      iPhone OS 14.4.2 (18D70)
Report Version:  104

Exception Type:  SIGABRT
Exception Codes: #0 at 0x1b4f51414
Crashed Thread:  20

Application Specific Information:
*** Terminating app due to uncaught exception 'SIGABRT', reason: 'Object reference not set to an instance of an object'

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) <0x105cc29f0 + 0x00074> in <ad1be307855442e4b2cb88e33f94f908#8bd8601acf85cfa73490ae975776a7b0>:0

Thread 20 Crashed:
0   libsystem_kernel.dylib               0x00000001b4f51414 __pthread_kill + 8
1   libsystem_c.dylib                    0x000000019042fb74 abort + 100
2   application.iOS                      0x000000010671f9f8 0x104d98000 + 26769912
3   application.iOS                      0x00000001065e494c 0x104d98000 + 25479500
4   application.iOS                      0x0000000106579718 0x104d98000 + 25040664
5   application.iOS                      0x0000000106578100 0x104d98000 + 25035008
6   application.iOS                      0x000000010656e404 0x104d98000 + 24994820

Thread 0:
0   application.iOS                      0x00000001051b7fe0 0x104d98000 + 4325344
1   application.iOS                      0x00000001050353d4 0x104d98000 + 2741204
2   application.iOS                      0x0000000104edd180 0x104d98000 + 1331584
3   application.iOS                      0x0000000104edaa74 0x104d98000 + 1321588
4   application.iOS                      0x0000000104f8fd24 0x104d98000 + 2063652
5   application.iOS                      0x0000000104eb1fd0 0x104d98000 + 1155024
6   application.iOS                      0x0000000104eb1e70 0x104d98000 + 1154672
7   application.iOS                      0x0000000104f85b5c 0x104d98000 + 2022236
8   application.iOS                      0x0000000104f85be4 0x104d98000 + 2022372
9   application.iOS                      0x000000010501388c 0x104d98000 + 2603148
10  application.iOS                      0x0000000105013454 0x104d98000 + 2602068
11  application.iOS                      0x0000000105012f8c 0x104d98000 + 2600844
12  application.iOS                      0x0000000105013afc 0x104d98000 + 2603772
13  application.iOS                      0x0000000105013bd4 0x104d98000 + 2603988
14  application.iOS                      0x0000000104ed65b4 0x104d98000 + 1303988
15  application.iOS                      0x0000000104ebdfb4 0x104d98000 + 1204148
16  application.iOS                      0x0000000104ebe34c 0x104d98000 + 1205068
17  application.iOS                      0x0000000104ebe16c 0x104d98000 + 1204588
18  application.iOS                      0x00000001060a3b98 0x104d98000 + 19970968
19  application.iOS                      0x0000000104f6e7fc 0x104d98000 + 1927164
20  application.iOS                      0x0000000104ef670c 0x104d98000 + 1435404
21  application.iOS                      0x0000000104ef650c 0x104d98000 + 1434892
22  application.iOS                      0x0000000104f6e6b8 0x104d98000 + 1926840
23  application.iOS                      0x0000000104f12e98 0x104d98000 + 1552024
24  application.iOS                      0x0000000105d3d78c 0x104d98000 + 16406412
25  application.iOS                      0x0000000105178a00 0x104d98000 + 4065792
26  application.iOS                      0x000000010658b33c 0x104d98000 + 25113404
27  application.iOS                      0x0000000106646a60 0x104d98000 + 25881184
28  application.iOS                      0x000000010664a2c4 0x104d98000 + 25895620
29  application.iOS                      0x0000000104dd6020 0x104d98000 + 253984
30  application.iOS                      0x0000000104dd8040 0x104d98000 + 262208
31  Foundation                           0x0000000188397b90 __NSThreadPerformPerform + 184
32  CoreFoundation                       0x0000000186f7d76c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24
33  CoreFoundation                       0x0000000186f7d668 __CFRunLoopDoSource0 + 204
34  CoreFoundation                       0x0000000186f7c960 __CFRunLoopDoSources0 + 264
35  CoreFoundation                       0x0000000186f76a8c __CFRunLoopRun + 820
36  CoreFoundation                       0x0000000186f7621c CFRunLoopRunSpecific + 596
37  GraphicsServices                     0x000000019eb40784 GSEventRunModal + 160
38  UIKitCore                            0x00000001899b6ee8 -[UIApplication _run] + 1068
39  UIKitCore                            0x00000001899bc75c UIApplicationMain + 164
40  application.iOS                      0x0000000105d950d8 0x104d98000 + 16765144
41  application.iOS                      0x0000000105cf1ee0 0x104d98000 + 16096992
42  application.iOS                      0x0000000105cf1e64 0x104d98000 + 16096868
43  application.iOS                      0x0000000104dff744 0x104d98000 + 423748
44  application.iOS                      0x0000000105178a00 0x104d98000 + 4065792
45  application.iOS                      0x000000010658b33c 0x104d98000 + 25113404
46  application.iOS                      0x0000000106646a60 0x104d98000 + 25881184
47  application.iOS                      0x000000010664ccb8 0x104d98000 + 25906360
48  application.iOS                      0x00000001065698d0 0x104d98000 + 24975568
49  application.iOS                      0x0000000106727488 0x104d98000 + 26801288
50  application.iOS                      0x0000000104dff650 0x104d98000 + 423504
51  libdyld.dylib                        0x0000000186c366b0 start + 0

Thread 1:
0   libsystem_kernel.dylib               0x00000001b4f511ac __psynch_cvwait + 8
1   application.iOS                      0x00000001066ea3d0 0x104d98000 + 26551248
2   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
3   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 2:
0   libsystem_kernel.dylib               0x00000001b4f2d30c semaphore_wait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 3:
0   libsystem_kernel.dylib               0x00000001b4f2d2d0 mach_msg_trap + 8
1   CoreFoundation                       0x0000000186f7cc30 __CFRunLoopServiceMachPort + 376
2   CoreFoundation                       0x0000000186f76c14 __CFRunLoopRun + 1212
3   CoreFoundation                       0x0000000186f7621c CFRunLoopRunSpecific + 596
4   Foundation                           0x0000000188225df0 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 228
5   Foundation                           0x0000000188225cbc -[NSRunLoop(NSRunLoop) runUntilDate:] + 88
6   UIKitCore                            0x0000000189a6ac48 -[UIEventFetcher threadMain] + 512
7   Foundation                           0x0000000188397a34 __NSThread__start__ + 860
8   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
9   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 4:
0   libsystem_kernel.dylib               0x00000001b4f511ac __psynch_cvwait + 8
1   application.iOS                      0x00000001066f9b60 0x104d98000 + 26614624
2   application.iOS                      0x00000001066a00c4 0x104d98000 + 26247364
3   application.iOS                      0x000000010669ff9c 0x104d98000 + 26247068
4   application.iOS                      0x00000001066a01c0 0x104d98000 + 26247616
5   application.iOS                      0x000000010668ef98 0x104d98000 + 26177432
6   application.iOS                      0x0000000106609334 0x104d98000 + 25629492
7   application.iOS                      0x0000000104eff558 0x104d98000 + 1471832
8   application.iOS                      0x0000000104eff0c4 0x104d98000 + 1470660
9   application.iOS                      0x0000000104efebcc 0x104d98000 + 1469388
10  application.iOS                      0x0000000104efeb7c 0x104d98000 + 1469308
11  application.iOS                      0x0000000104efe99c 0x104d98000 + 1468828
12  application.iOS                      0x0000000104efeae8 0x104d98000 + 1469160
13  application.iOS                      0x0000000104f01e14 0x104d98000 + 1482260
14  application.iOS                      0x0000000104ef8d2c 0x104d98000 + 1445164
15  application.iOS                      0x0000000104ef670c 0x104d98000 + 1435404
16  application.iOS                      0x0000000104ef650c 0x104d98000 + 1434892
17  application.iOS                      0x0000000104ef6490 0x104d98000 + 1434768
18  application.iOS                      0x0000000104ef8e94 0x104d98000 + 1445524
19  application.iOS                      0x0000000105178a00 0x104d98000 + 4065792
20  application.iOS                      0x000000010658b33c 0x104d98000 + 25113404
21  application.iOS                      0x0000000106646a60 0x104d98000 + 25881184
22  application.iOS                      0x000000010664c34c 0x104d98000 + 25903948
23  application.iOS                      0x0000000106694b44 0x104d98000 + 26200900
24  application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
25  libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
26  libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 5:
0   libsystem_kernel.dylib               0x00000001b4f2d2d0 mach_msg_trap + 8
1   CoreFoundation                       0x0000000186f7cc30 __CFRunLoopServiceMachPort + 376
2   CoreFoundation                       0x0000000186f76c14 __CFRunLoopRun + 1212
3   CoreFoundation                       0x0000000186f7621c CFRunLoopRunSpecific + 596
4   AudioSession                         0x000000018eabf0c4 GenericRunLoopThread::Entry(void*) + 160
5   AudioSession                         0x000000018eac125c CAPThread::Entry(CAPThread*) + 88
6   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
7   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 6:
0   libsystem_kernel.dylib               0x00000001b4f2d2d0 mach_msg_trap + 8
1   CoreFoundation                       0x0000000186f7cc30 __CFRunLoopServiceMachPort + 376
2   CoreFoundation                       0x0000000186f76c14 __CFRunLoopRun + 1212
3   CoreFoundation                       0x0000000186f7621c CFRunLoopRunSpecific + 596
4   CFNetwork                            0x000000018784c1c0 _CFURLStorageSessionCopyCache + 64448
5   Foundation                           0x0000000188397a34 __NSThread__start__ + 860
6   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
7   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 7:
0   libsystem_kernel.dylib               0x00000001b4f511ac __psynch_cvwait + 8
1   application.iOS                      0x00000001066f9b88 0x104d98000 + 26614664
2   application.iOS                      0x00000001066a00c4 0x104d98000 + 26247364
3   application.iOS                      0x000000010669ff9c 0x104d98000 + 26247068
4   application.iOS                      0x0000000106638db4 0x104d98000 + 25824692
5   application.iOS                      0x0000000106607918 0x104d98000 + 25622808
6   application.iOS                      0x0000000104ef8418 0x104d98000 + 1442840
7   application.iOS                      0x0000000104ef84d0 0x104d98000 + 1443024
8   application.iOS                      0x0000000104ef7fbc 0x104d98000 + 1441724
9   application.iOS                      0x0000000104ef8010 0x104d98000 + 1441808
10  application.iOS                      0x0000000104ef2ff8 0x104d98000 + 1421304
11  application.iOS                      0x0000000104ef2d5c 0x104d98000 + 1420636
12  application.iOS                      0x0000000105782750 0x104d98000 + 10397520
13  application.iOS                      0x0000000105782548 0x104d98000 + 10397000
14  application.iOS                      0x000000010578247c 0x104d98000 + 10396796
15  application.iOS                      0x0000000105f43734 0x104d98000 + 18528052
16  application.iOS                      0x0000000104ef8d2c 0x104d98000 + 1445164
17  application.iOS                      0x0000000104ef670c 0x104d98000 + 1435404
18  application.iOS                      0x0000000104ef650c 0x104d98000 + 1434892
19  application.iOS                      0x0000000104ef6490 0x104d98000 + 1434768
20  application.iOS                      0x0000000104ef8e94 0x104d98000 + 1445524
21  application.iOS                      0x0000000105178a00 0x104d98000 + 4065792
22  application.iOS                      0x000000010658b33c 0x104d98000 + 25113404
23  application.iOS                      0x0000000106646a60 0x104d98000 + 25881184
24  application.iOS                      0x000000010664c34c 0x104d98000 + 25903948
25  application.iOS                      0x0000000106694b44 0x104d98000 + 26200900
26  application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
27  libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
28  libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 8:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 9:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 10:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 11:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 12:
0   libsystem_kernel.dylib               0x00000001b4f532b4 kevent_qos + 8
1   libdispatch.dylib                    0x0000000186c15d4c _dispatch_kq_drain + 124
2   libdispatch.dylib                    0x0000000186c1578c _dispatch_event_loop_poke + 160
3   CFNetwork                            0x00000001876066dc 0x187600000 + 26332
4   CFNetwork                            0x000000018760507c 0x187600000 + 20604
5   CFNetwork                            0x000000018761e070 CFNetServiceBrowserSearchForServices + 76176
6   CFNetwork                            0x000000018761df30 CFNetServiceBrowserSearchForServices + 75856
7   CFNetwork                            0x0000000187601a88 0x187600000 + 6792
8   CFNetwork                            0x00000001878640bc __CFTubeSetTubeTypeNotifier + 59180
9   libdispatch.dylib                    0x0000000186bf424c _dispatch_call_block_and_release + 28
10  libdispatch.dylib                    0x0000000186bf5db0 _dispatch_client_callout + 16
11  libdispatch.dylib                    0x0000000186bfd10c _dispatch_lane_serial_drain + 576
12  libdispatch.dylib                    0x0000000186bfdc90 _dispatch_lane_invoke + 456
13  libdispatch.dylib                    0x0000000186c07d78 _dispatch_workloop_worker_thread + 704
14  libsystem_pthread.dylib              0x00000001d2aad814 _pthread_wqthread + 272
15  libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 13:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 14:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 15:
0   libsystem_kernel.dylib               0x00000001b4f511ac __psynch_cvwait + 8
1   application.iOS                      0x00000001066f9b60 0x104d98000 + 26614624
2   application.iOS                      0x0000000106702840 0x104d98000 + 26650688
3   application.iOS                      0x0000000106688c2c 0x104d98000 + 26151980
4   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
5   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
6   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
7   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 16:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 17:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 18:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 19:
0   libsystem_trace.dylib                0x000000019dddfa04 _os_log_impl_flatten_and_send + 1764
1   libnetwork.dylib                     0x0000000187ab2c88 nw_endpoint_flow_setup_protocols + 3172
2   libnetwork.dylib                     0x0000000187ab07cc nw_endpoint_flow_setup_channel + 2820
3   libnetwork.dylib                     0x0000000187aacc00 -[NWConcrete_nw_endpoint_flow updatePathWithHandler:] + 3120
4   libnetwork.dylib                     0x0000000187bafc50 nw_endpoint_handler_path_change + 14588
5   libnetwork.dylib                     0x0000000187baada0 __nw_endpoint_handler_initialize_association_block_invoke + 152
6   libnetwork.dylib                     0x0000000187d3788c __nw_association_update_paths_block_invoke.81 + 88
7   libnetwork.dylib                     0x0000000187ee1b78 nw_hash_table_apply + 452
8   libnetwork.dylib                     0x0000000187d371b0 nw_association_update_paths + 484
9   libnetwork.dylib                     0x000000018803c508 nw_path_necp_update_evaluator + 2080
10  libnetwork.dylib                     0x000000018803b9b8 nw_path_necp_check_for_updates + 1020
11  libnetwork.dylib                     0x0000000188051f18 nw_path_evaluator_force_update + 124
12  libnetwork.dylib                     0x0000000187d3f0b4 nw_association_force_update + 104
13  libnetwork.dylib                     0x0000000187ab1b9c nw_endpoint_flow_setup_channel + 7892
14  libnetwork.dylib                     0x0000000187abfe44 -[NWConcrete_nw_endpoint_flow startWithHandler:] + 3732
15  libnetwork.dylib                     0x0000000187bafc50 nw_endpoint_handler_path_change + 14588
16  libnetwork.dylib                     0x0000000187bb6e14 nw_endpoint_handler_start + 1172
17  libnetwork.dylib                     0x0000000187ee89b0 nw_endpoint_resolver_start_next_child + 4476
18  libdispatch.dylib                    0x0000000186bf424c _dispatch_call_block_and_release + 28
19  libdispatch.dylib                    0x0000000186bf5db0 _dispatch_client_callout + 16
20  libdispatch.dylib                    0x0000000186bfef90 _dispatch_workloop_invoke + 1888
21  libdispatch.dylib                    0x0000000186c07d78 _dispatch_workloop_worker_thread + 704
22  libsystem_pthread.dylib              0x00000001d2aad814 _pthread_wqthread + 272
23  libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 21:
0   libsystem_kernel.dylib               0x00000001b4f51814 __semwait_signal + 8
1   libsystem_c.dylib                    0x000000019042c5d4 usleep + 64
2   AVFCore                              0x000000018ebf419c -[AVFigRouteDiscovererOutputDeviceDiscoverySessionImpl _routePresentChanged] + 28
3   Foundation                           0x0000000188249490 -[__NSObserver _doit:] + 344
4   CoreFoundation                       0x0000000186f5e9a0 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 24
5   CoreFoundation                       0x0000000186f5e948 ___CFXRegistrationPost_block_invoke + 48
6   CoreFoundation                       0x0000000186f5deb0 _CFXRegistrationPost + 436
7   CoreFoundation                       0x0000000186f5d870 _CFXNotificationPost + 712
8   CoreFoundation                       0x0000000186f5e4a8 CFNotificationCenterPostNotificationWithOptions + 132
9   CoreMedia                            0x000000018fe86ad8 CMNotificationCenterPostNotification + 124
10  CoreMedia                            0x000000018ff4650c __figXPC_HandleNotificationMessage_block_invoke + 188
11  libdispatch.dylib                    0x0000000186bf424c _dispatch_call_block_and_release + 28
12  libdispatch.dylib                    0x0000000186bf5db0 _dispatch_client_callout + 16
13  libdispatch.dylib                    0x0000000186bfd10c _dispatch_lane_serial_drain + 576
14  libdispatch.dylib                    0x0000000186bfdc5c _dispatch_lane_invoke + 404
15  libdispatch.dylib                    0x0000000186c07d78 _dispatch_workloop_worker_thread + 704
16  libsystem_pthread.dylib              0x00000001d2aad814 _pthread_wqthread + 272
17  libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 22:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 23:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 24:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 25:
0   libsystem_kernel.dylib               0x00000001b4f52184 __workq_kernreturn + 8
1   libsystem_pthread.dylib              0x00000001d2ab476c start_wqthread + 4

Thread 26:
0   application.iOS                      0x00000001065637bc 0x104d98000 + 24950716
1   application.iOS                      0x000000010655e468 0x104d98000 + 24929384
2   application.iOS                      0x000000010655f5d0 0x104d98000 + 24933840
3   application.iOS                      0x000000010655ee0c 0x104d98000 + 24931852
4   application.iOS                      0x00000001065865fc 0x104d98000 + 25093628
5   application.iOS                      0x000000010658b050 0x104d98000 + 25112656
6   application.iOS                      0x000000010664837c 0x104d98000 + 25887612
7   application.iOS                      0x000000010664704c 0x104d98000 + 25882700
8   application.iOS                      0x0000000106586658 0x104d98000 + 25093720
9   application.iOS                      0x000000010658db30 0x104d98000 + 25123632
10  application.iOS                      0x000000010658d650 0x104d98000 + 25122384
11  application.iOS                      0x00000001051b602c 0x104d98000 + 4317228
12  application.iOS                      0x0000000104faf348 0x104d98000 + 2192200
13  application.iOS                      0x00000001060a3f70 0x104d98000 + 19971952
14  application.iOS                      0x0000000104f0c4e0 0x104d98000 + 1524960
15  application.iOS                      0x0000000104f0c1f4 0x104d98000 + 1524212
16  application.iOS                      0x0000000104f0c468 0x104d98000 + 1524840
17  application.iOS                      0x0000000104ef670c 0x104d98000 + 1435404
18  application.iOS                      0x0000000104ef650c 0x104d98000 + 1434892
19  application.iOS                      0x0000000104f10208 0x104d98000 + 1540616
20  application.iOS                      0x0000000104f0c380 0x104d98000 + 1524608
21  application.iOS                      0x0000000104f0c258 0x104d98000 + 1524312
22  application.iOS                      0x0000000104efb818 0x104d98000 + 1456152
23  application.iOS                      0x0000000105cc8ff0 0x104d98000 + 15929328
24  application.iOS                      0x0000000104efd8d8 0x104d98000 + 1464536
25  application.iOS                      0x0000000105178a00 0x104d98000 + 4065792
26  application.iOS                      0x000000010658b33c 0x104d98000 + 25113404
27  application.iOS                      0x000000010664837c 0x104d98000 + 25887612
28  application.iOS                      0x000000010668b9c8 0x104d98000 + 26163656
29  application.iOS                      0x00000001066892a4 0x104d98000 + 26153636
30  application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
31  application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
32  libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
33  libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 27:
0   libsystem_kernel.dylib               0x00000001b4f2d324 semaphore_timedwait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 28:
0   libsystem_kernel.dylib               0x00000001b4f2d324 semaphore_timedwait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 29:
0   libsystem_kernel.dylib               0x00000001b4f2d324 semaphore_timedwait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 30:
0   libsystem_kernel.dylib               0x00000001b4f2d324 semaphore_timedwait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 31:
0   libsystem_kernel.dylib               0x00000001b4f2d324 semaphore_timedwait_trap + 8
1   application.iOS                      0x0000000106694a4c 0x104d98000 + 26200652
2   application.iOS                      0x00000001066948d0 0x104d98000 + 26200272
3   libsystem_pthread.dylib              0x00000001d2aabcb0 _pthread_start + 316
4   libsystem_pthread.dylib              0x00000001d2ab4778 thread_start + 4

Thread 20 crashed with arm64 Thread State:
   x21: 0x000000016c4530e0     x2: 0x0000000000000000    x16: 0x0000000000000148     x3: 0x0000000000000000
   x22: 0x00000001082059e0     x4: 0x00000000000001b0    x17: 0x000000016c453000   cpsr: 0x0000000040000000
    x5: 0x00000001082ada10    x23: 0x0000000109826848     x6: 0x0000000000000021    x18: 0x0000000000000000
   x10: 0x00000000000bcfa0     lr: 0x00000001d2aacb50     x7: 0x000000028307dc70    x24: 0x000000010a41acc8
   x11: 0x0000000000003fff     x8: 0x00000000000005b9    x19: 0x0000000000000006    x25: 0x00000001084698f8
    x9: 0x0d05def64a16a7e2    x12: 0x0000000108200000     fp: 0x000000016c4511b0    x26: 0x000000010887b000
   x13: 0x00000000cb5b701f     pc: 0x00000001b4f51414    x27: 0x0000000000000002    x14: 0x00000000cb5b6ff8
   x20: 0x0000000000014627     x0: 0x0000000000000000     sp: 0x000000016c451190    x28: 0x00000001051789ff
   x15: 0x00000000000027aa     x1: 0x0000000000000000

Binary Images:
0x0000000104d98000 -        0x000000010683ffff +application.iOS arm64  <4ff40b39b7f93b72b2e36f63c52b1e81> /private/var/containers/Bundle/Application/B99CA9E0-70E0-4203-9EA3-7EB8B6B892B1/application.iOS.app/application.iOS
0x0000000186bf2000 -        0x0000000186c34fff  libdispatch.dylib arm64e  <b92757b104343e9ba3acb6da6b81945c> /usr/lib/system/libdispatch.dylib
0x0000000186c35000 -        0x0000000186c6ffff  libdyld.dylib arm64e  <21b19919133438bcb233896e929945e0> /usr/lib/system/libdyld.dylib
0x0000000186edc000 -        0x0000000187295fff  CoreFoundation arm64e  <727f2644eb4e3d57bc2ee6803ba92366> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
0x0000000187600000 -        0x0000000187a97fff  CFNetwork arm64e  <0ebb3119a8ce35da96245b5528802fc8> /System/Library/Frameworks/CFNetwork.framework/CFNetwork
0x0000000187a98000 -        0x00000001881a5fff  libnetwork.dylib arm64e  <39506b46189e3e828f32ab8326fcc2c7> /usr/lib/libnetwork.dylib
0x000000018821d000 -        0x00000001884d8fff  Foundation arm64e  <7698bf3e0cf631c085e9562714f01276> /System/Library/Frameworks/Foundation.framework/Foundation
0x0000000188dee000 -        0x000000018a29bfff  UIKitCore arm64e  <8518eae3832b3ff09fa59dbe3041f26c> /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
0x000000018eab2000 -        0x000000018eacafff  AudioSession arm64e  <ec9f661dcafc3a16b372bb4a803636e8> /System/Library/PrivateFrameworks/AudioSession.framework/AudioSession
0x000000018eacb000 -        0x000000018ec6bfff  AVFCore arm64e  <0294540d081033fdbad051943cb01d86> /System/Library/PrivateFrameworks/AVFCore.framework/AVFCore
0x000000018fe69000 -        0x000000018ff69fff  CoreMedia arm64e  <063231877fe13d2d9c430fdad845e776> /System/Library/Frameworks/CoreMedia.framework/CoreMedia
0x00000001903b9000 -        0x0000000190437fff  libsystem_c.dylib arm64e  <961a8fb0de8a3567b4375d99cd549fae> /usr/lib/system/libsystem_c.dylib
0x000000019ddd1000 -        0x000000019dde7fff  libsystem_trace.dylib arm64e  <3520924c65a7351dadbea07268a9bca7> /usr/lib/system/libsystem_trace.dylib
0x000000019eb3d000 -        0x000000019eb45fff  GraphicsServices arm64e  <1959c615fa0830de8461884a5bbc8037> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
0x00000001b4f29000 -        0x00000001b4f5afff  libsystem_kernel.dylib arm64e  <b373b97b671e301ca6b7c9f92e17dca0> /usr/lib/system/libsystem_kernel.dylib
0x00000001d2aaa000 -        0x00000001d2ab5fff  libsystem_pthread.dylib arm64e  <c939a2abd3b13b0a83c57ce2f4f339a9> /usr/lib/system/libsystem_pthread.dylib

@mandel-macaque
Copy link
Member

@adams-family so, from the version you have in the App Store, you have 14.14.2.5 with hash 3836759, looking in the repo, the code of that version points to https://github.com/xamarin/xamarin-macios/blob/3836759d4/src/Foundation/NSUrlSessionHandler.cs#L543 which does not have the fix. Please update and get back to us.

@adams-family
Copy link
Author

@mandel-macaque Got it! Updating and will push a new version to the AppStore. I should know in about it week if it helped.

@spouliot spouliot added the need-info Waiting for more information before the bug can be investigated label Jun 4, 2021
@spouliot spouliot reopened this Jun 4, 2021
@spouliot
Copy link
Contributor

spouliot commented Jun 4, 2021

re-opening issue so this gets reviewed in 30 days (if not earlier)
thanks!

@adams-family
Copy link
Author

@spouliot I might have some news, not necessarily good one...

I deployed version 1.1.8 of my app with the latest stable Xamarin.iOS framework:

Xamarin.iOS
Version: 14.20.0.1 (Visual Studio Community)
Hash: fe0e2c518
Branch: d16-10
Build date: 2021-05-19 08:15:56-0400

But still got the same exception from production (AppStore) which afterwards ended up with a crash:

image

Not sure if I can get you a more detailed report than this one:

image

BTW: Is there a way I can retrieve the Xamarin.iOS version (hash) during runtime? I would pass it to AppCenter crashlytics alongside with the app version. The only hackaround I found is this but I don't find it particularly reliable: https://stackoverflow.com/questions/54288683/how-to-programmatically-retrieve-the-version-information-of-visual-studio-for-ma

@spouliot
Copy link
Contributor

spouliot commented Jun 8, 2021

But still got the same exception from production (AppStore) which afterwards ended up with a crash:

@mandel-macaque ^

Is there a way I can retrieve the Xamarin.iOS version (hash) during runtime?

Not really - but you should not as knowing the version only gives you a very limited amount of information, which is better than nothing but we settle for less ;-)

I suggest you to generate msbuild binary logs (binlog files) when creating builds for submission. They are highly compressed, includes Xamarin.iOS version and a lot more very useful information. Keep the binlog along with your submitted .ipa and .dSYM.

That way you can read [1] it (or share it with us) in the future and figure out a lot more than just the SDK version.

[1] https://msbuildlog.com

@rolfbjarne
Copy link
Member

Is there a way I can retrieve the Xamarin.iOS version (hash) during runtime?

Not really - but you should not as knowing the version only gives you a very limited amount of information, which is better than nothing but we settle for less ;-)

Actually yes, we embed the Xamarin.iOS version (including hash) into the Info.plist when building the app.

This is from an app I just on my hard drive:

$ more bin/iPhone/Debug/testapp.app/Info.plist | grep com.xamarin.ios -A 4
	<key>com.xamarin.ios</key>
	<dict>
		<key>Version</key>
		<string>14.9.0.143 (main: 14ad1a122)</string>
	</dict>

I suggest you to generate msbuild binary logs (binlog files) when creating builds for submission.

I heartily second this, the binlogs contain a wealth of information and are very useful for us in diagnosing all sorts of problems.

@mandel-macaque
Copy link
Member

Lets try to see the version used and I'l dig deeper into the issue.

@adams-family
Copy link
Author

@rolfbjarne Thanks, the version number embedded in Info.plist is great, this way it can be forwarded to crash reporting 👍

@mandel-macaque Confirm that the version is 14.20.0.1 (d16-10: fe0e2c518) and my entire Info.plist is below:

image

@spouliot "I suggest you to generate msbuild binary logs (binlog files)" - I agree, I did some research, can you please confirm to me that the following way is the right way to build binary logs on VS for Mac:

msbuild -bl

@spouliot
Copy link
Contributor

spouliot commented Jun 8, 2021

to build binary logs on VS for Mac:

Not sure you can do them from VS for Mac. You normally want to be able to read build logs if you're in an IDE.

However you can build your .ipa from your solution on the command-line, e.g.

msbuild /t:Build /p:Platform=iPhone /p:Configuration=Release /bl:app.binlog app.sln

and you can use altool (from Apple) to automate the validation and submission of your .ipa to App Store Connect. Script it once and re-use multiple times :-)

@mandel-macaque
Copy link
Member

@adams-family it is indeed an issue since the changes in that hash are present:

if (sessionHandler.inflightRequests.TryGetValue (task, out inflight)) {

@adams-family
Copy link
Author

@mandel-macaque I just double checked that my app version 1.1.8 (188) was indeed built using Xamarin.iOS 14.20.0.1 (d16-10: fe0e2c518) - by going to Xcode -> Organizer -> find 1.1.8 (188) -> Show in Finder -> Show package contents -> Find Info.plist -> check com.xamarin.ios

The crash reason SIGABRT: Object reference not set to an instance of an object maybe signalizes another null reference problem?

@mandel-macaque
Copy link
Member

@adams-family yes, because this has not been that common until now, there might be changes in iOS that are generating a race condition. Do you know how to reproduce it? Can you show me how you instantiate the HttpClient?

@adams-family
Copy link
Author

@mandel-macaque Confirming that with the latest version of our app being automatically deployed by the AppStore the number of exceptions increases, so the problem still persists.

Sending samples of HttpClient usage below:

  1. We use new HttpClient instances in a background thread like this - with a GET request:
using System;
using System.Net.Http;
using System.Threading.Tasks;

namespace HttpTest
{
    class HttpTestPlain
    {
        protected HttpClient client = new HttpClient();

        void Test()
        {
            var url = "https://httpbin.org/get";     // we don't actually use this url - this is just an example

            Task.Run(async () =>
            {
                try
                {
                    using (var client = new HttpClient())
                    {
                        using (var response = await client.GetAsync(url))
                        {
                            Console.WriteLine("Response code is: " + response.StatusCode);
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            });
        }
    }
}
  1. We also use the Polly library for error ressiliance, also in a background thread asynchronously and then it looks like this - with a POST request:
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Polly;

namespace HttpTest
{
    class HttpTestWithPolly
    {
        protected HttpClient client = new HttpClient();

        void Test()
        {
            var url = "https://httpbin.org/post";     // we don't actually use this url - this is just an example

            Task.Run(async () =>
            {
                try
                {
                    var postData = new List<KeyValuePair<string, string>>();
                    postData.Add(new KeyValuePair<string, string>("v", "1"));

                    HttpContent content = new FormUrlEncodedContent(postData);

                    var result = await Policy
                        .Handle<HttpRequestException>(ex => !ex.Message.ToLower().Contains("404"))
                        .WaitAndRetryAsync
                        (
                            retryCount: 3,
                            sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
                            onRetry: (ex, time) =>
                            {
                                Console.WriteLine("Something went wrong: {ex.Message}");
                            }
                        )
                        .ExecuteAsync(async () =>
                        {
                            var response = await client.PostAsync(url, content);

                            Console.WriteLine("Response code is: " + response.StatusCode);
                            return true;
                        });
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            });
        }
    }
}

Unfortunately from the crash logs it's unclear which one causes the crash, however I can say that we do have more places using the Polly wrapper than those that don't.

@adams-family
Copy link
Author

@mandel-macaque Confirming that it's still happening on:

Version: 14.20.0.12 (Visual Studio Community)
Hash: bb11ec4c3
Branch: d16-10
Build date: 2021-06-09 10:47:24-0400

Stacktrace:

UNHANDLED (UnhandledException)
System.ObjectDisposedException: The CancellationTokenSource has been disposed.
 at System.Threading.CancellationTokenSource.Cancel (System.Boolean throwOnFirstException) <0x100acbba0 + 0x00038> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Threading.CancellationTokenSource.Cancel () <0x100acbb70 + 0x00017> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x10189f8a0 + 0x0007b> in <2e439eb70c714f5f9c50aa9db7939d35#3eb8ccfc4f6758322994486ee103c98d>:0  /   at System.Threading.CancellationTokenSource.Cancel (System.Boolean throwOnFirstException) <0x100acbba0 + 0x00038> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Threading.CancellationTokenSource.Cancel () <0x100acbb70 + 0x00017> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x10189f8a0 + 0x0007b> in <2e439eb70c714f5f9c50aa9db7939d35#3eb8ccfc4f6758322994486ee103c98d>:0  => System.ObjectDisposedException: The CancellationTokenSource has been disposed.
 at System.Threading.CancellationTokenSource.Cancel (System.Boolean throwOnFirstException) <0x100acbba0 + 0x00038> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Threading.CancellationTokenSource.Cancel () <0x100acbb70 + 0x00017> in <da8885cdf78b449d96de00cdb9d47225#3eb8ccfc4f6758322994486ee103c98d>:0 
 at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x10189f8a0 + 0x0007b> in <2e439eb70c714f5f9c50aa9db7939d35#3eb8ccfc4f6758322994486ee103c98d>:0

@adams-family
Copy link
Author

@mandel-macaque Just confirming that I received a crash report from Xamarin.iOS 14.20.0.24 (d16-10: c4b89cddb):

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) <0x10123b8a0 + 0x00074>
  in <16c89537d3e84c9193e31188658ef0ac#5b5c7dd2729acb21ba638e5f62afdc5f>:0
    /   at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) <0x10123b8a0 + 0x00074>
  in <16c89537d3e84c9193e31188658ef0ac#5b5c7dd2729acb21ba638e5f62afdc5f>: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) <0x10123b8a0 + 0x00074>
  in <16c89537d3e84c9193e31188658ef0ac#5b5c7dd2729acb21ba638e5f62afdc5f>:0

@mandel-macaque
Copy link
Member

Hmmm we might have an issue with Polly, I di don't thing about that, i'll take a look with that exact sample.

@SuperMario77
Copy link

SuperMario77 commented Aug 17, 2021

@mandel-macaque This issue is still a big problem in our app. In #9132 you said there was a fix made in branch d16-10.
One week ago we finally updated Xamarin.iOS to 14.20.0.24 in our app hoping the issue was fixed but I can confirm the issue still persists or partly persists. The exception in "DidCompleteWithError" still occurred with the latest Xamarin.iOS version.

We have installed this version of Xamarin.iOS in Visual Studio for Mac:

Xamarin.iOS
Version: 14.20.0.24 (Visual Studio Community)
Hash: c4b89cddb
Branch: d16-10
Build date: 2021-06-15 22:03:01-0400

This is a callstack from AppCenter of one of our users:

Date/Time:       2021-08-16T12:55:31.999Z
OS Version:      iPhone OS 14.7.1 (18G82)

Exception Type:  SIGABRT
Exception Codes: #0 at 0x1d3d2f334
Crashed Thread:  17

Application Specific Information:
*** Terminating app due to uncaught exception 'SIGABRT', reason: 'Object reference not set to an instance of an object'

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) [0x00023] in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:674

https://github.com/xamarin/xamarin-macios/blob/d16-10/src/Foundation/NSUrlSessionHandler.cs
The line 674 looks like this:

672:		if (error != null || serverError != null) {
673:			// got an error, cancel the stream operatios before we do anything
674:			inflight.CancellationTokenSource.Cancel ();       <------- crash line
675:			inflight.Errored = true;

So, I assume like in #9132 the CancellationTokenSource is null and therefore the crash. Why not check for null here?

BTW, why I can see the path of the developers workspace in official version 14.20.0.24? (/Users/builder/azdo/_work/....)

In the AppCenter callstacks of the older version of Xamarin.iOS 13.20.2.2 I saw the version string in the path:
NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error) /Library/Frameworks/Xamarin.iOS.framework/Versions/13.20.2.2/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:663

[UPDATE]
We don't use Polly in our app.

@hjharvey-MSFT
Copy link

@

@hjharvey-MSFT from a userbase of 800+ users, we got 9 crashes over a one week period of time. and this time, the crashes point to the same place, not different NSURLDelegate methods

Screenshot 2023-02-09 at 14 20 46

Will keep an eye out and come back at the end of next week with some updates. After that I will on holiday until the first week of March.

PS: You tagged the wrong Narcis person in your previous message :)

Is this an improvement over previous versions?

@narciszait
Copy link

@hjharvey-MSFT i would say yes - even through the beta phase we saw higher numbers and pointing to different NSURLSession methods.
If the time allows it, i will return with more numbers next week

@tipa
Copy link

tipa commented Feb 15, 2023

I am also seeing a ton of app crashes after migrating to .NET7 and doing concurrent network requests. Stack traces look a bit different (I am also using AppCenter, not Sentry). I also reported this here. Might be the same bug despite the different stack traces?
Has anyone figured out a way to reduce the impact of this? Perhaps running the network requests serially or something?

Edit: was able to extract this data out of one stack trace, maybe it contains and useful info:

Thread 6 name:  .NET ThreadPool Worker
Thread 6:
0   MyApp.iOS                     	       0x1044c783c mono_seq_point_iterator_next + 25327676 (seq-points-data.c:303)
1   MyApp.iOS                     	       0x1044c7ad0 mono_seq_point_find_prev_by_native_offset + 25328336 (seq-points-data.c:243)
2   MyApp.iOS                     	       0x10458a424 mono_walk_stack_full + 26125348 (mini-exceptions.c:0)
3   MyApp.iOS                     	       0x10458cda0 mono_handle_native_crash + 26135968 (mini-exceptions.c:3016)
4   MyApp.iOS                     	       0x10456a49c mono_sigsegv_signal_handler_debug + 25994396 (mini-runtime.c:0)
5   libsystem_platform.dylib      	       0x1f60aba90 _sigtramp + 56
6   MyApp.iOS                     	       0x1045722a4 mini_init_method_rgctx + 26026660 (jit-icalls.c:1681)
7   MyApp.iOS                     	       0x103dea86c corlib_System_Runtime_InteropServices_MemoryMarshal_TryGetArray_T_BYTE_System_ReadOnlyMemory_1_T_BYTE_System_ArraySegment_1_T_BYTE_ + 92
8   MyApp.iOS                     	       0x103024c88 wrapper_runtime_invoke_object_runtime_invoke_dynamic_intptr_intptr_intptr_intptr + 296
9   MyApp.iOS                     	       0x10456d558 mono_jit_runtime_invoke + 26006872 (mini-runtime.c:3525)
10  MyApp.iOS                     	       0x1044ac07c mono_runtime_invoke_checked + 25215100 (object.c:2583)
11  MyApp.iOS                     	       0x104571b6c mono_gsharedvt_constrained_call + 26024812 (jit-icalls.c:1477)
12  MyApp.iOS                     	       0x1030294c8 wrapper_managed_to_native_object___icall_wrapper_mono_gsharedvt_constrained_call_intptr_intptr_intptr_intptr_intptr + 152
13  MyApp.iOS                     	       0x102fe9674 System_Runtime_CompilerServices_AsyncMethodBuilderCore_Start_TStateMachine_GSHAREDVT_TStateMachine_GSHAREDVT_ + 372
14  MyApp.iOS                     	       0x103da2a30 corlib_System_IO_Stream__CopyToAsyncg__Core_27_0_System_IO_Stream_System_IO_Stream_int_System_Threading_CancellationToken + 172
15  MyApp.iOS                     	       0x102e26d78 System_Net_Http_HttpClient___SendAsyncg__Core_83_0d_MoveNext + 1600888 (/<unknown>:1)
16  MyApp.iOS                     	       0x1035a12c4 System_Net_Http_System_Runtime_CompilerServices_AsyncTaskMethodBuilder_1_AsyncStateMachineBox_1_System_Net_Http_HttpResponseMessage_System_Net_Http_HttpClient___SendAsyncg__Core_83_0d_ExecutionContextCallback_object + 56
17  MyApp.iOS                     	       0x103d40f58 corlib_System_Threading_Tasks_Task_RunContinuations_object + 276
18  MyApp.iOS                     	       0x103d40df4 corlib_System_Threading_Tasks_Task_FinishContinuations + 124
19  MyApp.iOS                     	       0x103344698 Microsoft_iOS_System_Runtime_CompilerServices_AsyncTaskMethodBuilder_1_AsyncStateMachineBox_1_System_Net_Http_HttpResponseMessage_System_Net_Http_NSUrlSessionHandler__SendAsyncd__58_ExecutionContextCallback_object + 56
20  MyApp.iOS                     	       0x103d40f58 corlib_System_Threading_Tasks_Task_RunContinuations_object + 276
21  MyApp.iOS                     	       0x103d40df4 corlib_System_Threading_Tasks_Task_FinishContinuations + 124
22  MyApp.iOS                     	       0x103d3f74c corlib_System_Threading_Tasks_Task_ExecuteEntryUnsafe_System_Threading_Thread + 164
23  MyApp.iOS                     	       0x103d14bc8 corlib_System_Threading_Thread_StartCallback + 256
24  MyApp.iOS                     	       0x10456d558 mono_jit_runtime_invoke + 26006872 (mini-runtime.c:3525)
25  MyApp.iOS                     	       0x1044ac07c mono_runtime_invoke_checked + 25215100 (object.c:2583)
26  MyApp.iOS                     	       0x1044c5bd0 start_wrapper_internal + 25320400 (threads.c:1215)
27  MyApp.iOS                     	       0x1044c5960 start_wrapper + 25319776 (threads.c:1266)
28  libsystem_pthread.dylib       	       0x1f61416cc _pthread_start + 148

@narciszait
Copy link

@hjharvey-MSFT having a look in Xcode for crashes, we got 2 reports for NSURLSessionDelegateWrapper, one with 9 crashes and the other one with23
Will have a look at our other tool and let you know.

@dalexsoto
Copy link
Member

Unfortunately with the current information we have there is really not a good idea on the why this is happening. We would need a way to reproduce this issue in order to continue investigating this.

@narciszait
Copy link

On one of the referenced issues that is closed, i commented about triggering this crash by hitting a breakpoint where i was making a network request and letting it stay on the breakpoint for some time.
When i resumed, it crashed - i suspect it was a timeout on the network call.
I can dig out the comment if you want

@narciszait
Copy link

I am doing some symbolication at the moment and i keep seeing this line:
System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate_DidCompleteWithError_Foundation_NSUrlSession_Foundation_NSUrlSessionTask_Foundation_NSError + 27411356 (NSUrlSessionHandler.cs:770)
if it is of any help

@hjharvey-MSFT
Copy link

@narciszait would you be able to provide a sample and instructions on how to repro the issue?

@narciszait
Copy link

@hjharvey-MSFT unfortunately no - i have no idea on how to reproduce it, hence i cannot make a sample.

Like i said, i managed once in a blue moon to get the crash when hitting a breakpoint where i was making a network request and letting it stay on the breakpoint for some time.
When i resumed, it crashed - i suspect it was a timeout on the network call.

@miszu
Copy link
Contributor

miszu commented Sep 15, 2023

We are seeing this crash from time to time as well, I think it happens in cases when the app was in the background for a long time.

Info.plist:

<key>com.microsoft.ios</key>
<dict>
<key>Version</key>
<string>16.4.7089+sha.663e05acb</string>
</dict>

Crash from AppCenter:

Application Specific Information:
*** Terminating app due to uncaught exception 'SIGABRT', reason: 'CancellationTokenSource_Disposed'

Xamarin Exception Stack:
System.ObjectDisposedException: CancellationTokenSource_Disposed
  at System.ThrowHelper.ThrowObjectDisposedException ()
  at System.Threading.CancellationTokenSource.ThrowIfDisposed ()
  at System.Threading.CancellationTokenSource.Cancel ()
  at System.Threading.CancellationTokenSource.Cancel ()
  at System.Net.Http.NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError ()

Thread 11 Crashed:
0   libsystem_kernel.dylib               0x00000002048f1578 __pthread_kill + 8
1   libsystem_c.dylib                    0x00000001cd75c178 abort + 176
2   <redacted_appname>               0x0000000102c8b3c8 xamarin_unhandled_exception_handler (runtime.m:1126)
3   <redacted_appname>               0x0000000102e64f44 mono_invoke_unhandled_exception_hook (exception.c:1219)
4    <redacted_appname>               0x0000000102f89128 mono_handle_exception_internal (mini-exceptions.c:2326)
5   <redacted_appname>               0x0000000102f87684 mono_handle_exception (mini-exceptions.c:2668)
6    <redacted_appname>               0x0000000102f9e1f0 mono_arm_throw_exception (exceptions-arm64.c:402)
7    <redacted_appname>               0x0000000102c6430c throw_exception + 168
8   <redacted_appname>               0x0000000102eb5384 mono_raise_exception (object.c:7409)
9    <redacted_appname>               0x0000000102c8b0dc xamarin_process_managed_exception (runtime.m:2360)
10   <redacted_appname>               0x00000001030607e0 native_to_managed_trampoline_11(objc_object*, objc_selector*, _MonoMethod**, objc_object*, objc_object*, objc_object*, unsigned int) (registrar.mm:736)
11  <redacted_appname>               0x000000010309cb70 -[System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate URLSession:task:didCompleteWithError:] (registrar.mm:25370)
12  CFNetwork                            0x00000001c725ad0c CFURLRequestCopyHTTPRequestMethod + 2268
13  libdispatch.dylib                    0x00000001cd6fa320 _dispatch_call_block_and_release + 28
14  libdispatch.dylib                    0x00000001cd6fbeac _dispatch_client_callout + 16
15  libdispatch.dylib                    0x00000001cd703534 _dispatch_lane_serial_drain + 664
16  libdispatch.dylib                    0x00000001cd7040d8 _dispatch_lane_invoke + 432
17  libdispatch.dylib                    0x00000001cd70ecdc _dispatch_workloop_worker_thread + 644
18  libsystem_pthread.dylib              0x00000002255f2ddc _pthread_wqthread + 284
19  libsystem_pthread.dylib              0x00000002255f2b7c start_wqthread + 4

@TimofeyBurak
Copy link

We are also seeing crash reports with the same stack trace in AppCenter:

CancellationTokenSource.ThrowObjectDisposedException ()
CancellationTokenSource.ThrowIfDisposed ()
CancellationTokenSource.Cancel ()
NSUrlSessionHandler+NSUrlSessionHandlerDelegate.DidCompleteWithError (Foundation.NSUrlSession session, Foundation.NSUrlSessionTask task, Foundation.NSError error)

This issue might be hard to reproduce because it requires very precise timings, but I have a theory about possible root cause:

https://github.com/xamarin/xamarin-macios/blob/net8.0/src/Foundation/NSUrlSessionHandler.cs#L930-L957

According to the source code of the DidCompleteWithError(...) method, there's a short period of time between inflight data being obtained (line 933) and cancellation being called (line 941). If the same inflight data is removed somewhere between those two points - it will be already disposed by the line 941, thus causing ObjectDisposedException being thrown.

Inflight data is removed whenever original managed request is cancelled:

https://github.com/xamarin/xamarin-macios/blob/net8.0/src/Foundation/NSUrlSessionHandler.cs#L527-L542

So, one of possible reproduction scenarios is following: original managed request is being cancelled shortly after DidCompleteWithError(...) callback is called.
@hjharvey-MSFT @dalexsoto can you check if this might be the cause of the issue, please?

@csnellemann
Copy link

@hjharvey-MSFT any update on this, given the hypothesis from @TimofeyBurak above?

@kmiterror
Copy link

This is still a thing, we are getting tons of those after updating to net7.
We changed default HttpClient implementation to use HttpClient (managed) handler, but it seems that one of our sub-dependencies doesn't respect that setting.

@tipa
Copy link

tipa commented Mar 11, 2024

I was able to reproduce the crash locally 4 times within just a couple of minutes using this example:
nsurlsessionhandler.zip

2024-03-11 12:14:44.122321+0100 nsurlsessionhandler[15973:365565] *** Terminating app due to uncaught exception 'System.ObjectDisposedException', reason: 'The CancellationTokenSource has been disposed. (System.ObjectDisposedException)
   at System.Threading.CancellationTokenSource.ThrowIfDisposed()
   at System.Threading.CancellationTokenSource.get_Token()
   at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.SetResponse(InflightData inflight) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 965
   at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 927
'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010ea86761 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x000000011ef2e904 objc_exception_throw + 48
	2   libxamarin-dotnet-debug.dylib       0x0000000109eb9016 xamarin_process_ma
*** Terminating app due to uncaught exception 'System.ObjectDisposedException', reason: 'The CancellationTokenSource has been disposed. (System.ObjectDisposedException)
   at System.Threading.CancellationTokenSource.ThrowIfDisposed()
   at System.Threading.CancellationTokenSource.get_Token()
   at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.SetResponse(InflightData inflight) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 965
   at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 927
'
*** First throw call stack:
(
	0   CoreFoundation                      0x000000010ea86761 __exceptionPreprocess + 242
	1   libobjc.A.dylib                     0x000000011ef2e904 objc_exception_throw + 48
	2   libxamarin-dotnet-debug.dylib       0x0000000109eb9016 xamarin_process_managed_exception + 934
	3   nsurlsessionhandler                 0x000000010014ed67 _ZL31native_to_managed_trampoline_59P11objc_objectP13objc_selectorPP11_MonoMethodS0_S0_S0_j + 1143
	4   nsurlsessionhandler                 0x0000000100176713 -[System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate URLSession:dataTask:didReceiveData:] + 67
	5   CFNetwork                           0x000000010bd4ea69 CFNetwork + 72297
	6   libdispatch.dylib                   0x000000011f39ca90 _dispatch_call_block_and_release + 12
	7   libdispatch.dylib                   0x000000011f39dd3a _dispatch_client_callout + 8
	8   libdispatch.dylib                   0x000000011f3a585c _dispatch_lane_serial_drain + 1236
	9   libdispatch.dylib                   0x000000011f3a63f7 _dispatch_lane_invoke + 406
	10  libdispatch.dylib                   0x000000011f3b1fac _dispatch_root_queue_drain_deferred_wlh + 276
	11  libdispatch.dylib                   0x000000011f3b1572 _dispatch_workloop_worker_thread + 552
	12  libsystem_pthread.dylib             0x0000000120341c47 _pthread_wqthread + 327
	13  libsystem_pthread.dylib             0x0000000120340b97 start_wqthread + 15

I also noticed several of those logs, which appear to be unrelated, but perhaps should be addressed as well:

2024-03-11 12:15:58.090531+0100 nsurlsessionhandler[16022:367058] [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000c05d20> (0x600000c05d20)
2024-03-11 12:15:58.092269+0100 nsurlsessionhandler[16022:367058] [API] API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

@rolfbjarne
Copy link
Member

I was able to reproduce the crash locally 4 times within just a couple of minutes using this example:

I can reproduce crashes too. I'll have a look.

@rolfbjarne
Copy link
Member

It seems this is the sequence of events for the ObjectDisposedException:

  1. The request is sent.
  2. The request is cancelled.
  3. The InflightData instance is removed here:
    RemoveInflightData (dataTask);

    3a. The InflightData is disposed:
    3a. The InflightData.CancellationTokenSource instance is disposed:
    CancellationTokenSource.Dispose ();
  4. Data comes in (in the DidReceiveData callback), and SetResponse is called:
    4a. The SetResponse method tries to use InflightData.CancellationTokenSource, and that throws an ObjectDisposedException:
    if (inflight.CancellationTokenSource.Token.IsCancellationRequested)

Full stack traces:

Inflight disposed:
   at System.Net.Http.NSUrlSessionHandler.InflightData.Dispose(Boolean disposing) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 1168
   at System.Net.Http.NSUrlSessionHandler.InflightData.Dispose() in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 1160
   at System.Net.Http.NSUrlSessionHandler.RemoveInflightData(NSUrlSessionTask task, Boolean cancel) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 224
   at System.Net.Http.NSUrlSessionHandler.<>c__DisplayClass58_0.<SendAsync>b__0() in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 545
   at System.Threading.CancellationToken.<>c.<Register>b__12_0(Object obj)
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
   at System.Threading.CancellationTokenSource.CallbackNode.<>c.<ExecuteCallback>b__9_0(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.CancellationTokenSource.CallbackNode.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.LinkedNCancellationTokenSource.<>c.<.cctor>b__4_0(Object s)
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
   at System.Threading.CancellationTokenSource.CallbackNode.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.TimerCallback(Object state)
   at System.Threading.TimerQueueTimer.CallCallback(Boolean isThreadPool)
   at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool)
   at System.Threading.TimerQueue.FireNextTimers()
   at System.Threading.TimerQueue.System.Threading.IThreadPoolWorkItem.Execute()
   at System.Threading.ThreadPoolWorkQueue.DispatchItemWithAutoreleasePool(Object workItem, Thread currentThread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()
ObjectDisposedException:
   *** Terminating app due to uncaught exception 'System.ObjectDisposedException', reason: 'The CancellationTokenSource has been disposed. (System.ObjectDisposedException)
      at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.SetResponse(InflightData inflight) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 970
      at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 932
   '
   *** First throw call stack:
   (
      0   CoreFoundation                      0x0000000186e72ccc __exceptionPreprocess + 176
      1   libobjc.A.dylib                     0x000000018695a788 objc_exception_throw + 60
      2   nsurlsessionhandler                 0x0000000106d4562c xamarin_process_managed_exception + 1052
      3   nsurlsessionhandler                 0x00000001071a26c4 _ZL31native_to_managed_trampoline_53P11objc_objectP13objc_selectorPP11_MonoMethodS0_S0_S0_j + 1028
      4   nsurlsessionhandler                 0x00000001071ca8e0 -[System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate URLSession:dataTask:didReceiveData:] + 68
      5   CFNetwork                           0x000000018c0fe28c CFURLCredentialStorageCopyAllCredentials + 61900
      6   libdispatch.dylib                   0x0000000186b6c750 _dispatch_call_block_and_release + 32
      7   libdispatch.dylib                   0x0000000186b6e3e8 _dispatch_client_callout + 20
      8   libdispatch.dylib                   0x0000000186b75a14 _dispatch_lane_serial_drain + 748
      9   libdispatch.dylib                   0x0000000186b76578 _dispatch_lane_invoke + 432
      10  libdispatch.dylib                   0x0000000186b812d0 _dispatch_root_queue_drain_deferred_wlh + 288
      11  libdispatch.dylib                   0x0000000186b80b44 _dispatch_workloop_worker_thread + 404
      12  libsystem_pthread.dylib             0x0000000186d1b00c _pthread_wqthread + 288
      13  libsystem_pthread.dylib             0x0000000186d19d28 start_wqthread + 8

@mandel-macaque it seems an easy fix would be to just not dispose the CancellationTokenSource, the GC will eventually do it.

Although MSDN says quite clearly:

Always call Dispose before you release your last reference to the CancellationTokenSource. Otherwise, the resources it is using will not be freed until the garbage collector calls the CancellationTokenSource object's Finalize method.

The amount of entry points into this code makes it quite complex to keep it
thread-safe, while at the same time not accidentally not run into deadlocks.
While disposing the CancellationTokenSource would be ideal, the additional
complexity this would entail makes me question whether it's worth it.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Mar 18, 2024
… NSUrlSessionHandler.

Fixes these warnings:

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x6000002cffa0> (0x6000002cffa0)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

which is printed numerous times with the test case here:

xamarin#11799 (comment)
@rolfbjarne
Copy link
Member

I also noticed several of those logs, which appear to be unrelated, but perhaps should be addressed as well:

2024-03-11 12:15:58.090531+0100 nsurlsessionhandler[16022:367058] [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000c05d20> (0x600000c05d20)
2024-03-11 12:15:58.092269+0100 nsurlsessionhandler[16022:367058] [API] API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

Fix in progress: #20326.

rolfbjarne added a commit to rolfbjarne/xamarin-macios that referenced this issue Mar 18, 2024
…nHandler's InflightData. Fixes xamarin#11799.

There's a random ObjectDisposedException occurring, which seems to have this sequence of events:

1. The request is sent.
2. The request is cancelled.
3. The InflightData instance is removed here: https://github.com/xamarin/xamarin-macios/blob/c3437328bf2c02d03a6d4dc4e752ef8ac927ca4e/src/Foundation/NSUrlSessionHandler.cs#L545
   3a. The InflightData is disposed: https://github.com/xamarin/xamarin-macios/blob/c3437328bf2c02d03a6d4dc4e752ef8ac927ca4e/src/Foundation/NSUrlSessionHandler.cs#L224
   3a. The InflightData.CancellationTokenSource instance is disposed: https://github.com/xamarin/xamarin-macios/blob/c3437328bf2c02d03a6d4dc4e752ef8ac927ca4e/src/Foundation/NSUrlSessionHandler.cs#L1168
4. Data comes in (in the DidReceiveData callback), and SetResponse is called: https://github.com/xamarin/xamarin-macios/blob/c3437328bf2c02d03a6d4dc4e752ef8ac927ca4e/src/Foundation/NSUrlSessionHandler.cs#L932
   4a. The SetResponse method tries to use InflightData.CancellationTokenSource, and that throws an ObjectDisposedException: https://github.com/xamarin/xamarin-macios/blob/c3437328bf2c02d03a6d4dc4e752ef8ac927ca4e/src/Foundation/NSUrlSessionHandler.cs#L970

Full stack traces:

```
InflightData disposed:
   at System.Net.Http.NSUrlSessionHandler.InflightData.Dispose(Boolean disposing) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 1168
   at System.Net.Http.NSUrlSessionHandler.InflightData.Dispose() in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 1160
   at System.Net.Http.NSUrlSessionHandler.RemoveInflightData(NSUrlSessionTask task, Boolean cancel) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 224
   at System.Net.Http.NSUrlSessionHandler.<>c__DisplayClass58_0.<SendAsync>b__0() in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 545
   at System.Threading.CancellationToken.<>c.<Register>b__12_0(Object obj)
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
   at System.Threading.CancellationTokenSource.CallbackNode.<>c.<ExecuteCallback>b__9_0(Object s)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.CancellationTokenSource.CallbackNode.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.LinkedNCancellationTokenSource.<>c.<.cctor>b__4_0(Object s)
   at System.Threading.CancellationTokenSource.Invoke(Delegate d, Object state, CancellationTokenSource source)
   at System.Threading.CancellationTokenSource.CallbackNode.ExecuteCallback()
   at System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.NotifyCancellation(Boolean throwOnFirstException)
   at System.Threading.CancellationTokenSource.TimerCallback(Object state)
   at System.Threading.TimerQueueTimer.CallCallback(Boolean isThreadPool)
   at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool)
   at System.Threading.TimerQueue.FireNextTimers()
   at System.Threading.TimerQueue.System.Threading.IThreadPoolWorkItem.Execute()
   at System.Threading.ThreadPoolWorkQueue.DispatchItemWithAutoreleasePool(Object workItem, Thread currentThread)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback()
```

```
ObjectDisposedException:
   *** Terminating app due to uncaught exception 'System.ObjectDisposedException', reason: 'The CancellationTokenSource has been disposed. (System.ObjectDisposedException)
      at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.SetResponse(InflightData inflight) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 970
      at System.Net.Http.NSUrlSessionHandler.NSUrlSessionHandlerDelegate.DidReceiveData(NSUrlSession session, NSUrlSessionDataTask dataTask, NSData data) in xamarin-macios/src/Foundation/NSUrlSessionHandler.cs:line 932
   '
   *** First throw call stack:
   (
      0   CoreFoundation                      0x0000000186e72ccc __exceptionPreprocess + 176
      1   libobjc.A.dylib                     0x000000018695a788 objc_exception_throw + 60
      2   nsurlsessionhandler                 0x0000000106d4562c xamarin_process_managed_exception + 1052
      3   nsurlsessionhandler                 0x00000001071a26c4 _ZL31native_to_managed_trampoline_53P11objc_objectP13objc_selectorPP11_MonoMethodS0_S0_S0_j + 1028
      4   nsurlsessionhandler                 0x00000001071ca8e0 -[System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate URLSession:dataTask:didReceiveData:] + 68
      5   CFNetwork                           0x000000018c0fe28c CFURLCredentialStorageCopyAllCredentials + 61900
      6   libdispatch.dylib                   0x0000000186b6c750 _dispatch_call_block_and_release + 32
      7   libdispatch.dylib                   0x0000000186b6e3e8 _dispatch_client_callout + 20
      8   libdispatch.dylib                   0x0000000186b75a14 _dispatch_lane_serial_drain + 748
      9   libdispatch.dylib                   0x0000000186b76578 _dispatch_lane_invoke + 432
      10  libdispatch.dylib                   0x0000000186b812d0 _dispatch_root_queue_drain_deferred_wlh + 288
      11  libdispatch.dylib                   0x0000000186b80b44 _dispatch_workloop_worker_thread + 404
      12  libsystem_pthread.dylib             0x0000000186d1b00c _pthread_wqthread + 288
      13  libsystem_pthread.dylib             0x0000000186d19d28 start_wqthread + 8
```

Disposing the CancellationTokenSource would be ideal, but the additional
complexity this would entail makes me question whether it's worth it: the
amount of entry points into this code makes it quite complex to keep track of
what the request has done, what it's doing and what's pending when things can
happen simultaneously on multiple threads at the same time, and keeping the
code thread-safe, and at the same time not accidentally not run into
deadlocks.

So it seems an easy fix would be to just not dispose the
CancellationTokenSource, the GC will eventually do it, even though MSDN
[says][1] quite clearly:

> Always call Dispose before you release your last reference to the CancellationTokenSource. Otherwise, the resources it is using will not be freed until the garbage collector calls the CancellationTokenSource object's Finalize method.

An additional note is that a network request's resource usage would already be
dwarfing any memory usage by the CancellationTokenSource, so it's unlikely
this will show up in any profiles.

So with this change we won't be disposing the CancellationTokenSource anymore.

Fixes xamarin#11799.

[1]: https://learn.microsoft.com/en-us/dotnet/api/system.threading.cancellationtokensource.dispose?view=net-8.0
@rolfbjarne rolfbjarne added the networking If an issue or pull request is related to networking label Mar 18, 2024
rolfbjarne added a commit that referenced this issue Mar 19, 2024
… NSUrlSessionHandler. (#20326)

Fixes these warnings:

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:didReceiveResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x600000676c60> (0x600000676c60)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

    [API] API MISUSE: NSURLSession delegate System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: <System_Net_Http_NSUrlSessionHandler_NSUrlSessionHandlerDelegate: 0x6000002cffa0> (0x6000002cffa0)
    [API] API MISUSE: dataTask:willCacheResponse:completionHandler: completion handler not called

which is printed numerous times with the test case here:

#11799 (comment)
@kmiterror
Copy link

@rolfbjarne
Is this going to be backported to dotnet8?

@rolfbjarne
Copy link
Member

@rolfbjarne Is this going to be backported to dotnet8?

The networking code is rather sensitive, in that any changes has a high potential for breaking someone. Coupled with the fact that these changes weren't entirely trivial, at the moment we're leaning towards not backporting them to .NET 8.

However, these changes are included in .NET 9 preview 3, so if you want to try the preview, and see if your issues are fixed, then that would be great (and a successful test might also make us reconsider backporting the fixes).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug If an issue is a bug or a pull request a bug fix iOS Issues affecting iOS mobcat Issues affecting the mobile cat team networking If an issue or pull request is related to networking support The issue is related to support
Projects
None yet