Skip to content
Prev Previous commit
Added option to enable/disable Segfault suppression
  • Loading branch information
jamescrosswell committed Feb 6, 2025
commit e06c09cd33ed4c931ba78741bca1fe010b745747
16 changes: 16 additions & 0 deletions src/Sentry/Platforms/Android/AndroidOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,21 @@ public class AndroidOptions
/// </summary>
/// <seealso cref="LogCatIntegration" />
public int LogCatMaxLines { get; set; } = 1000;

/// <summary>
/// <para>
/// Whether to suppress capturing SIGSEGV (Segfault) errors in the Native SDK.
/// </para>
/// <para>
/// When managed code results in a NullReferenceException, this also causes a SIGSEGV (Segfault). Duplicate
/// events (one managed and one native) can be prevented by suppressing native Segfaults, which may be
/// convenient.
/// </para>
/// <para>
/// Enabling this may prevent the capture of Segfault originating from native (not managed) code... so it may
/// prevent the capture of genuine native Segfault errors.
/// </para>
/// </summary>
public bool SuppressSegfaults { get; set; } = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ public class AndroidOptions
{
public LogCatIntegrationType? LogCatIntegration { get; set; }
public int? LogCatMaxLines { get; set; }
public bool? SuppressSegfaults { get; set; }

public void ApplyTo(SentryOptions.AndroidOptions options)
{
options.LogCatIntegration = LogCatIntegration ?? options.LogCatIntegration;
options.LogCatMaxLines = LogCatMaxLines ?? options.LogCatMaxLines;
options.SuppressSegfaults = SuppressSegfaults ?? options.SuppressSegfaults;
}
}
}
3 changes: 2 additions & 1 deletion src/Sentry/Platforms/Android/SentrySdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ private static void InitSentryAndroidSdk(SentryOptions options)
{
// Suppress SIGSEGV errors.
// See: https://github.com/getsentry/sentry-dotnet/pull/3903
if (evt.SentryExceptions?.FirstOrDefault() is { Type: "SIGSEGV", Value: "Segfault" } exception)
if (options.Android.SuppressSegfaults
&& evt.SentryExceptions?.FirstOrDefault() is { Type: "SIGSEGV", Value: "Segfault" })
{
options.LogDebug("Suppressing SIGSEGV (this will be thrown as a managed exception instead)");
return null;
Expand Down
29 changes: 28 additions & 1 deletion test/Sentry.Tests/Platforms/Android/SentrySdkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ namespace Sentry.Tests.Platforms.Android;
public class SentrySdkTests
{
[Fact]
public void BeforeSendWrapper_SuppressSIGSEGV_ReturnsNull()
public void BeforeSendWrapper_Suppress_SIGSEGV_ReturnsNull()
{
// Arrange
var options = new SentryOptions();
options.Android.SuppressSegfaults = true;
var evt = new SentryEvent
{
SentryExceptions = new[]
Expand All @@ -28,6 +29,32 @@ public void BeforeSendWrapper_SuppressSIGSEGV_ReturnsNull()
result.Should().BeNull();
}

[Fact]
public void BeforeSendWrapper_DontSupress_SIGSEGV_ReturnsEvent()
{
// Arrange
var options = new SentryOptions();
options.Android.SuppressSegfaults = false;
var evt = new SentryEvent
{
SentryExceptions = new[]
{
new SentryException
{
Type = "SIGSEGV",
Value = "Segfault"
}
}
};
var hint = new SentryHint();

// Act
var result = SentrySdk.BeforeSendWrapper(options).Invoke(evt, hint);

// Assert
result.Should().Be(evt);
}

[Fact]
public void BeforeSendWrapper_BeforeSendCallbackDefined_CallsBeforeSend()
{
Expand Down
Loading