Skip to content

Commit 57fd7bb

Browse files
jpnurmiclaude
andcommitted
fix(android): preload NDK integration for CoreCLR
Enable the io.sentry.ndk.preload metadata so that SentryNdkPreloadProvider installs sentry-native's signal handlers before .NET/CoreCLR. This ensures the correct handler chain order (.NET -> sentry-native -> debuggerd) where .NET handles managed exceptions first, and native crashes chain to sentry-native. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 39ea4d8 commit 57fd7bb

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

integration-test/android.Tests.ps1

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ BeforeDiscovery {
1818
}
1919

2020
$cases = @(
21-
@{ configuration = 'Release' }
22-
@{ configuration = 'Debug' }
21+
# TODO: uncomment mono tests (#4676)
22+
#@{ configuration = 'Release'; runtime = 'mono' }
23+
@{ configuration = 'Release'; runtime = 'coreclr' }
24+
#@{ configuration = 'Debug'; runtime = 'mono' }
25+
@{ configuration = 'Debug'; runtime = 'coreclr' }
2326
)
24-
Describe 'MAUI app (<dotnet_version>, <configuration>)' -ForEach $cases -Skip:(-not $script:emulator) {
27+
Describe 'MAUI app (<dotnet_version>, <configuration>, <runtime>)' -ForEach $cases -Skip:(-not $script:emulator) {
2528
BeforeAll {
2629
$tfm = "$dotnet_version-android$(GetAndroidTpv $dotnet_version)"
2730

src/Sentry/Platforms/Android/SentrySdk.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Sentry;
2222
public static partial class SentrySdk
2323
{
2424
private static AndroidContext AppContext { get; set; } = Application.Context;
25+
private static bool IsMonoRuntime => Type.GetType("Mono.RuntimeStructs") != null;
2526

2627
private static void InitSentryAndroidSdk(SentryOptions options)
2728
{
@@ -64,7 +65,11 @@ private static void InitSentryAndroidSdk(SentryOptions options)
6465
o.ServerName = options.ServerName;
6566
o.SessionTrackingIntervalMillis = (long)options.AutoSessionTrackingInterval.TotalMilliseconds;
6667
o.ShutdownTimeoutMillis = (long)options.ShutdownTimeout.TotalMilliseconds;
67-
o.SetNativeHandlerStrategy(JavaSdk.Android.Core.NdkHandlerStrategy.SentryHandlerStrategyDefault);
68+
if (!IsMonoRuntime)
69+
{
70+
// TODO: exclude ChainAtStart when running on CoreCLR (#4676)
71+
o.SetNativeHandlerStrategy(JavaSdk.Android.Core.NdkHandlerStrategy.SentryHandlerStrategyDefault);
72+
}
6873

6974
if (options.CacheDirectoryPath is { } cacheDirectoryPath)
7075
{

src/Sentry/buildTransitive/Sentry.targets

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,21 @@
281281
</ItemGroup>
282282
</Target>
283283

284+
<!-- Preload NDK to ensure sentry-native's signal handlers are installed before .NET runtime's.
285+
Disabled on Mono until: https://github.com/dotnet/runtime/pull/125835 -->
286+
<Target Name="SetAndroidNdkPreload"
287+
BeforeTargets="GetAssemblyAttributes"
288+
Condition="'$(AndroidApplication)' == 'True'
289+
and $([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'
290+
and '$(UseMonoRuntime)' != 'true'">
291+
<ItemGroup>
292+
<AssemblyAttribute Include="Android.App.MetaData">
293+
<_Parameter1>io.sentry.ndk.preload</_Parameter1>
294+
<Value>true</Value>
295+
</AssemblyAttribute>
296+
</ItemGroup>
297+
</Target>
298+
284299
<!-- Upload Android ProGuard mapping file to Sentry after the build. -->
285300
<Target Name="UploadAndroidProGuardMappingFileToSentry" AfterTargets="Build" DependsOnTargets="PrepareSentryCLI"
286301
Condition="'$(SentryCLI)' != '' and '$(SentryUploadAndroidProGuardMapping)' == 'true' And '$(AndroidProguardMappingFile)' != ''">

0 commit comments

Comments
 (0)