xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.44k stars 508 forks source link

Notification Callback crashes because collection is not locked #6704

Closed SOFSPEEL closed 4 years ago

SOFSPEEL commented 5 years ago

Crash report from AppCenter is:

Application Specific Information:
*** Terminating app due to uncaught exception 'SIGABRT', reason: 'Collection was modified; enumeration operation may not execute.'

Xamarin Exception Stack:
System.InvalidOperationException: **Collection was modified**; enumeration operation may not execute.
  at System.Collections.Generic.Dictionary`2+ValueCollection+Enumerator[TKey,TValue].MoveNext () <0x101176360 + 0x0010c> in <4fffe1dab0494c34a2a213216932b318#a1561c6d5fc7357f1676f8d092d865e6>:0
  at System.Net.Http.NSUrlSessionHandler.BackgroundNotificationCb (Foundation.NSNotification obj) <0x101e1f270 + 0x00087> in <0da8ced3e38d42039920e99e08ca9321#a1561c6d5fc7357f1676f8d092d865e6>:0
  at Foundation.InternalNSNotificationHandler.Post (Foundation.NSNotification s) <0x1013bdec0 + 0x0002f> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) <0x10137d0c0 + 0x0002b> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10137d030 + 0x00043> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at ACS.FundRaising.Application.Main (System.String[] args) <0x100e47f20 + 0x0004b> in <1463607bddc044fcbe415fbcafd2d18a#a1561c6d5fc7357f1676f8d092d865e6>:0

Note that I suspect the crash occurs because the following code in NsUrlSessionHandler is not locked:

https://github.com/xamarin/xamarin-macios/blob/master/src/Foundation/NSUrlSessionHandler.cs

So this:

void BackgroundNotificationCb (NSNotification obj)
{
    // we do not need to call the lock, we call cancel on the source, that will trigger all the needed code to 
    // clean the resources and such
    foreach (var r in inflightRequests.Values) {
        r.CompletionSource.TrySetCanceled ();
    }
}

Should be changed to (I think):

void BackgroundNotificationCb (NSNotification obj)
{
    lock (inflightRequestsLock) {
        foreach (var r in inflightRequests.Values) {
            r.CompletionSource.TrySetCanceled ();
        }
    }
}
OS Version:      iPhone OS 12.3.1 (16F203)
Report Version:  104

Exception Type:  SIGABRT
Exception Codes: #0 at 0x1879ae0dc
Crashed Thread:  0

Application Specific Information:
*** Terminating app due to uncaught exception 'SIGABRT', reason: 'Collection was modified; enumeration operation may not execute.'

Xamarin Exception Stack:
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
  at System.Collections.Generic.Dictionary`2+ValueCollection+Enumerator[TKey,TValue].MoveNext () <0x101176360 + 0x0010c> in <4fffe1dab0494c34a2a213216932b318#a1561c6d5fc7357f1676f8d092d865e6>:0
  at System.Net.Http.NSUrlSessionHandler.BackgroundNotificationCb (Foundation.NSNotification obj) <0x101e1f270 + 0x00087> in <0da8ced3e38d42039920e99e08ca9321#a1561c6d5fc7357f1676f8d092d865e6>:0
  at Foundation.InternalNSNotificationHandler.Post (Foundation.NSNotification s) <0x1013bdec0 + 0x0002f> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) <0x10137d0c0 + 0x0002b> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) <0x10137d030 + 0x00043> in <b28d782d99614fe0b872da2da418b7b8#a1561c6d5fc7357f1676f8d092d865e6>:0
  at ACS.FundRaising.Application.Main (System.String[] args) <0x100e47f20 + 0x0004b> in <1463607bddc044fcbe415fbcafd2d18a#a1561c6d5fc7357f1676f8d092d865e6>:0

Steps to Reproduce

  1. Have not been able to reproduce but our AppCenter crash reports relatively high incidence of the above issue.

Expected Behavior

Actual Behavior

Environment

=== Visual Studio Enterprise 2019 for Mac ===

