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.46k stars 511 forks source link

TaskCanceledException raised when attempting an HttpClient.GetAsync #6936

Closed wadebaird closed 4 years ago

wadebaird commented 5 years ago

I realize the root of this issue is most likely related to https://github.com/xamarin/xamarin-macios/issues/6443 and https://github.com/xamarin/xamarin-macios/pull/5463, however the application in this case is not in the background. In addition, I also think this is probably not the most ideal breaking change to make, especially since it immediately cancels all http calls when an app is placed in the background (such as simply swiping up from the bottom of the phone to load the iPhones bottom menu). This behavior will cause everyone to implement the background session handler for ALL http calls in order to prevent their simple calls from being cancelled, or simply go back to the version of Xamarin.Forms before this change was made.

Steps to Reproduce

  1. Load the attached project, it needs to be run on an actual device with TouchId enabled.
  2. Fingerprint auth when the TouchId dialog pops up

Expected Behavior

No CanceledTaskException occurs (there is no real behavior in this repro as I minimized the code and project down to the bare minimum.

Actual Behavior

TaskCanceledException occurs on the Http.GetAsync call at callstack:

    0xC in System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/exceptionservices/exceptionservicescommon.cs:157,13 C#
    0x1B in System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:175,25    C#
    0x29 in System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:154,48   C#
    0x9 in System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs:125,17    C#
    0xFFFFFFFFFFFFFFFF in System.Runtime.CompilerServices.TaskAwaiter<System.Net.Http.HttpResponseMessage>.GetResult at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/shared/System/Runtime/CompilerServices/TaskAwaiter.cs C#
>   0xBD in CanceledTaskRepro.Services.ProboscisService.EstablishUserSession at C:\source\CanceledTaskExceptionRepro\CanceledTaskExceptionRepro\Services\ProboscisService.cs:103    C#
    0x6 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1092,17  C#
    0x73 in System.Threading.ExecutionContext.RunInternal at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:968,17  C#
    0x4 in System.Threading.ExecutionContext.Run at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:910,13   C#
    0x32 in System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:1073,25    C#
    0x6 in System.Threading.Tasks.SynchronizationContextAwaitTaskContinuation.<>c.<.cctor>b__7_0 at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/TaskContinuation.cs:379,78 C#
    0xC in Foundation.NSAsyncSynchronizationContextDispatcher.Apply at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/Foundation/NSAction.cs:178,5  C#
    0xFFFFFFFFFFFFFFFF in UIKit.UIApplication.UIApplicationMain C#
    0xB in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/UIKit/UIApplication.cs:86,4   C#
    0x11 in UIKit.UIApplication.Main at /Library/Frameworks/Xamarin.iOS.framework/Versions/12.14.0.114/src/Xamarin.iOS/UIKit/UIApplication.cs:65,5  C#
    0x8 in CanceledTaskExceptionRepro.iOS.Application.Main at C:\source\CanceledTaskExceptionRepro\CanceledTaskExceptionRepro.iOS\Main.cs:17,13 C#

Environment

Microsoft Visual Studio Professional 2019 (2)
Version 16.2.3
VisualStudio.16.Release/16.2.3+29215.179
Microsoft .NET Framework
Version 4.8.03761

Installed Version: Professional

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

ADL Tools Service Provider   1.0
This package contains services used by Data Lake tools

Application Insights Tools for Visual Studio Package   9.1.00611.1
Application Insights Tools for Visual Studio

ASP.NET and Web Tools 2019   16.2.290.48383
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.2.290.48383
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.2.290.48383
Azure App Service Tools v3.0.0

Azure Data Lake Node   1.0
This package contains the Data Lake integration nodes for Server Explorer.

Azure Data Lake Tools for Visual Studio   2.3.9000.1
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   16.2.290.48383
Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio   2.3.9000.1
Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools   3.2.1-beta4-19408-03+2fc6a04980f800c59e8ede97e6ae294ff47d666e
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.

Cookiecutter   16.2.19178.1
Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Extensibility Message Bus   1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

Fabric.DiagnosticEvents   1.0
Fabric Diagnostic Events

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Microsoft Azure HDInsight Azure Node   2.3.9000.1
HDInsight Node under Azure Node

Microsoft Azure Hive Query Language Service   2.3.9000.1
Language service for Hive query

Microsoft Azure Service Fabric Tools for Visual Studio   2.5
Microsoft Azure Service Fabric Tools for Visual Studio

Microsoft Azure Stream Analytics Language Service   2.3.9000.1
Language service for Azure Stream Analytics

Microsoft Azure Stream Analytics Node   1.0
Azure Stream Analytics Node under Azure Node

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 0x10 - v2.9.20626.2

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

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

Microsoft Library Manager   1.0
Install client-side libraries easily to any web project

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 Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.2.6 (4cfc7c3)
Support for debugging Mono processes with Visual Studio.

Node.js Tools   1.5.10610.1 Commit Hash:529a87de2769e143655e31ea81795c42e5775ef8
Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager   5.2.0
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

Python   16.2.19178.1
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Python - Conda support   16.2.19178.1
Conda support for Python projects.

Python - Django support   16.2.19178.1
Provides templates and integration for the Django web framework.

Python - IronPython support   16.2.19178.1
Provides templates and integration for IronPython-based projects.

Python - Profiling support   16.2.19178.1
Profiling support for Python projects.

ResourcePackage Extension   1.0
ResourcePackage Visual Studio Extension Detailed Info

ResourcePackage Extension   1.0
ResourcePackage Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.61906.28070
Microsoft SQL Server Data Tools

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.

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

TypeScript Tools   16.0.10627.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.2.1-beta4-19408-03+2fc6a04980f800c59e8ede97e6ae294ff47d666e
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Cross Platform Mobile Development (Android)   15.0.29006.69
Visual C++ for Cross Platform Mobile Development (Android)

Visual F# Tools 10.4 for F# 4.6   16.2.0-beta.19321.1+a24d94ecf97d0d69d4fbe6b8b10cd1f97737fff4
Microsoft Visual F# Tools 10.4 for F# 4.6

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

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

Visual Studio Tools for Kubernetes   1.0
Visual Studio Tools for Kubernetes

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.2.0.95 (d16-2@37df81894)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.2.0.375 (remotes/origin/d16-2@357d38ef4)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.3.117 (59a59e8)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   9.4.1.0 (d16-2/ebd65c9)
Xamarin.Android Reference Assemblies and MSBuild support.
    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

Xamarin.iOS and Xamarin.Mac SDK   12.14.0.114 (c669116)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

Build Logs

N/A

Example Project (If Possible)

CanceledTaskExceptionRepro.zip

deyupopp commented 5 years ago

I confirm we have the exact same issue. We are executing the authentication request immediately after decryption using TouchId, and this worked until the last update. We made a workaround for this particular case, by delaying the request a few milliseconds, so it executes after the app regains "foreground" state, but this is definitely not ideal.

czuck commented 4 years ago

I am having this issue when iOS prompts the user to accept the required permissions. I've put a work around in to catch the exception and re-initiate the HTTP call if the caught exception is a TaskCanceledException, but this is not ideal.

mandel-macaque commented 4 years ago

I am closing the issues since we provided a workaround to stop this behavior from happening in https://github.com/xamarin/xamarin-macios/pull/6918

The issue is that we added this workaround because the mono runtime has issues with the ThreadPool in certain situations in which a request is performed and the application is moved to the background. In that case, we cancel the task so that we are in a known state and we can recover.

The workaround is a temporary fix until the mono team fixes the issue. But if you do not want the new behavior, please take a look at the new property added in the PR mentioned above and voluntarily make your HttpClient not to cancel requests (which is something that, for small requests, will work correctly).