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.48k stars 514 forks source link

Xamarin iOS SignalR issue on real device #7399

Closed ricavir11 closed 4 years ago

ricavir11 commented 5 years ago

Steps to Reproduce

  1. Connect a Xamarin Forms iOS app to a signalR hub
  2. Try to invoke a method on the hub connection by calling hubConnection.InvokeAsync()
  3. Getting a JSON serializer exception (only if running on a real device, no exception if running app on ios simulator) :

{System.ExecutionEngineException: Attempting to JIT compile method '(wrapper delegate-invoke) System.Nullable1<int> <Module>:invoke_callvirt_Nullable1_SendChatMessageInput (Services.Chat.SendChatMessageInput)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

at System.Text.Json.ReflectionMemberAccessor+<>c__DisplayClass11_02[TClass,TProperty].<CreatePropertyGetter>b__0 (System.Object obj) [0x00000] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonPropertyInfoNullable2[TClass,TProperty].OnWrite (System.Text.Json.WriteStackFrame& current, System.Text.Json.Utf8JsonWriter writer) [0x0001c] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonPropertyInfo.Write (System.Text.Json.WriteStack& state, System.Text.Json.Utf8JsonWriter writer) [0x00029] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.HandleObject (System.Text.Json.JsonPropertyInfo jsonPropertyInfo, System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonWriter writer, System.Text.Json.WriteStack& state) [0x0005f] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.WriteObject (System.Text.Json.JsonSerializerOptions options, System.Text.Json.Utf8JsonWriter writer, System.Text.Json.WriteStack& state) [0x000d4] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.Write (System.Text.Json.Utf8JsonWriter writer, System.Int32 originalWriterDepth, System.Int32 flushThreshold, System.Text.Json.JsonSerializerOptions options, System.Text.Json.WriteStack& state) [0x0005b] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.WriteCore (System.Text.Json.Utf8JsonWriter writer, System.Text.Json.PooledByteBufferWriter output, System.Object value, System.Type type, System.Text.Json.JsonSerializerOptions options) [0x00050] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.WriteValueCore (System.Text.Json.Utf8JsonWriter writer, System.Object value, System.Type type, System.Text.Json.JsonSerializerOptions options) [0x00016] in <996dc3e1eb544ee0861c0639083a927e>:0 at System.Text.Json.JsonSerializer.Serialize (System.Text.Json.Utf8JsonWriter writer, System.Object value, System.Type inputType, System.Text.Json.JsonSerializerOptions options) [0x00007] in <996dc3e1eb544ee0861c0639083a927e>:0 at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteArguments (System.Object[] arguments, System.Text.Json.Utf8JsonWriter writer) [0x00062] in <1496dd884aa74204bf430db4f9b7c287>:0 at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteInvocationMessage (Microsoft.AspNetCore.SignalR.Protocol.InvocationMessage message, System.Text.Json.Utf8JsonWriter writer) [0x0001f] in <1496dd884aa74204bf430db4f9b7c287>:0 at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessageCore (Microsoft.AspNetCore.SignalR.Protocol.HubMessage message, System.Buffers.IBufferWriter1[T] stream) [0x00080] in <1496dd884aa74204bf430db4f9b7c287>:0 at Microsoft.AspNetCore.SignalR.Protocol.JsonHubProtocol.WriteMessage (Microsoft.AspNetCore.SignalR.Protocol.HubMessage message, System.Buffers.IBufferWriter1[T] output) [0x00000] in <1496dd884aa74204bf430db4f9b7c287>:0 at Microsoft.AspNetCore.SignalR.Client.HubConnection.SendHubMessage (Microsoft.AspNetCore.SignalR.Client.HubConnection+ConnectionState connectionState, Microsoft.AspNetCore.SignalR.Protocol.HubMessage hubMessage, System.Threading.CancellationToken cancellationToken) [0x00035] in :0 at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCore (Microsoft.AspNetCore.SignalR.Client.HubConnection+ConnectionState connectionState, System.String methodName, Microsoft.AspNetCore.SignalR.Client.Internal.InvocationRequest irq, System.Object[] args, System.String[] streams, System.Threading.CancellationToken cancellationToken) [0x00133] in :0 at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsyncCore (System.String methodName, System.Type returnType, System.Object[] args, System.Threading.CancellationToken cancellationToken) [0x00212] in :0 at System.Threading.Tasks.ForceAsyncAwaiter`1[T].GetResult () [0x0000c] in :0 at Microsoft.AspNetCore.SignalR.Client.HubConnection.InvokeCoreAsync (System.String methodName, System.Type returnType, System.Object[] args, System.Threading.CancellationToken cancellationToken) [0x0009e] in :0 at Services.Chat.ChatManager.SendMessageAsync (Services.Chat.SendChatMessageInput input) [0x00062] in C:\src\Mobile.Shared\Services\Chat\ChatManager.cs:88 }

Expected Behavior

A message is sent to the server

Actual Behavior

When running on real device (Debug or Release mode), message is not sent and an exception is thrown When running over iOS simulator, message is sent, no problem. Same code running on Android works as excepted.

Environment

Server : ASP.NET CORE 2.1

Client : Xamarin Forms v4.2 ; SignalR.Client v3.0.0

Microsoft Visual Studio Community 2019
Version 16.3.5
VisualStudio.16.Release/16.3.5+29411.108
Microsoft .NET Framework
Version 4.8.03752

Version installée : Community

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

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

ASP.NET and Web Tools 2019   16.3.285.24939
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.3.285.24939
Pour plus d'informations, visitez https://www.asp.net/

Assistants Microsoft Visual C++   1.0
Assistants Microsoft Visual C++

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.0
Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools   16.3.285.24939
Azure Functions and Web Jobs Tools

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

Common Azure Tools   1.10
Fournit des services courants utilisables par Microsoft Azure Mobile Services et Microsoft Azure Tools.

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.

Extension IntelliCode   1.0
Informations détaillées sur l'extension Visual Studio IntelliCode

Fabric.DiagnosticEvents   1.0
Événements de diagnostic Fabric

Gestionnaire de package NuGet   5.3.0
Gestionnaire de package NuGet dans Visual Studio. Pour plus d'informations sur NuGet, visitez https://docs.nuget.org/

Microsoft Azure HDInsight Azure Node   2.3.9000.0
HDInsight Node under Azure Node

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

Microsoft Azure Service Fabric Tools pour Visual Studio   16.0
Microsoft Azure Service Fabric Tools pour Visual Studio

Microsoft Azure Stream Analytics Language Service   2.3.9000.0
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 pour Microsoft Visual Studio 0x10 - v2.9.20816.1

Microsoft Continuous Delivery Tools pour Visual Studio   0.4
Simplification de la configuration des pipelines Azure DevOps à partir de l'IDE Visual Studio.

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

Mono Debugging for Visual Studio   16.3.7 (9d260c5)
Support for debugging Mono processes with Visual Studio.

Outils Azure App Service v3.0.0   16.3.285.24939
Outils Azure App Service v3.0.0

Outils C#   3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b
Composants C# utilisés dans l'IDE. Selon votre type de projet et vos paramètres, une version différente du compilateur peut être utilisée.

Outils TypeScript   16.0.10821.2002
Outils TypeScript pour Microsoft Visual Studio

Outils Visual Basic   3.3.1-beta3-19461-02+2fd12c210e22f7d6245805c60340f6a34af6875b
Composants Visual Basic utilisés dans l'IDE. Selon votre type de projet et vos paramètres, une version différente du compilateur peut être utilisée.

Package hôte de l'adaptateur de débogage de Visual Studio Code   1.0
Couche d'interopérabilité pour l'hébergement d'adaptateurs de débogage Visual Studio Code dans Visual Studio

Package Microsoft Visual Studio VC   1.0
Package Microsoft Visual Studio VC

Package Outils Application Insights pour Visual Studio   9.1.00913.1
Outils Application Insights pour Visual Studio

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.61908.27190
Microsoft SQL Server Data Tools

ToolWindowHostedEditor   1.0
Hosting json editor into a tool window

Visual F# Tools 10.4 pour F# 4.6   16.3.0-beta.19455.1+0422ff293bb2cc722fe5021b85ef50378a9af823
Microsoft Visual F# Tools 10.4 pour F# 4.6

Visual Studio Tools pour Conteneurs   1.0
Visual Studio Tools pour Conteneurs

Visual Studio Tools pour Docker   1.1
Développez, exécutez et validez vos applications ASP.NET Core dans l'environnement cible. Appuyez sur F5 pour exécuter votre application directement dans un conteneur avec débogage ou sur CTRL+F5 pour modifier et actualiser votre application sans avoir à recréer le conteneur.

Visual Studio Tools pour Kubernetes   1.0
Visual Studio Tools pour Kubernetes

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Xamarin   16.3.0.277 (d16-3@c0fcab7)
Extension Visual Studio permettant de développer pour Xamarin.iOS et Xamarin.Android.

Xamarin Designer   16.3.0.246 (remotes/origin/d16-3@bd2f86892)
Extension Visual Studio pour activer les outils Xamarin Designer dans Visual Studio.

Xamarin Templates   16.3.565 (27e9746)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   10.0.3.0 (d16-3/4d45b41)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: mono/mono/2019-06@5608fe0abb3
    Java.Interop: xamarin/java.interop/d16-3@5836f58
    LibZipSharp: grendello/LibZipSharp/d16-3@71f4a94
    LibZip: nih-at/libzip/rel-1-5-1@b95cf3fd
    ProGuard: xamarin/proguard/master@905836d
    SQLite: xamarin/sqlite/3.27.1@8212a2d
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-3@cb41333

Xamarin.iOS and Xamarin.Mac SDK   13.4.0.2 (e37549b)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

VS bug #1028573

chamons commented 5 years ago

It looks like we're running into a AOT issue:

{System.ExecutionEngineException: Attempting to JIT compile method '(wrapper delegate-invoke) System.Nullable1<int> <Module>:invoke_callvirt_Nullable1_SendChatMessageInput (Services.Chat.SendChatMessageInput)' while running in aot-only mode. See https://docs.microsoft.com/xamarin/ios/internals/limitations for more information.

Could you please attach a sample project, as we'll likely need it to run down this bug.

ricavir11 commented 5 years ago

Yes seems to be AOT issue. Can't share the project in public, I will send you a link by mail.

chamons commented 5 years ago

Is the Logisav e-mail I have that link?

ricavir11 commented 5 years ago

You have received another email to donwload the project, the first one was for your credentials

chamons commented 5 years ago

I have been able to reproduce and it is internal bug 1020091

rhy-ama commented 4 years ago

@chamons what is the root cause ? problem with a specific object/interface or simply broken for all use cases?

chamons commented 4 years ago

This has been tracked down to an issue in work at https://github.com/mono/mono/pull/17832

From the internal bug:

It looks like System.Text.Json was not designed to work in full-aot environments, esp the class ReflectionMemberAccessor.

A possible workaround is to avoid using nullable fields in the classes which are serialized/deserialized to/from JSON.

rhy-ama commented 4 years ago

@chamons Thank you vm!

If this is pure AOT issue then using MessagePack instead of Json serializer would not raise same issues ? - kindly confirm.

chamons commented 4 years ago

Unsure, I don't know how either of those libraries work under the hood.

rhy-ama commented 4 years ago

@chamons thank you

MessagePack has tooling to pre-compile serialization/deserialization code and should be compatible with most use cases.

Maybe @ricavir11 could test it, switching from json would be trivial. Here are the docs:

MessagePack hub protocol description - msft.

The referenced package is Microsoft.AspNetCore.SignalR.Protocols.MessagePack which is some fork of MessagePack-CSharp - github

MessagePack AOT support doc is here

ricavir11 commented 4 years ago

hi @rhy-ama ,

Unfortunately, I can't change server side easily... and from my understanding, MessagePack should b configured on server side.

Actualy, the application I'm working on is already in production with an angular web client (signal R OK), a Xamarin Android app (signal R OK) and a Xamarin iOS app which is causing the issue.

I can't ask to change all these apps just because of a serialization issue due to AOT on iOS.

Do you think we have a chance to have it fixed soon ? If so, do you have any time frame to provide ?

chamons commented 4 years ago

@ricavir11 @rhy-ama

The fix is in mono master, and will likely make a future release early this spring. It unfortunately is too late to get into the next major release.

It might be possible to get you folks a one-off build with the fix, but if you want an "official" build, we would need to make a service release.

To bring that up for consideration, you'd need to file an escalation. The link to create one depends on if you have any support contact from Microsoft (or Xamarin).:

General - https://support.microsoft.com/en-us/supportforbusiness/productselection?sapId=211dd84f-3474-c3c5-79bf-66db630c92a6

Paid (Unified or Professional) Support - https://support.microsoft.com/en-us/help/4341255/support-for-business

Xamarin premier support - https://support.microsoft.com/en-us/premier

ricavir11 commented 4 years ago

Wow, that's a huge one ! I've tried to open a support ticket but it seems also to have an issue :/ image

Unfortunately, I don't have a paid support available (or maybe with our azure subscription that is running ??).

Nevertheless, if we achieve to have a build earlier with this fix included, how can we add it to our projet ?

chamons commented 4 years ago

Are you building from VS Windows or VS for Mac? One is easier for you to get a one-off build and one will almost certainly require a service release for you to get the fix.

ricavir11 commented 4 years ago

VS Windows

chamons commented 4 years ago

Ok - We're looking into when we'd be able to get this fix out.

There are a few more moving parts here than I expected in getting your a VS (Windows) build.

jrikhof commented 4 years ago

Found a workaround for this issue:

It looks like iOS is having problems with Guid/DateTime objects. The workaround is to pass an new object with only strings/int values:

await _connection.InvokeAsync("SendMessage", new { Id = message.Id.ToString(), Text = message.Text, DateCreated = message.DateCreated.ToString("yyyy-MM-ddTHH\\:mm\\:ss.fffffffzzz") });