Version 8.2.1 (build 19)
Installation UUID: 10188454-d1d9-4857-a988-1a86cdc38cea
    GTK+ 2.24.23 (Raleigh theme)
    Xamarin.Mac 5.14.0.85 (d16-2-fix-mac-updates / 86973979)

    Package version: 600000311

=== Mono Framework MDK ===

Runtime:
    Mono 6.0.0.311 (2019-02/494641b300c) (64-bit)
    Package version: 600000311

=== NuGet ===

Version: 5.1.0.6013

=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
    2.2.4
    2.1.9
    2.1.8
    2.1.7
    2.1.2
    2.1.1
    2.0.5
SDK: /usr/local/share/dotnet/sdk/2.2.203/Sdks
SDK Versions:
    2.2.203
    2.1.505
    2.1.504
    2.1.503
    2.1.302
    2.1.301
    2.1.4
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/6.0.0/lib/mono/msbuild/Current/bin/Sdks

=== Xamarin.Profiler ===

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

=== Updater ===

Version: 11

=== Xamarin.Android ===

Version: 9.4.0.51 (Visual Studio Enterprise)
Commit: xamarin-android/d16-2/9fa7775
Android SDK: /Users/SteveFeidelberg/Library/Android/sdk
    Supported Android versions:
        6.0 (API level 23)
        7.0 (API level 24)
        7.1 (API level 25)
        8.0 (API level 26)
        8.1 (API level 27)

SDK Tools Version: 26.1.1
SDK Platform Tools Version: 29.0.1
SDK Build Tools Version: 29.0.0 rc2

Build Information: 
Mono: mono/mono/2019-02@e6f5369c2d2
Java.Interop: xamarin/java.interop/d16-2@d64ada5
LibZipSharp: grendello/LibZipSharp/d16-2@caa0c74
LibZip: nih-at/libzip/rel-1-5-1@b95cf3f
ProGuard: xamarin/proguard/master@905836d
SQLite: xamarin/sqlite/3.27.1@8212a2d
Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-2@6f6c969

=== Microsoft Mobile OpenJDK ===

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

=== Android Device Manager ===

Version: 1.2.0.58
Hash: d03e3ea
Branch: remotes/origin/d16-2~1
Build date: 2019-07-16 17:37:14 UTC

=== Apple Developer Tools ===

Xcode 10.2.1 (14490.122)
Build 10E1001

=== Xamarin.Mac ===

Version: 5.14.0.110 (Visual Studio Enterprise)
Hash: a8bcecce
Branch: d16-2
Build date: 2019-06-28 07:31:43-0400

=== Xamarin.iOS ===

Version: 12.14.0.110 (Visual Studio Enterprise)
Hash: a8bcecce
Branch: d16-2
Build date: 2019-06-28 07:31:42-0400

=== Xamarin Designer ===

Version: 16.2.0.375
Hash: 357d38ef4
Branch: remotes/origin/d16-2
Build date: 2019-07-26 14:44:54 UTC

=== Xamarin Inspector ===

Version: 1.4.3
Hash: db27525
Branch: 1.4-release
Build date: Mon, 09 Jul 2018 21:20:18 GMT
Client compatibility: 1

=== Build Information ===

Release ID: 802010019
Git revision: c7aee29ba7d2a827dbfe4901e4ad7249ce7e5bc7
Build date: 2019-07-29 20:22:23+00
Build branch: release-8.2
Xamarin extensions: c990de17e8456ead7fce80aff9dab01df769b2fd

=== Operating System ===

Mac OS X 10.14.5
Darwin 18.6.0 Darwin Kernel Version 18.6.0
    Thu Apr 25 23:16:27 PDT 2019
    root:xnu-4903.261.4~2/RELEASE_X86_64 x86_64

=== Enabled user installed extensions ===

Team Foundation Version Control for TFS and Azure DevOps 0.4.4.5

Build Logs

Example Project (If Possible)

VS bug #960404

chamons commented 4 years ago

@SavikPavel - This issue has been closed for most of a month, and questions here both complicated the conversation (since I am not sure they are strictly related) and don't give us much detail.

Can you please file a new issue with: