Repository navigation
fix(android): drop native events when BeforeSend conversion fails - #5868
jamescrosswell merged 3 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5868 +/- ##
==========================================
+ Coverage 76.57% 76.78% +0.20%
==========================================
Files 528 532 +4
Lines 19624 19826 +202
Branches 3873 3909 +36
==========================================
+ Hits 15028 15224 +196
- Misses 3711 3717 +6
Partials 885 885 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for the PR @osamashtayat ❤️
Hm... that's a fair question. One option would be to change: - return null;
+ return options.Native.EnableBeforeSend && options.BeforeSendInternal != null ? null : e;So you'd only return null (dropping the event) if the SDK user had set That seems like a hack/workaround though - inside this handler we're trying to guess/infer whether the code is being executed to handle a segv signal or not and adapt the behaviour of the code accordingly. Probably a cleaner solution would be to have the sigsegv errors trigger a different code path to the custom beforesend handlers that SDK users wire up. Java’s SentryOptions has a single BeforeSend slot, so the two jobs need to be chained... so something like this in JavaSdk.SentryOptions.IBeforeSendCallback? beforeSend = null;
if (options.Native.EnableBeforeSend && options.BeforeSendInternal is not null)
{
beforeSend = new BeforeSendCallback(BeforeSendWrapper(options), options, o);
}
if (options.Android.SuppressSegfaults)
{
beforeSend = new SegfaultSuppressionCallback(beforeSend);
}
o.BeforeSend = beforeSend; |
|
To fill in the gap above: internal class SegfaultSuppressionCallback : JavaObject, JavaSdk.SentryOptions.IBeforeSendCallback
{
private readonly JavaSdk.SentryOptions.IBeforeSendCallback? _next;
public SegfaultSuppressionCallback(JavaSdk.SentryOptions.IBeforeSendCallback? next) => _next = next;
public JavaSdk.SentryEvent? Execute(JavaSdk.SentryEvent e, JavaSdk.Hint h) =>
e.Exceptions?.FirstOrDefault() is { Type: "SIGSEGV", Value: "Segfault" }
? null
: _next is null ? e : _next.Execute(e, h);
}With that in place, (A Java |
Co-Authored-By: Codex <noreply@openai.com>
|
Thanks for the review @jamescrosswell. I addressed both comments in Both callbacks share Java’s I ran the Android emulator suite: 2,933 passed, 0 failed, 27 skipped. All 12 callback-related cases passed, including the conversion regressions and the new tests for callback ordering and preserving a filter’s edits or drop decision. |
jamescrosswell
left a comment
There was a problem hiding this comment.
Thanks @osamashtayat - looks good.
About the only thing left is test coverage. No test sends SuppressSegfaults = true, EnableBeforeSend = false through CreateBeforeSendCallback, and that’s what this rework exists for.
Execute_WithoutFilter_* builds the class directly with null, and the factory tests either turn suppression off or turn both on. If someone broke the EnableBeforeSend check, nothing would fail. A single test would cover it: suppression on, filter set but not enabled, an unconvertible non-segfault event, then assert the filter wasn’t called and the same event came back.
|
Thanks @jamescrosswell. added the regression test in It uses All 13 Android callback cases passed. The full emulator run finished with 2,934 passed, 0 failed, and 27 skipped. |
jamescrosswell
left a comment
There was a problem hiding this comment.
Awesome - thank you for contributing @osamashtayat ❤️
|
Happy to help @jamescrosswell! I’d love to contribute more in the future. Thanks for the review and support! |
Description
Problem
On Android, when native
BeforeSendis enabled, a Java/NDK event that cannot be converted to or from a managedSentryEventis currently sent unfiltered.BeforeSendCallback.Executeconverts the original Java event to a managed event, runs the user'sBeforeSend, then converts the result back to Java. If either conversion throws, the exception handler logs the error and returnse— the original Java event. That means:BeforeSendnever runs and the raw event is sent;Users enable
Native.EnableBeforeSendspecifically to redact native events, so sending the raw event when filtering cannot run defeats the purpose.Root cause
BeforeSendCallback.ExecutewrapsToSentryEvent,ToHint, the user callback, andToJavaSentryEventin onetryblock. Itscatchreturns the original Java event:Returning
ebypasses whatever the managed filter did, or would have done.Fix
Return
nullon conversion failure instead of returning the original event:The Java SDK treats a
nullBeforeSendresult as a drop and records abefore_sendclient report. This is consistent withBeforeSendWrapper, which already returnsnullwhen the user's callback throws (#5610), and with the other Android callback bridges.Successful conversions and explicit callback drops are unchanged. SIGSEGV suppression still runs before the user's
BeforeSend, but it now has its own callback (see below). There is no public API change and no new dependency.Tests
Added Android device tests (13 cases).
BeforeSendCallbackTests:ServerName;ServerName.SegfaultSuppressionCallbackTests:SIGSEGV/Segfaultfirst exception is dropped;SentrySdkTests(CreateBeforeSendCallback):BeforeSenddrops the event and logs the error.Verified on an API 34 arm64 emulator using the repository's device-test script:
Also green:
Sentry.Testson .NET 8, 9, and 10 (0 failures)Sentry.Testsnet10.0-android36.0 build (0 warnings, 0 errors)dotnet formatandgit diff --checkThe full macOS solution build could not complete on the local machine because of unrelated host tooling issues (Intel
protocon Apple Silicon, sample DSN placeholders, and the Cocoa-from-source step). Android CI should be the authoritative device check.SIGSEGV suppression
Android.SuppressSegfaultsno longer goes through this bridge, andBeforeSendCallbackis only installed for an enabled user filter.SegfaultSuppressionCallbackchecks the first exception on the Java event and drops an exactSIGSEGV/Segfaultmatch before the user'sBeforeSendruns. Other events go to the user's filter if one is enabled. With suppression alone, the original Java event passes through without conversion, so a conversion failure can't drop it.Issues
Closes #5764