dotnet / android

.NET for Android provides open-source bindings of the Android SDK for use with .NET managed languages such as C#
MIT License
1.91k stars 525 forks source link

net6.0-android, F#: "Cannot transition thread" crashes #5949

Closed bddckr closed 3 years ago

bddckr commented 3 years ago

Steps to Reproduce

My private project works on monoandroid10.0. Moving that project to net6.0-android results in crashes. I'm having a hard time to reduce the private project down to a minimum that I can share - I'm aware that this report isn't that helpful without a project that reproduces the issue, but I hope someone can point me to next steps to debug it further.

Expected Behavior

The app should not crash and (mostly) behave as it did with <TargetFramework>monoandroid10.0</TargetFramework>. I don't have any issues with the pre-.NET 6 copy of my project, especially no crashes.

Actual Behavior

When using

<TargetFramework>net6.0-android</TargetFramework>
<OutputType>Exe</OutputType>
<AndroidRuntimeIdentifier>android.30-arm64</AndroidRuntimeIdentifier>

in a newly created project to which I copied my existing project's files to, the app crashes.

The sln file uses 6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705 as the project type GUID to specify an F# project, using the new project system.

The crashes seem to occur whenever a new thread is spawned, or when the task pool switches to one. If I comment out the part that triggers a crash and allow the app to continue doing something else, it crashes in other places, again when it seemingly spawns/switches threads.

Version Information

My project is using a few packages:

<ItemGroup>
  <PackageReference Include="Xamarin.Google.Android.Material" Version="1.3.0.1-net6preview03.4680155" />
  <PackageReference Include="IdentityModel.OidcClient.IdentityTokenValidator" Version="4.0.0" />
  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
  <PackageReference Include="Xamarin.AndroidX.Concurrent.Futures" Version="1.1.0.2" />
  <PackageReference Include="Xamarin.AndroidX.Core" Version="1.3.2.3" />
  <PackageReference Include="Xamarin.AndroidX.WebKit" Version="1.4.0.1" />
  <PackageReference Include="Xamarin.AndroidX.Work.Runtime" Version="2.5.0.1" />
  <PackageReference Include="Xamarin.ANRWatchDog" Version="1.4.25" />
</ItemGroup>

The Nuget.Config looks like this:

<?xml version="1.0" encoding="utf-8"?>

<configuration>
  <packageSources>
    <clear />
    <add key="nuget" value="https://api.nuget.org/v3/index.json" />
    <add key="dotnet6" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet6/nuget/v3/index.json" />
    <add key="xamarin" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/xamarin-impl/nuget/v3/index.json" />
    <add key="public"  value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-public/nuget/v3/index.json" />
  </packageSources>
  <fallbackPackageFolders>
    <clear />
  </fallbackPackageFolders>
  <packageManagement>
    <add key="format" value="1" />
    <add key="disabled" value="False" />
  </packageManagement>
</configuration>

The Android manifest is pretty simple:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          package="my.package.name"
          android:versionCode="1"
          android:versionName="1.0"
          android:installLocation="internalOnly">
  <uses-sdk android:minSdkVersion="25" android:targetSdkVersion="30" />
  <application android:requestLegacyExternalStorage="true">
    <provider android:name="androidx.work.impl.WorkManagerInitializer" tools:node="remove" />
  </application>
</manifest>

There are no build errors or warnings.

Visual Studio Details ``` Microsoft Visual Studio Community 2019 Preview Version 16.10.0 Preview 4.0 VisualStudio.16.Preview/16.10.0-pre.4.0+31313.381 Microsoft .NET Framework Version 4.8.04161 Installed Version: Community Visual C++ 2019 00435-60000-00000-AA534 Microsoft Visual C++ 2019 ASP.NET and Web Tools 2019 16.10.524.62055 ASP.NET and Web Tools 2019 Azure App Service Tools v3.0.0 16.10.524.62055 Azure App Service Tools v3.0.0 C# Tools 3.10.0-4.21262.12+dc0aff25e0ace6e6bf98269963612f9a67cdc897 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. IntelliCode Extension 1.0 IntelliCode Visual Studio Extension Detailed Info 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 2.1.113+g422d40002e.RR 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.2 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.10.15 (552afdf) Support for debugging Mono processes with Visual Studio. NuGet Package Manager 5.10.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 Razor (ASP.NET Core) 16.1.0.2122504+13c05c96ea6bdbe550bd88b0bf6cdddf8cde1725 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 16.0.62105.04180 Microsoft SQL Server Data Tools Syntax Visualizer 1.0 An extension for visualizing Roslyn SyntaxTrees. TypeScript Tools 16.0.30429.2002 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 3.10.0-4.21262.12+dc0aff25e0ace6e6bf98269963612f9a67cdc897 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) 16.0.31306.167 Visual C++ for Cross Platform Mobile Development (Android) Visual F# Tools 16.10.0-beta.21262.7+1b23bbeda88ea3cb9be9af777f4c99fa8663df81 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 Container Tools Extensions 1.0 View, manage, and diagnose containers within Visual Studio. 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 Unity 4.10.2.0 Visual Studio Tools for Unity 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.10.000.226 (d16-10@e568dc9) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 16.10.0.115 (remotes/origin/c750fbf1bde3c720d077f51640fe197c6dac7cbe@c750fbf1b) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 16.10.5 (355b57a) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 11.3.0.1 (d16-10/22fc2b3) Xamarin.Android Reference Assemblies and MSBuild support. Mono: b4a3858 Java.Interop: xamarin/java.interop/d16-10@f39db25 ProGuard: Guardsquare/proguard/v7.0.1@912d149 SQLite: xamarin/sqlite/3.35.4@85460d3 Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-10@c5732a0 Xamarin.iOS and Xamarin.Mac SDK 14.19.0.7 (bb4428722) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```

Log File

I don't see anything in the logs when the crash occurs (besides the expected output from ActivityManager) except for

05-22 11:24:35.703 17447 17447 W monodroid: MonodroidRuntime::monodroid_pinvoke_override ("xa-internal-api", "_monodroid_gc_wait_for_bridge_processing")
05-22 11:24:35.703 17447 17447 W monodroid: Found xa-internal-api@_monodroid_gc_wait_for_bridge_processing in internal p/invoke map (0x73eeb620f4)
05-22 11:24:35.703 17447 17447 W monodroid: MonodroidRuntime::monodroid_pinvoke_override ("xa-internal-api", "_monodroid_gc_wait_for_bridge_processing")
05-22 11:24:35.703 17447 17447 W monodroid: Found xa-internal-api@_monodroid_gc_wait_for_bridge_processing in internal p/invoke map (0x73eeb620f4)
05-22 11:24:35.703 17447 17447 W monodroid: MonodroidRuntime::monodroid_pinvoke_override ("xa-internal-api", "_monodroid_gc_wait_for_bridge_processing")
05-22 11:24:35.703 17447 17447 W monodroid: Found xa-internal-api@_monodroid_gc_wait_for_bridge_processing in internal p/invoke map (0x73eeb620f4)
05-22 11:24:35.703 17447 17447 E         : (null) Cannot transition thread 0x748e8e49a8 from STATE_BLOCKING with DO_BLOCKING
05-22 11:24:35.704 17447 17447 F libc    : Fatal signal 6 (SIGABRT), code -6 in tid 17447 (my.package.name), pid 17447 (my.package.name)
grendello commented 3 years ago

@bddckr can you include the entire logcat please? It's best if you run the following commands before recording the crash:

adb shell setprop debug.mono.log default,timing=bare,assembly
adb logcat -G 16M
adb logcat -c
rem Run the application here
adb logcat -d > log.txt
adb bugreport

The last command will produce a zip file with the crash tombstone, detailed stack traces etc, please attach it as well, thanks!

jonpryor commented 3 years ago

The crash is probably from here: https://github.com/dotnet/runtime/blob/5dcc340a17e32d81dc12f644449c8b58396b2fd7/src/mono/mono/utils/mono-threads-state-machine.c#L757

@bddckr: would it be possible to get a complete repro project?

bddckr commented 3 years ago

Thanks for the help to both of you! I'll be following the steps on the weekend and will provide the requested details. Just been too busy at work currently.

bddckr commented 3 years ago

I've started a new project, trying to find a minimum repro but I've not been successful at that so far - it never crashes for me. I've also moved my existing sources into a new project (which crashes as expected), trying to reduce it down slowly but it's a very involved process.

Here is the log + bugreport for my full project at least:

bddckr commented 3 years ago

It looks like I am running into multiple things at the same time here. At least it looks like it to me - while trying to find a minimal repro I've noticed that any HttpClient usage seems to crash the app: #5982

I'm continuing to try to find the Cannot transition thread crash repro, but I don't seem to have any luck with that 😞

bddckr commented 3 years ago

I don't seem to be getting this threading issue anymore on Preview 5 (6.0.100-preview.5.21302.13).