Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.java-version
.idea/
.gradle/
.run/
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Update Android targetSdk to API 36 (Android 16) ([#5016](https://github.com/getsentry/sentry-java/pull/5016))
- Merge Tombstone and Native SDK events into single crash event. ([#5037](https://github.com/getsentry/sentry-java/pull/5037))

### Internal

Expand Down
16 changes: 15 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,20 @@ public final class io/sentry/android/core/LoadClass : io/sentry/util/LoadClass {
public fun loadClass (Ljava/lang/String;Lio/sentry/ILogger;)Ljava/lang/Class;
}

public final class io/sentry/android/core/NativeEventCollector {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
public fun collect ()V
public fun deleteNativeEventFile (Lio/sentry/android/core/NativeEventCollector$NativeEventData;)Z
public fun findAndRemoveMatchingNativeEvent (J)Lio/sentry/android/core/NativeEventCollector$NativeEventData;
}

public final class io/sentry/android/core/NativeEventCollector$NativeEventData {
public fun getEnvelope ()Lio/sentry/SentryEnvelope;
public fun getEvent ()Lio/sentry/SentryEvent;
public fun getFile ()Ljava/io/File;
public fun getTimestampMs ()J
}

public final class io/sentry/android/core/NdkHandlerStrategy : java/lang/Enum {
public static final field SENTRY_HANDLER_STRATEGY_CHAIN_AT_START Lio/sentry/android/core/NdkHandlerStrategy;
public static final field SENTRY_HANDLER_STRATEGY_DEFAULT Lio/sentry/android/core/NdkHandlerStrategy;
Expand Down Expand Up @@ -500,7 +514,7 @@ public final class io/sentry/android/core/TombstoneIntegration$TombstoneHint : i
}

public class io/sentry/android/core/TombstoneIntegration$TombstonePolicy : io/sentry/android/core/ApplicationExitInfoHistoryDispatcher$ApplicationExitInfoPolicy {
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;)V
public fun <init> (Lio/sentry/android/core/SentryAndroidOptions;Landroid/content/Context;)V
public fun buildReport (Landroid/app/ApplicationExitInfo;Z)Lio/sentry/android/core/ApplicationExitInfoHistoryDispatcher$Report;
public fun getLabel ()Ljava/lang/String;
public fun getLastReportedTimestamp ()Ljava/lang/Long;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ static void initializeIntegrationsAndProcessors(
if (options.getSocketTagger() instanceof NoOpSocketTagger) {
options.setSocketTagger(AndroidSocketTagger.getInstance());
}

if (options.getPerformanceCollectors().isEmpty()) {
options.addPerformanceCollector(new AndroidMemoryCollector());
options.addPerformanceCollector(new AndroidCpuCollector(options.getLogger()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ final class ManifestMetadataReader {
static final String ANR_TIMEOUT_INTERVAL_MILLIS = "io.sentry.anr.timeout-interval-millis";
static final String ANR_ATTACH_THREAD_DUMPS = "io.sentry.anr.attach-thread-dumps";

static final String TOMBSTONE_ENABLE = "io.sentry.tombstone.enable";

static final String AUTO_INIT = "io.sentry.auto-init";
static final String NDK_ENABLE = "io.sentry.ndk.enable";
static final String NDK_SCOPE_SYNC_ENABLE = "io.sentry.ndk.scope-sync.enable";
Expand Down Expand Up @@ -201,6 +203,8 @@ static void applyMetadata(
}

options.setAnrEnabled(readBool(metadata, logger, ANR_ENABLE, options.isAnrEnabled()));
options.setTombstoneEnabled(
readBool(metadata, logger, TOMBSTONE_ENABLE, options.isTombstoneEnabled()));

// use enableAutoSessionTracking as fallback
options.setEnableAutoSessionTracking(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
package io.sentry.android.core;

import static io.sentry.cache.EnvelopeCache.PREFIX_CURRENT_SESSION_FILE;
import static io.sentry.cache.EnvelopeCache.PREFIX_PREVIOUS_SESSION_FILE;
import static io.sentry.cache.EnvelopeCache.STARTUP_CRASH_MARKER_FILE;

import io.sentry.SentryEnvelope;
import io.sentry.SentryEnvelopeItem;
import io.sentry.SentryEvent;
import io.sentry.SentryItemType;
import io.sentry.SentryLevel;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Collects native crash events from the outbox directory. These events can be correlated with
* tombstone events from ApplicationExitInfo to avoid sending duplicate crash reports.
*/
@ApiStatus.Internal
public final class NativeEventCollector {

private static final String NATIVE_PLATFORM = "native";

// TODO: will be replaced with the correlationId once the Native SDK supports it
private static final long TIMESTAMP_TOLERANCE_MS = 5000;

private final @NotNull SentryAndroidOptions options;
private final @NotNull List<NativeEventData> nativeEvents = new ArrayList<>();
private boolean collected = false;

public NativeEventCollector(final @NotNull SentryAndroidOptions options) {
this.options = options;
}

/** Holds a native event along with its source file for later deletion. */
public static final class NativeEventData {
private final @NotNull SentryEvent event;
private final @NotNull File file;
private final @NotNull SentryEnvelope envelope;
private final long timestampMs;

NativeEventData(
final @NotNull SentryEvent event,
final @NotNull File file,
final @NotNull SentryEnvelope envelope,
final long timestampMs) {
this.event = event;
this.file = file;
this.envelope = envelope;
this.timestampMs = timestampMs;
}

public @NotNull SentryEvent getEvent() {
return event;
}

public @NotNull File getFile() {
return file;
}

public @NotNull SentryEnvelope getEnvelope() {
return envelope;
}

public long getTimestampMs() {
return timestampMs;
}
}

/**
* Scans the outbox directory and collects all native crash events. This method should be called
* once before processing tombstones. Subsequent calls are no-ops.
*/
public void collect() {
if (collected) {
return;
}
collected = true;

final @Nullable String outboxPath = options.getOutboxPath();
if (outboxPath == null) {
options
.getLogger()
.log(SentryLevel.DEBUG, "Outbox path is null, skipping native event collection.");
return;
}

final File outboxDir = new File(outboxPath);
if (!outboxDir.isDirectory()) {
options.getLogger().log(SentryLevel.DEBUG, "Outbox path is not a directory: %s", outboxPath);
return;
}

final File[] files = outboxDir.listFiles((d, name) -> isRelevantFileName(name));
if (files == null || files.length == 0) {
options.getLogger().log(SentryLevel.DEBUG, "No envelope files found in outbox.");
return;
}

options
.getLogger()
.log(SentryLevel.DEBUG, "Scanning %d files in outbox for native events.", files.length);

for (final File file : files) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried of ending up with too many envelopes in memory here and potentially running into OOMs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me which code in particular is likely to cause references to spill? I'm not entirely sure. My assumption was that there would typically be very few native events (on average, 1 after each native crash), since they are usually sent on the next restart (and we only collect native events and their envelopes). So I would guess that, while iterating over and deserializing all envelopes, each iteration should leave the discarded ones ready for GC, right?

My bigger worry was the attachments that we pass around with the envelopes (yes, they are currently not attached to the tombstone events). I am fine with not passing around the envelope for the event, but we must ensure it is loaded at some point. Was this your concern? We could store only the envelope path in NativeEventData and only load it once we have a matching tombstone. Or did I completely misunderstand?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see we extractNativeEventFromFile() parses any envelope, so there could be transactions, or multiple crashes, e.g. if the device was offline. Or e.g. the app continuously crashes (in native code) during startup, a fix get released and now all unset envelopes pile up.

So I would just be a bit more defensive here, e.g. drop transactions, and have a sane maximum for nativeEvents.size()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I would just be a bit more defensive here, e.g. drop transactions, and have a sane maximum for nativeEvents.size()

I would love to be defensive; I would like to discard envelopes based solely on the filename (or other metadata), rather than having to read them. But even with reading them i am not sure about the concrete consequences of your proposal:

  • In extractNativeEventFromFile(), when iterating over the envelope items, we drop everything that isn't an event; isn't that what you mean by "drop transactions"?
    for (final SentryEnvelopeItem item : envelope.getItems()) {
    if (!SentryItemType.Event.equals(item.getHeader().getType())) {
    continue;
    }
  • RE: sane maximum:
    • If we limit the native events that we collect for matching, should the other events be discarded (since we assume sending a pure tombstone in their place) or allow a duplicate?
    • How do we know which events are the most significant/recent? Should we rely on disk timestamps?
    • We would, in any case, read more envelopes at least until decoding and reaching the maximum collection size, than we actually collect. So if heap pressure during parsing/decoding until those pieces are GCed is the worry, then the collection threshold is still happening too late.
    • If otoh, passing around the event + envelope is the worry, then we could reduce the NativeEventData to just timestamp and File and re-read the data only once there is an actual match.

The true cost is, of course, that we have to read, decode all envelopes, and deserialize every event, to verify that it is a native event and read its timestamp.

Copy link
Collaborator Author

@supervacuus supervacuus Jan 28, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The true cost is, of course, that we have to read, decode all envelopes, and deserialize every event, to verify that it is a native event and read its timestamp.

To be clear: We can make this part of the NativeEventCollector stream the envelopes (we only need item headers to skip anything but events) and the event (using a JsonObjectReader on an InputStream that is bound by the payload size), avoiding loading the entire envelope and hydrating the full object graph of each event until we actually have a match with a tombstone (at which point we would just use the current implementation extractNativeEventFromFile()).

I can't work on this today, but tomorrow you can have a basic implementation. That would eliminate the worry about loading a bunch of items into memory that we will never use.

if (!file.isFile()) {
continue;
}

final @Nullable NativeEventData nativeEventData = extractNativeEventFromFile(file);
if (nativeEventData != null) {
nativeEvents.add(nativeEventData);
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Found native event in outbox: %s (timestamp: %d)",
file.getName(),
nativeEventData.getTimestampMs());
}
}

options
.getLogger()
.log(SentryLevel.DEBUG, "Collected %d native events from outbox.", nativeEvents.size());
}

/**
* Finds a native event that matches the given tombstone timestamp. If a match is found, it is
* removed from the internal list so it won't be matched again.
*
* <p>This method will lazily collect native events from the outbox on first call.
*
* @param tombstoneTimestampMs the timestamp from ApplicationExitInfo
* @return the matching native event data, or null if no match found
*/
public @Nullable NativeEventData findAndRemoveMatchingNativeEvent(
final long tombstoneTimestampMs) {

// Lazily collect on first use (runs on executor thread, not main thread)
collect();

for (final NativeEventData nativeEvent : nativeEvents) {
final long timeDiff = Math.abs(tombstoneTimestampMs - nativeEvent.getTimestampMs());
if (timeDiff <= TIMESTAMP_TOLERANCE_MS) {
options
.getLogger()
.log(SentryLevel.DEBUG, "Matched native event by timestamp (diff: %d ms)", timeDiff);
nativeEvents.remove(nativeEvent);
return nativeEvent;
}
}

return null;
}

/**
* Deletes a native event file from the outbox.
*
* @param nativeEventData the native event data containing the file reference
* @return true if the file was deleted successfully
*/
public boolean deleteNativeEventFile(final @NotNull NativeEventData nativeEventData) {
final File file = nativeEventData.getFile();
try {
if (file.delete()) {
options
.getLogger()
.log(SentryLevel.DEBUG, "Deleted native event file from outbox: %s", file.getName());
return true;
} else {
options
.getLogger()
.log(
SentryLevel.WARNING,
"Failed to delete native event file: %s",
file.getAbsolutePath());
return false;
}
} catch (Throwable e) {
options
.getLogger()
.log(
SentryLevel.ERROR, e, "Error deleting native event file: %s", file.getAbsolutePath());
return false;
}
}

private @Nullable NativeEventData extractNativeEventFromFile(final @NotNull File file) {
try (final InputStream stream = new BufferedInputStream(new FileInputStream(file))) {
final SentryEnvelope envelope = options.getEnvelopeReader().read(stream);
if (envelope == null) {
return null;
}

for (final SentryEnvelopeItem item : envelope.getItems()) {
if (!SentryItemType.Event.equals(item.getHeader().getType())) {
continue;
}

try (final Reader eventReader =
new BufferedReader(
new InputStreamReader(
new ByteArrayInputStream(item.getData()), StandardCharsets.UTF_8))) {
final SentryEvent event =
options.getSerializer().deserialize(eventReader, SentryEvent.class);
if (event != null && NATIVE_PLATFORM.equals(event.getPlatform())) {
final long timestampMs = extractTimestampMs(event);
return new NativeEventData(event, file, envelope, timestampMs);
}
}
}
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.DEBUG, e, "Error reading envelope file: %s", file.getAbsolutePath());
}
return null;
}

private long extractTimestampMs(final @NotNull SentryEvent event) {
final @Nullable Date timestamp = event.getTimestamp();
if (timestamp != null) {
return timestamp.getTime();
}
return 0;
}

private boolean isRelevantFileName(final @Nullable String fileName) {
return fileName != null
&& !fileName.startsWith(PREFIX_CURRENT_SESSION_FILE)
&& !fileName.startsWith(PREFIX_PREVIOUS_SESSION_FILE)
&& !fileName.startsWith(STARTUP_CRASH_MARKER_FILE);
}
}
Loading
Loading