Skip to content

Can't handle exception by AppDomain.UnhandledException when thrown in Executor thread #6211

Closed
@azyobuzin

Description

@azyobuzin

Steps to Reproduce

  1. Set a handler on Application.OnCreate.

    public override void OnCreate()
    {
       AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
       {
           Console.WriteLine("!!! UnhandledException: " + e.ExceptionObject.GetType().FullName);
       };
       base.OnCreate();
    }
  2. Throw an exception on a thread made by Java.Util.Concurrent.Executors. In this example, the program is written on MainActivity.OnStart.

    protected override void OnStart()
    {
        base.OnStart();
        Java.Util.Concurrent.Executors.NewSingleThreadExecutor()
            .Execute(new Runnable(() =>
            {
                throw new Exception("Exception from Java Executor!");
            }));
    }

Project file: AndroidCrashTest.zip

Expected Behavior

The message !!! UnhandledException is written on Logcat before crash.

Actual Behavior

A crash occurs with SIGSEGV before the exception handler is called.

08-20 15:00:20.619 19214 19241 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
08-20 15:00:20.619 19214 19241 E AndroidRuntime: Process: com.companyname.androidcrashtest, PID: 19214
08-20 15:00:20.619 19214 19241 E AndroidRuntime: java.lang.Error: java.lang.Exception: Exception from Java Executor!
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:919)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: Caused by: java.lang.Exception: Exception from Java Executor!
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at mono.java.lang.Runnable.n_run(Native Method)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at mono.java.lang.Runnable.run(Runnable.java:30)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
08-20 15:00:20.619 19214 19241 E AndroidRuntime: 	... 2 more
08-20 15:00:20.619 19214 19241 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19241 (pool-1-thread-1), pid 19214 (ndroidcrashtest)

Version Information

Visual Studio 16.11.1 on Windows 10, Android Emulator (API 29, x86)

Microsoft Visual Studio Enterprise 2019
Version 16.11.1
VisualStudio.16.Release/16.11.1+31613.86
Microsoft .NET Framework
Version 4.8.04084

インストールされているバージョン:Enterprise

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

ASP.NET and Web Tools 2019   16.11.75.64347
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.11.75.64347
詳細については、https://www.asp.net/ をご覧ください

Azure App Service Tools v3.0.0   16.11.75.64347
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   16.11.75.64347
Azure Functions and Web Jobs Tools

C# ツール   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
IDE で使用する C# コンポーネント。プロジェクトの種類や設定に応じて、異なるバージョンのコンパイラを使用できます。

Common Azure Tools   1.10
Azure Mobile Services および Microsoft Azure Tools で使用する共通サービスを提供します。

Debugging Tools for Windows   10.0.18346.1000
Integrates the Windows Debugger functionality (http://go.microsoft.com/fwlink/?linkid=223405) in Visual Studio.

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

Fabric.DiagnosticEvents   1.0
ファブリックの診断イベント

GitHub.VisualStudio   2.11.106.19330
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode 拡張機能   1.0
IntelliCode Visual Studio 拡張機能の詳細情報

JetBrains ReSharper 2021.2   Build 212.0.20210730.153713
JetBrains ReSharper package for Microsoft Visual Studio. For more information about ReSharper, visit http://www.jetbrains.com/resharper. Copyright © 2021 JetBrains, Inc.

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

Microsoft Azure Tools for Visual Studio   2.9
Azure Cloud Services プロジェクトのサポート

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Visual Studio IDE 内からの Azure DevOps パイプラインの構成を簡略化しています。

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++ ウィザード   1.0
Microsoft Visual C++ ウィザード

Microsoft Visual Studio Tools for Containers   1.2
ターゲット環境で ASP.NET Core アプリケーションを開発、実行、検証します。F5 キーを押してコンテナーで直接アプリケーションをデバッグ実行するか、Ctrl + F5 キーを押してコンテナーをリビルドせずにアプリを編集して更新します。

Microsoft Visual Studio VC パッケージ   1.0
Microsoft Visual Studio VC パッケージ

Mono Debugging for Visual Studio   16.10.15 (552afdf)
Support for debugging Mono processes with Visual Studio.

NuGet パッケージ マネージャー   5.11.0
Visual Studio 内の NuGet パッケージ マネージャー。NuGet の詳細については、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.62107.28140
Microsoft SQL Server Data Tools

Syntax Visualizer   1.0
An extension for visualizing Roslyn SyntaxTrees.

TypeScript Tools   16.0.30526.2002
TypeScript Tools for Microsoft Visual Studio

Visual Basic ツール   3.11.0-4.21403.6+ae1fff344d46976624e68ae17164e0607ab68b10
IDE で使用する Visual Basic コンポーネント。プロジェクトの種類や設定に応じて、異なるバージョンのコンパイラを使用できます。

Visual F# Tools   16.11.0-beta.21322.6+488cc578cafcd261d90d748d8aaa7b8b091232dc
Microsoft Visual F# Tools

Visual Studio Code デバッグ アダプターのホスト パッケージ   1.0
Visual Studio Code デバッグ アダプターを 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 コンテナー ツール拡張機能   1.0
Visual Studio 内のコンテナーを表示、管理、診断します。

VisualStudio.DeviceLog   1.0
パッケージに関する情報

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

Windows Driver Kit   10.0.18346.1000
Headers, libraries, and tools needed to develop, debug, and test Windows drivers (msdn.microsoft.com/en-us/windows/hardware/gg487428.aspx)

Xamarin   16.11.000.174 (d16-11@e8f56f1)
Xamarin.iOS と Xamarin.Android の開発を有効にする Visual Studio 拡張機能

Xamarin Designer   16.11.0.17 (remotes/origin/11e0001f0b17269345e80b58fb3adf1ba4efe2cd@11e0001f0)
Visual Studio で Xamarin Designer ツールを有効にするための 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.4.0.5 (d16-11/7776c9f)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c633fe9
    Java.Interop: xamarin/java.interop/d16-11@48766c0
    ProGuard: Guardsquare/proguard/v7.0.1@912d149
    SQLite: xamarin/sqlite/3.35.4@85460d3
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-11@683f375


Xamarin.iOS and Xamarin.Mac SDK   14.20.0.25 (3b53e529b)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.

This issue can be reproduced when the project is built with both Debug and Release configuration. And also can be reproduced with the latest source code with some patches to build xamarin-android on my environment.

Log File

logcat

Investigation

Tombstone indicates the cause is null pointer dereference. I think mono_domain_get in this line returns null. https://github.com/xamarin/xamarin-android/blob/33f380858d2f7ca41f45856c552c80bca2ca0a67/src/monodroid/jni/monodroid-glue.cc#L2408

To prove this hypothesis, I added log outputs around domain usages and ran the application. The log indicates domain is null and the crash occurred in get_android_runtime_class.

08-20 15:22:04.461 19737 19763 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
08-20 15:22:04.461 19737 19763 E AndroidRuntime: Process: com.companyname.androidcrashtest, PID: 19737
08-20 15:22:04.461 19737 19763 E AndroidRuntime: java.lang.Error: java.lang.Exception: Exception from Java Executor!
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1173)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.lang.Thread.run(Thread.java:919)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: Caused by: java.lang.Exception: Exception from Java Executor!
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at mono.java.lang.Runnable.n_run(Native Method)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at mono.java.lang.Runnable.run(Runnable.java:30)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
08-20 15:22:04.461 19737 19763 E AndroidRuntime: 	... 2 more
08-20 15:22:04.461 19737 19763 W monodroid: !!! Java_mono_android_Runtime_propagateUncaughtException domain = 0x0
08-20 15:22:04.461 19737 19763 W monodroid: !!! propagate_uncaught_exception (Legacy)
08-20 15:22:04.461 19737 19763 W monodroid: !!! propagate_uncaught_exception domain = 0x0
08-20 15:22:04.462 19737 19763 F libc    : Fatal signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xb8 in tid 19763 (pool-1-thread-1), pid 19737 (ndroidcrashtest)

Full Log

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions