From 9c190de8b70426605597335644204a12c8795b22 Mon Sep 17 00:00:00 2001 From: Nelson Osacky Date: Tue, 1 Dec 2026 16:19:54 +0100 Subject: [PATCH] ref(android): Hand SentryRuntime to the SDK's integrations The SDK's integrations become InternalIntegrations, so Sentry.init passes them the runtime and they take their executor and envelope cache from it instead of from SentryOptions. Their two-argument register keeps working by building a runtime over the collaborators on the options. The ANR, Tombstone and MemoryLimiter tests now build a strict runtime holding only what each integration uses. Part of JAVA-817 Co-Authored-By: Claude Opus 5.5 --- .../api/sentry-android-core.api | 30 +++-- .../core/ActivityLifecycleIntegration.java | 26 +++- .../sentry/android/core/AnrIntegration.java | 36 +++-- .../sentry/android/core/AnrV2Integration.java | 26 +++- .../AppComponentsBreadcrumbsIntegration.java | 22 +++- .../ApplicationExitInfoHistoryDispatcher.java | 4 +- .../core/EnvelopeFileObserverIntegration.java | 38 ++++-- .../core/FeedbackShakeIntegration.java | 26 +++- .../core/MemoryLimiterIntegration.java | 24 +++- .../core/SendCachedEnvelopeIntegration.java | 124 ++++++++++-------- .../SystemEventsBreadcrumbsIntegration.java | 109 ++++++++------- .../android/core/TombstoneIntegration.java | 19 ++- .../core/anr/AnrProfilingIntegration.java | 41 ++++-- .../android/core/AnrV2IntegrationTest.kt | 12 +- ...ApplicationExitInfoCrossIntegrationTest.kt | 19 ++- .../ApplicationExitIntegrationTestBase.kt | 66 ++++++---- .../core/MemoryLimiterIntegrationTest.kt | 65 +++++---- .../android/core/TombstoneIntegrationTest.kt | 18 +-- sentry/api/sentry.api | 3 +- ...achedEnvelopeFireAndForgetIntegration.java | 120 +++++++++-------- 20 files changed, 516 insertions(+), 312 deletions(-) diff --git a/sentry-android-core/api/sentry-android-core.api b/sentry-android-core/api/sentry-android-core.api index 9a2c6f6d931..7ffcca80fe7 100644 --- a/sentry-android-core/api/sentry-android-core.api +++ b/sentry-android-core/api/sentry-android-core.api @@ -21,7 +21,7 @@ public final class io/sentry/android/core/ActivityFramesTracker { public fun takeMetrics (Lio/sentry/protocol/SentryId;)Ljava/util/Map; } -public final class io/sentry/android/core/ActivityLifecycleIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/ActivityLifecycleIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/app/Application;Lio/sentry/android/core/BuildInfoProvider;Lio/sentry/android/core/ActivityFramesTracker;)V public fun close ()V public fun onActivityCreated (Landroid/app/Activity;Landroid/os/Bundle;)V @@ -38,6 +38,7 @@ public final class io/sentry/android/core/ActivityLifecycleIntegration : android public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/AndroidContinuousProfiler : io/sentry/IContinuousProfiler, io/sentry/transport/RateLimiter$IRateLimitObserver { @@ -147,10 +148,11 @@ public final class io/sentry/android/core/AndroidSocketTagger : io/sentry/ISocke public fun untagSockets ()V } -public final class io/sentry/android/core/AnrIntegration : io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/AnrIntegration : io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/AnrIntegrationFactory { @@ -158,10 +160,11 @@ public final class io/sentry/android/core/AnrIntegrationFactory { public static fun create (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;)Lio/sentry/Integration; } -public final class io/sentry/android/core/AnrV2Integration : io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/AnrV2Integration : io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/AnrV2Integration$AnrV2Hint : io/sentry/hints/BlockingFlushHint, io/sentry/hints/AbnormalExit, io/sentry/hints/Backfillable { @@ -174,13 +177,14 @@ public final class io/sentry/android/core/AnrV2Integration$AnrV2Hint : io/sentry public fun timestamp ()Ljava/lang/Long; } -public final class io/sentry/android/core/AppComponentsBreadcrumbsIntegration : android/content/ComponentCallbacks2, io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/AppComponentsBreadcrumbsIntegration : android/content/ComponentCallbacks2, io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V public fun onConfigurationChanged (Landroid/content/res/Configuration;)V public fun onLowMemory ()V public fun onTrimMemory (I)V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/AppLifecycleIntegration : io/sentry/Integration, java/io/Closeable { @@ -294,15 +298,16 @@ public final class io/sentry/android/core/DeviceInfoUtil { public static fun isCharging (Landroid/content/Intent;Lio/sentry/SentryOptions;)Ljava/lang/Boolean; } -public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : io/sentry/Integration, java/io/Closeable { +public abstract class io/sentry/android/core/EnvelopeFileObserverIntegration : io/sentry/internal/InternalIntegration, java/io/Closeable { protected final field startLock Lio/sentry/util/AutoClosableReentrantLock; public fun ()V public fun close ()V public static fun getOutboxFileObserver ()Lio/sentry/android/core/EnvelopeFileObserverIntegration; public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public final fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } -public final class io/sentry/android/core/FeedbackShakeIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/Integration, io/sentry/SentryFeedbackOptions$IShakeController, java/io/Closeable { +public final class io/sentry/android/core/FeedbackShakeIntegration : android/app/Application$ActivityLifecycleCallbacks, io/sentry/SentryFeedbackOptions$IShakeController, io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/app/Application;)V public fun close ()V public fun disableOnShake ()V @@ -316,6 +321,7 @@ public final class io/sentry/android/core/FeedbackShakeIntegration : android/app public fun onActivityStarted (Landroid/app/Activity;)V public fun onActivityStopped (Landroid/app/Activity;)V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public abstract interface class io/sentry/android/core/IDebugImagesLoader { @@ -348,10 +354,11 @@ 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/MemoryLimiterIntegration : io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/MemoryLimiterIntegration : io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;)V public fun close ()V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/MemoryLimiterIntegration$MemoryLimiterHint : io/sentry/hints/BlockingFlushHint, io/sentry/hints/AbnormalExit, io/sentry/hints/Backfillable { @@ -665,7 +672,7 @@ public final class io/sentry/android/core/SpanFrameMetricsCollector : io/sentry/ public fun onSpanStarted (Lio/sentry/ISpan;)V } -public final class io/sentry/android/core/SystemEventsBreadcrumbsIntegration : io/sentry/Integration, io/sentry/android/core/AppState$AppStateListener, java/io/Closeable { +public final class io/sentry/android/core/SystemEventsBreadcrumbsIntegration : io/sentry/android/core/AppState$AppStateListener, io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun (Landroid/content/Context;Landroid/os/Handler;)V public fun (Landroid/content/Context;Ljava/util/List;)V @@ -674,12 +681,14 @@ public final class io/sentry/android/core/SystemEventsBreadcrumbsIntegration : i public fun onBackground ()V public fun onForeground ()V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } -public final class io/sentry/android/core/TombstoneIntegration : io/sentry/Integration, java/io/Closeable { +public final class io/sentry/android/core/TombstoneIntegration : io/sentry/internal/InternalIntegration, java/io/Closeable { public fun (Landroid/content/Context;)V public fun close ()V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public final class io/sentry/android/core/TombstoneIntegration$TombstoneHint : io/sentry/hints/BlockingFlushHint, io/sentry/hints/Backfillable, io/sentry/hints/NativeCrashExit { @@ -768,7 +777,7 @@ public final class io/sentry/android/core/anr/AnrProfileRotationHelper { public static fun rotate ()V } -public final class io/sentry/android/core/anr/AnrProfilingIntegration : io/sentry/Integration, io/sentry/android/core/AppState$AppStateListener, java/io/Closeable, java/lang/Runnable { +public final class io/sentry/android/core/anr/AnrProfilingIntegration : io/sentry/android/core/AppState$AppStateListener, io/sentry/internal/InternalIntegration, java/io/Closeable, java/lang/Runnable { public static final field POLLING_INTERVAL_MS J public static final field THRESHOLD_ANR_MS J public fun ()V @@ -776,6 +785,7 @@ public final class io/sentry/android/core/anr/AnrProfilingIntegration : io/sentr public fun onBackground ()V public fun onForeground ()V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V public fun run ()V } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java index 6d4a74429eb..de36ce8dac5 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java @@ -14,11 +14,12 @@ import io.sentry.FullyDisplayedReporter; import io.sentry.IScope; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; import io.sentry.ISpan; import io.sentry.ITransaction; import io.sentry.Instrumenter; -import io.sentry.Integration; +import io.sentry.NoOpSentryExecutorService; import io.sentry.NoOpTransaction; import io.sentry.PropagationContext; import io.sentry.SentryDate; @@ -35,6 +36,9 @@ import io.sentry.android.core.performance.ActivityLifecycleSpanHelper; import io.sentry.android.core.performance.AppStartMetrics; import io.sentry.android.core.performance.TimeSpan; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.MeasurementValue; import io.sentry.protocol.SentryId; import io.sentry.protocol.TransactionNameSource; @@ -56,7 +60,10 @@ import org.jetbrains.annotations.VisibleForTesting; public final class ActivityLifecycleIntegration - implements Integration, Closeable, Application.ActivityLifecycleCallbacks { + implements InternalIntegration, Closeable, Application.ActivityLifecycleCallbacks { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); static final String UI_LOAD_OP = "ui.load"; static final String STANDALONE_APP_START_OP = "app.start"; @@ -128,6 +135,15 @@ public ActivityLifecycleIntegration( @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -390,10 +406,8 @@ private void startTracing(final @NotNull Activity activity) { try { ttfdSpanMap.put(activity, ttfdSpan); ttfdAutoCloseFuture = - options - .getExecutorService() - .schedule( - () -> finishExceededTtfdSpan(ttfdSpan, ttidSpan), TTFD_TIMEOUT_MILLIS); + executorService.schedule( + () -> finishExceededTtfdSpan(ttfdSpan, ttidSpan), TTFD_TIMEOUT_MILLIS); } catch (RejectedExecutionException e) { options .getLogger() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java index a1b3abd3906..48cb0807edb 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrIntegration.java @@ -6,14 +6,18 @@ import android.content.Context; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.Integration; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.exception.ExceptionMechanismException; import io.sentry.hints.AbnormalExit; import io.sentry.hints.TransactionEnd; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.Mechanism; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.HintUtils; @@ -28,7 +32,10 @@ * When the UI thread of an Android app is blocked for too long, an "Application Not Responding" * (ANR) error is triggered. Sends an event if an ANR happens */ -public final class AnrIntegration implements Integration, Closeable { +public final class AnrIntegration implements InternalIntegration, Closeable { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private final @NotNull Context context; private boolean isClosed = false; @@ -51,6 +58,15 @@ public AnrIntegration(final @NotNull Context context) { @Override public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public final void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.options = Objects.requireNonNull(options, "SentryOptions is required"); register(scopes, (SentryAndroidOptions) options); } @@ -65,15 +81,13 @@ private void register( if (options.isAnrEnabled()) { addIntegrationToSdkVersion("Anr"); try { - options - .getExecutorService() - .submit(() -> { - try (final @NotNull ISentryLifecycleToken ignored = startLock.acquire()) { - if (!isClosed) { - startAnrWatchdog(scopes, options); - } - } - }); + executorService.submit(() -> { + try (final @NotNull ISentryLifecycleToken ignored = startLock.acquire()) { + if (!isClosed) { + startAnrWatchdog(scopes, options); + } + } + }); } catch (Throwable e) { options .getLogger() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java index 1fafe4e0d9e..d9f12979712 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AnrV2Integration.java @@ -11,7 +11,6 @@ import io.sentry.Hint; import io.sentry.ILogger; import io.sentry.IScopes; -import io.sentry.Integration; import io.sentry.SentryEvent; import io.sentry.SentryLevel; import io.sentry.SentryOptions; @@ -22,6 +21,9 @@ import io.sentry.hints.AbnormalExit; import io.sentry.hints.Backfillable; import io.sentry.hints.BlockingFlushHint; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.ArtContext; import io.sentry.protocol.DebugImage; import io.sentry.protocol.DebugMeta; @@ -42,7 +44,7 @@ import org.jetbrains.annotations.Nullable; @SuppressLint("NewApi") // we check this in AnrIntegrationFactory -public final class AnrV2Integration implements Integration, Closeable { +public final class AnrV2Integration implements InternalIntegration, Closeable { private final @NotNull Context context; private @Nullable SentryAndroidOptions options; @@ -51,10 +53,18 @@ public AnrV2Integration(final @NotNull Context context) { this.context = ContextUtils.getApplicationContext(context); } - @SuppressLint("NewApi") // we do the check in the AnrIntegrationFactory @Override - @SuppressWarnings("FutureReturnValueIgnored") public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + @SuppressLint("NewApi") // we do the check in the AnrIntegrationFactory + @SuppressWarnings("FutureReturnValueIgnored") + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -73,11 +83,15 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { if (this.options.isAnrEnabled()) { try { - options + runtime .getExecutorService() .submit( new ApplicationExitInfoHistoryDispatcher( - context, scopes, this.options, new AnrV2Policy(this.options))); + context, + scopes, + this.options, + runtime.getEnvelopeDiskCache(), + new AnrV2Policy(this.options))); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to start ANR processor.", e); } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java index 66afe6033a3..9d85f4d690a 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/AppComponentsBreadcrumbsIntegration.java @@ -9,11 +9,15 @@ import io.sentry.Breadcrumb; import io.sentry.Hint; import io.sentry.IScopes; -import io.sentry.Integration; +import io.sentry.ISentryExecutorService; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.android.core.internal.util.Debouncer; import io.sentry.android.core.internal.util.DeviceOrientations; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.Device; import io.sentry.util.Objects; import java.io.Closeable; @@ -23,7 +27,10 @@ import org.jetbrains.annotations.Nullable; public final class AppComponentsBreadcrumbsIntegration - implements Integration, Closeable, ComponentCallbacks2 { + implements InternalIntegration, Closeable, ComponentCallbacks2 { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private static final long DEBOUNCE_WAIT_TIME_MS = 60 * 1000; // pre-allocate hint to avoid creating it every time for the low memory case @@ -42,6 +49,15 @@ public AppComponentsBreadcrumbsIntegration(final @NotNull Context context) { @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( @@ -172,7 +188,7 @@ private void captureConfigurationChangedBreadcrumb( private void executeInBackground(final @NotNull Runnable runnable) { if (options != null) { try { - options.getExecutorService().submit(runnable); + executorService.submit(runnable); } catch (Throwable t) { options .getLogger() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoHistoryDispatcher.java b/sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoHistoryDispatcher.java index 88c9d377e4b..484e8d435f8 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoHistoryDispatcher.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoHistoryDispatcher.java @@ -49,6 +49,7 @@ final class ApplicationExitInfoHistoryDispatcher implements Runnable { private final @NotNull Context context; private final @NotNull IScopes scopes; private final @NotNull SentryAndroidOptions options; + private final @NotNull IEnvelopeCache cache; private final @NotNull ApplicationExitInfoPolicy policy; private final long threshold; @@ -56,10 +57,12 @@ final class ApplicationExitInfoHistoryDispatcher implements Runnable { final @NotNull Context context, final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options, + final @NotNull IEnvelopeCache cache, final @NotNull ApplicationExitInfoPolicy policy) { this.context = ContextUtils.getApplicationContext(context); this.scopes = scopes; this.options = options; + this.cache = cache; this.policy = policy; // ApplicationExitInfo timestamps are epoch millis, so the cutoff has to be on the wall clock. this.threshold = options.getEpochClock().now().epochMillis() - NINETY_DAYS_THRESHOLD; @@ -134,7 +137,6 @@ public void run() { } private void waitPreviousSessionFlush() { - final IEnvelopeCache cache = options.getEnvelopeDiskCache(); if (cache instanceof EnvelopeCache) { if (options.isEnableAutoSessionTracking() && !((EnvelopeCache) cache).waitPreviousSessionFlush()) { diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java index e81577cfd4b..e697a23c844 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/EnvelopeFileObserverIntegration.java @@ -4,11 +4,15 @@ import io.sentry.ILogger; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.Integration; +import io.sentry.NoOpSentryExecutorService; import io.sentry.OutboxSender; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.FileUtils; import io.sentry.util.Objects; @@ -19,7 +23,10 @@ import org.jetbrains.annotations.TestOnly; /** Watches the envelope dir. and send them (events) over. */ -public abstract class EnvelopeFileObserverIntegration implements Integration, Closeable { +public abstract class EnvelopeFileObserverIntegration implements InternalIntegration, Closeable { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private @Nullable EnvelopeFileObserver observer; private @Nullable ILogger logger; private boolean isClosed = false; @@ -30,8 +37,17 @@ public abstract class EnvelopeFileObserverIntegration implements Integration, Cl } @Override - @SuppressWarnings("FutureReturnValueIgnored") public final void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + @SuppressWarnings("FutureReturnValueIgnored") + public final void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); Objects.requireNonNull(scopes, "Scopes are required"); Objects.requireNonNull(options, "SentryOptions is required"); @@ -47,15 +63,13 @@ public final void register(final @NotNull IScopes scopes, final @NotNull SentryO SentryLevel.DEBUG, "Registering EnvelopeFileObserverIntegration for path: %s", path); try { - options - .getExecutorService() - .submit(() -> { - try (final @NotNull ISentryLifecycleToken ignored = startLock.acquire()) { - if (!isClosed) { - startOutboxSender(scopes, options, path); - } - } - }); + executorService.submit(() -> { + try (final @NotNull ISentryLifecycleToken ignored = startLock.acquire()) { + if (!isClosed) { + startOutboxSender(scopes, options, path); + } + } + }); } catch (Throwable e) { logger.log( SentryLevel.DEBUG, diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java index 677678b2438..3f9a0cad2d5 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/FeedbackShakeIntegration.java @@ -7,10 +7,14 @@ import android.app.Dialog; import android.os.Bundle; import io.sentry.IScopes; -import io.sentry.Integration; +import io.sentry.ISentryExecutorService; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SentryFeedbackOptions; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.util.Objects; import java.io.Closeable; import java.io.IOException; @@ -34,11 +38,14 @@ * suppress detection elsewhere. */ public final class FeedbackShakeIntegration - implements Integration, + implements InternalIntegration, Closeable, Application.ActivityLifecycleCallbacks, SentryFeedbackOptions.IShakeController { + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); + private final @NotNull Application application; private final @NotNull SentryShakeDetector shakeDetector; private @Nullable SentryAndroidOptions options; @@ -59,7 +66,16 @@ public FeedbackShakeIntegration(final @NotNull Application application) { } @Override - public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions sentryOptions) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions sentryOptions, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.options = Objects.requireNonNull( (sentryOptions instanceof SentryAndroidOptions) @@ -95,9 +111,7 @@ public synchronized void enableOnShake() { // access), so warm it up off the main thread. start() re-runs init() on demand, so shake // detection still works if an activity resumes before this completes. try { - options - .getExecutorService() - .submit(() -> shakeDetector.init(application, options.getLogger())); + executorService.submit(() -> shakeDetector.init(application, options.getLogger())); } catch (Throwable t) { options .getLogger() diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/MemoryLimiterIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/MemoryLimiterIntegration.java index c9f2eeb114c..b835ee8310c 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/MemoryLimiterIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/MemoryLimiterIntegration.java @@ -13,7 +13,6 @@ import io.sentry.Hint; import io.sentry.ILogger; import io.sentry.IScopes; -import io.sentry.Integration; import io.sentry.SentryBaseEvent; import io.sentry.SentryEvent; import io.sentry.SentryLevel; @@ -23,6 +22,9 @@ import io.sentry.hints.AbnormalExit; import io.sentry.hints.Backfillable; import io.sentry.hints.BlockingFlushHint; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.Mechanism; import io.sentry.protocol.Message; import io.sentry.protocol.SentryException; @@ -63,7 +65,7 @@ *

Only available on Android API ≥ 37. */ @ApiStatus.Internal -public final class MemoryLimiterIntegration implements Integration, Closeable { +public final class MemoryLimiterIntegration implements InternalIntegration, Closeable { static final @NotNull String MEMORY_LIMITER_DESCRIPTION_PREFIX = "MemoryLimiter:"; static final @NotNull String MEMORY_LIMITER_DESCRIPTION = @@ -92,8 +94,16 @@ public MemoryLimiterIntegration( } @Override - @SuppressWarnings("FutureReturnValueIgnored") public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + @SuppressWarnings("FutureReturnValueIgnored") + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { androidOptions = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -127,11 +137,15 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { } try { - options + runtime .getExecutorService() .submit( new ApplicationExitInfoHistoryDispatcher( - context, scopes, androidOptions, new MemoryLimiterPolicy(androidOptions))); + context, + scopes, + androidOptions, + runtime.getEnvelopeDiskCache(), + new MemoryLimiterPolicy(androidOptions))); } catch (Throwable e) { ExceptionUtils.rethrowIfFatal(e); options.getLogger().log(DEBUG, "Failed to start MemoryLimiter processor.", e); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java index 1f3dc572792..11aa33e8eb5 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SendCachedEnvelopeIntegration.java @@ -5,11 +5,15 @@ import io.sentry.DataCategory; import io.sentry.IConnectionStatusProvider; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.Integration; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SendCachedEnvelopeFireAndForgetIntegration; import io.sentry.SentryLevel; import io.sentry.SentryOptions; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.transport.RateLimiter; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.LazyEvaluator; @@ -25,7 +29,10 @@ import org.jetbrains.annotations.Nullable; final class SendCachedEnvelopeIntegration - implements Integration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable { + implements InternalIntegration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private final @NotNull SendCachedEnvelopeFireAndForgetIntegration.SendFireAndForgetFactory factory; @@ -47,7 +54,16 @@ public SendCachedEnvelopeIntegration( } @Override - public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( @@ -87,60 +103,54 @@ private void sendCachedEnvelopes( final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { final Future future = - options - .getExecutorService() - .submit(() -> { - try { - if (isClosed.get()) { - options - .getLogger() - .log( - SentryLevel.INFO, - "SendCachedEnvelopeIntegration, not trying to send after closing."); - return; - } - - if (!isInitialized.getAndSet(true)) { - connectionStatusProvider = options.getConnectionStatusProvider(); - connectionStatusProvider.addConnectionStatusObserver(this); - - sender = factory.create(scopes, options); - } - - if (connectionStatusProvider != null - && connectionStatusProvider.getConnectionStatus() - == IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) { - options - .getLogger() - .log(SentryLevel.INFO, "SendCachedEnvelopeIntegration, no connection."); - return; - } - - // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); - if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { - options - .getLogger() - .log( - SentryLevel.INFO, - "SendCachedEnvelopeIntegration, rate limiting active."); - return; - } - - if (sender == null) { - options - .getLogger() - .log(SentryLevel.ERROR, "SendCachedEnvelopeIntegration factory is null."); - return; - } - - sender.send(); - } catch (Throwable e) { - options - .getLogger() - .log(SentryLevel.ERROR, "Failed trying to send cached events.", e); - } - }); + executorService.submit(() -> { + try { + if (isClosed.get()) { + options + .getLogger() + .log( + SentryLevel.INFO, + "SendCachedEnvelopeIntegration, not trying to send after closing."); + return; + } + + if (!isInitialized.getAndSet(true)) { + connectionStatusProvider = options.getConnectionStatusProvider(); + connectionStatusProvider.addConnectionStatusObserver(this); + + sender = factory.create(scopes, options); + } + + if (connectionStatusProvider != null + && connectionStatusProvider.getConnectionStatus() + == IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) { + options + .getLogger() + .log(SentryLevel.INFO, "SendCachedEnvelopeIntegration, no connection."); + return; + } + + // in case there's rate limiting active, skip processing + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); + if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { + options + .getLogger() + .log(SentryLevel.INFO, "SendCachedEnvelopeIntegration, rate limiting active."); + return; + } + + if (sender == null) { + options + .getLogger() + .log(SentryLevel.ERROR, "SendCachedEnvelopeIntegration factory is null."); + return; + } + + sender.send(); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Failed trying to send cached events.", e); + } + }); // startupCrashMarkerEvaluator remains true on subsequent runs, let's ensure we only block on // the very first execution (=app start) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java index b3caf622c37..95cb7d94aac 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/SystemEventsBreadcrumbsIntegration.java @@ -31,11 +31,15 @@ import io.sentry.Breadcrumb; import io.sentry.Hint; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.Integration; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.android.core.internal.util.Debouncer; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.Objects; import io.sentry.util.StringUtils; @@ -52,7 +56,10 @@ import org.jetbrains.annotations.TestOnly; public final class SystemEventsBreadcrumbsIntegration - implements Integration, Closeable, AppState.AppStateListener { + implements InternalIntegration, Closeable, AppState.AppStateListener { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private final @NotNull Context context; @@ -99,6 +106,15 @@ public SystemEventsBreadcrumbsIntegration( @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull( @@ -135,54 +151,51 @@ private void registerReceiver( } try { - options - .getExecutorService() - .submit(() -> { - try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) { - if (isClosed || isStopped || receiver != null) { - return; - } + executorService.submit(() -> { + try (final @NotNull ISentryLifecycleToken ignored = receiverLock.acquire()) { + if (isClosed || isStopped || receiver != null) { + return; + } - receiver = new SystemEventsBroadcastReceiver(scopes, options); - final @NotNull IntentFilter filter = new IntentFilter(); - for (String item : actions) { - filter.addAction(item); - } - try { - // registerReceiver can throw SecurityException but it's not documented in the - // official docs - - // onReceive will be called on this handler thread - @NotNull Handler handler; - if (customHandler != null) { - handler = customHandler; - } else { - if (handlerThread == null) { - handlerThread = - new HandlerThread( - "SystemEventsReceiver", Process.THREAD_PRIORITY_BACKGROUND); - handlerThread.start(); - } - handler = new Handler(handlerThread.getLooper()); - } - ContextUtils.registerReceiver(context, options, receiver, filter, handler); - if (!isReceiverRegistered.getAndSet(true)) { - options - .getLogger() - .log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed."); - addIntegrationToSdkVersion("SystemEventsBreadcrumbs"); - } - } catch (Throwable e) { - options.setEnableSystemEventBreadcrumbs(false); - options - .getLogger() - .log( - SentryLevel.ERROR, - "Failed to initialize SystemEventsBreadcrumbsIntegration.", - e); + receiver = new SystemEventsBroadcastReceiver(scopes, options); + final @NotNull IntentFilter filter = new IntentFilter(); + for (String item : actions) { + filter.addAction(item); + } + try { + // registerReceiver can throw SecurityException but it's not documented in the + // official docs + + // onReceive will be called on this handler thread + @NotNull Handler handler; + if (customHandler != null) { + handler = customHandler; + } else { + if (handlerThread == null) { + handlerThread = + new HandlerThread("SystemEventsReceiver", Process.THREAD_PRIORITY_BACKGROUND); + handlerThread.start(); } + handler = new Handler(handlerThread.getLooper()); } - }); + ContextUtils.registerReceiver(context, options, receiver, filter, handler); + if (!isReceiverRegistered.getAndSet(true)) { + options + .getLogger() + .log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed."); + addIntegrationToSdkVersion("SystemEventsBreadcrumbs"); + } + } catch (Throwable e) { + options.setEnableSystemEventBreadcrumbs(false); + options + .getLogger() + .log( + SentryLevel.ERROR, + "Failed to initialize SystemEventsBreadcrumbsIntegration.", + e); + } + } + }); } catch (Throwable e) { options .getLogger() @@ -203,7 +216,7 @@ private void scheduleUnregisterReceiver() { } try { - options.getExecutorService().submit(() -> unregisterReceiver(options)); + executorService.submit(() -> unregisterReceiver(options)); } catch (RejectedExecutionException e) { unregisterReceiver(options); } diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/TombstoneIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/TombstoneIntegration.java index d3b16aa3fc5..1c1cfa417e6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/TombstoneIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/TombstoneIntegration.java @@ -12,7 +12,6 @@ import io.sentry.Hint; import io.sentry.ILogger; import io.sentry.IScopes; -import io.sentry.Integration; import io.sentry.SentryEnvelope; import io.sentry.SentryEnvelopeItem; import io.sentry.SentryEvent; @@ -28,6 +27,9 @@ import io.sentry.hints.Backfillable; import io.sentry.hints.BlockingFlushHint; import io.sentry.hints.NativeCrashExit; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.protocol.DebugMeta; import io.sentry.protocol.Mechanism; import io.sentry.protocol.SentryException; @@ -47,7 +49,7 @@ import org.jetbrains.annotations.Nullable; @ApiStatus.Internal -public final class TombstoneIntegration implements Integration, Closeable { +public final class TombstoneIntegration implements InternalIntegration, Closeable { private final @NotNull Context context; private @Nullable SentryAndroidOptions options; @@ -56,8 +58,16 @@ public TombstoneIntegration(final @NotNull Context context) { } @Override - @SuppressWarnings("FutureReturnValueIgnored") public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + @SuppressWarnings("FutureReturnValueIgnored") + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -79,13 +89,14 @@ public void register(@NotNull IScopes scopes, @NotNull SentryOptions options) { } try { - options + runtime .getExecutorService() .submit( new ApplicationExitInfoHistoryDispatcher( context, scopes, this.options, + runtime.getEnvelopeDiskCache(), new TombstonePolicy(this.options, this.context))); } catch (Throwable e) { options.getLogger().log(SentryLevel.DEBUG, "Failed to start tombstone processor.", e); diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java index 018fd49f6fd..038432cd90e 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/anr/AnrProfilingIntegration.java @@ -7,13 +7,17 @@ import io.sentry.IDebugManager; import io.sentry.ILogger; import io.sentry.IScopes; +import io.sentry.ISentryExecutorService; import io.sentry.ISentryLifecycleToken; -import io.sentry.Integration; import io.sentry.NoOpLogger; +import io.sentry.NoOpSentryExecutorService; import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.android.core.AppState; import io.sentry.android.core.SentryAndroidOptions; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.time.MonotonicTicker; import io.sentry.time.Stopwatch; import io.sentry.util.AutoClosableReentrantLock; @@ -38,7 +42,10 @@ * io.sentry.anr.profiling.sample-rate} manifest metadata. */ public final class AnrProfilingIntegration - implements Integration, Closeable, AppState.AppStateListener, Runnable { + implements InternalIntegration, Closeable, AppState.AppStateListener, Runnable { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); public static final long POLLING_INTERVAL_MS = 66; private static final long THRESHOLD_SUSPICION_MS = 1000; @@ -76,6 +83,15 @@ public final class AnrProfilingIntegration @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.options = Objects.requireNonNull( (options instanceof SentryAndroidOptions) ? (SentryAndroidOptions) options : null, @@ -131,18 +147,17 @@ public void close() throws IOException { } if (opts != null) { try { - opts.getExecutorService() - .submit(() -> { - if (pm == null) { - return; - } + executorService.submit(() -> { + if (pm == null) { + return; + } - try { - pm.close(); - } catch (IOException e) { - logger.log(SentryLevel.WARNING, "Failed to close AnrProfileManager"); - } - }); + try { + pm.close(); + } catch (IOException e) { + logger.log(SentryLevel.WARNING, "Failed to close AnrProfileManager"); + } + }); } catch (Throwable e) { logger.log(SentryLevel.WARNING, "Failed to submit AnrProfileManager close"); } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt index 5d43c28ede3..53d82680cdf 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/AnrV2IntegrationTest.kt @@ -171,7 +171,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes) .captureEvent( @@ -188,7 +188,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes) .captureEvent( @@ -210,7 +210,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), check { assertNotNull(it.threadDump) }) } @@ -222,7 +222,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { .writeText(newTimestamp.toString()) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @@ -232,7 +232,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addTrace = false) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -242,7 +242,7 @@ class AnrV2IntegrationTest : ApplicationExitIntegrationTestBase() { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addBadTrace = true) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitInfoCrossIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitInfoCrossIntegrationTest.kt index 560ef5211a1..001829d5e42 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitInfoCrossIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitInfoCrossIntegrationTest.kt @@ -10,6 +10,7 @@ import io.sentry.ILogger import io.sentry.IScopes import io.sentry.SentryEvent import io.sentry.cache.EnvelopeCache +import io.sentry.internal.SentryRuntime import io.sentry.protocol.SentryId import io.sentry.test.ImmediateExecutorService import java.io.File @@ -52,6 +53,12 @@ class ApplicationExitInfoCrossIntegrationTest { shadowActivityManager = Shadow.extract(activityManager) } + fun newRuntime(options: SentryAndroidOptions): SentryRuntime = + SentryRuntime.builder() + .executorService(ImmediateExecutorService()) + .envelopeDiskCache(EnvelopeCache.create(options)) + .buildForTest() + fun newOptions( dir: File, configure: SentryAndroidOptions.() -> Unit, @@ -65,8 +72,6 @@ class ApplicationExitInfoCrossIntegrationTest { // Nothing ever releases these latches here, so every wait would run to its timeout. flushTimeoutMillis = 0L sessionFlushTimeoutMillis = 0L - executorService = ImmediateExecutorService() - setEnvelopeDiskCache(EnvelopeCache.create(this)) addInAppInclude("io.sentry.samples") configure() } @@ -170,7 +175,7 @@ class ApplicationExitInfoCrossIntegrationTest { val buildInfoProvider = mock() whenever(buildInfoProvider.sdkInfoVersion).thenReturn(37) MemoryLimiterIntegration(fixture.context, buildInfoProvider) - .register(memoryLimiterScopes, memoryLimiterOptions) + .register(memoryLimiterScopes, memoryLimiterOptions, fixture.newRuntime(memoryLimiterOptions)) verify(memoryLimiterScopes) .captureEvent( @@ -190,7 +195,8 @@ class ApplicationExitInfoCrossIntegrationTest { isAnrEnabled = true isReportHistoricalAnrs = true } - AnrV2Integration(fixture.context).register(anrScopes, anrOptions) + AnrV2Integration(fixture.context) + .register(anrScopes, anrOptions, fixture.newRuntime(anrOptions)) verify(anrScopes) .captureEvent( @@ -221,7 +227,7 @@ class ApplicationExitInfoCrossIntegrationTest { val buildInfoProvider = mock() whenever(buildInfoProvider.sdkInfoVersion).thenReturn(37) MemoryLimiterIntegration(fixture.context, buildInfoProvider) - .register(memoryLimiterScopes, memoryLimiterOptions) + .register(memoryLimiterScopes, memoryLimiterOptions, fixture.newRuntime(memoryLimiterOptions)) verify(memoryLimiterScopes) .captureEvent( @@ -241,7 +247,8 @@ class ApplicationExitInfoCrossIntegrationTest { isTombstoneEnabled = true isReportHistoricalTombstones = true } - TombstoneIntegration(fixture.context).register(tombstoneScopes, tombstoneOptions) + TombstoneIntegration(fixture.context) + .register(tombstoneScopes, tombstoneOptions, fixture.newRuntime(tombstoneOptions)) verify(tombstoneScopes) .captureEvent( diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitIntegrationTestBase.kt b/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitIntegrationTestBase.kt index 30214fe52c1..bcff425d29d 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitIntegrationTestBase.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/ApplicationExitIntegrationTestBase.kt @@ -13,6 +13,8 @@ import io.sentry.SentryLevel import io.sentry.cache.EnvelopeCache import io.sentry.hints.DiskFlushNotification import io.sentry.hints.SessionStartHint +import io.sentry.internal.InternalIntegration +import io.sentry.internal.SentryRuntime import io.sentry.protocol.SentryId import io.sentry.test.ImmediateExecutorService import io.sentry.util.HintUtils @@ -64,25 +66,25 @@ abstract class ApplicationExitIntegrationTestBase { fun `when cacheDir is not set, does not process historical exits`() { val integration = fixture.getSut(null, useImmediateExecutorService = false) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test fun `when integration is not enabled, does not process historical exits`() { val integration = fixture.getSut(tmpDir, enabled = false, useImmediateExecutorService = false) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test fun `when historical exit list is empty, does not process historical exits`() { val integration = fixture.getSut(tmpDir) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -92,7 +94,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(reason = null) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -106,7 +108,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -116,7 +118,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -126,7 +128,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = null) fixture.addAppExitInfo(timestamp = oldTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @@ -136,7 +138,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes) .captureEvent( @@ -167,7 +169,7 @@ abstract class ApplicationExitIntegrationTestBase { SentryId() } - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) verify(fixture.logger, never()) @@ -180,7 +182,7 @@ abstract class ApplicationExitIntegrationTestBase { fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp, lastEventId = SentryId.EMPTY_ID) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) verify(fixture.logger, never()) @@ -193,7 +195,7 @@ abstract class ApplicationExitIntegrationTestBase { fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp, lastEventId = SentryId.EMPTY_ID) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) assertEquals(newTimestamp.toString(), fixture.lastReportedFile.readText()) } @@ -209,7 +211,7 @@ abstract class ApplicationExitIntegrationTestBase { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) assertEquals(oldTimestamp.toString(), fixture.lastReportedFile.readText()) } @@ -221,7 +223,7 @@ abstract class ApplicationExitIntegrationTestBase { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, times(2)) .captureEvent( @@ -241,7 +243,7 @@ abstract class ApplicationExitIntegrationTestBase { fixture.addAppExitInfo(timestamp = newTimestamp - 1 * 60 * 1000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, atMost(1)) .captureEvent( @@ -260,7 +262,7 @@ abstract class ApplicationExitIntegrationTestBase { fixture.addAppExitInfo(timestamp = newTimestamp - TimeUnit.DAYS.toMillis(1)) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) inOrder(fixture.scopes) { verify(fixture.scopes) @@ -283,7 +285,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes) .captureEvent( @@ -304,13 +306,13 @@ abstract class ApplicationExitIntegrationTestBase { thread { Thread.sleep(200L) val sessionHint = HintUtils.createWithTypeCheckHint(SessionStartHint()) - fixture.options.envelopeDiskCache.storeEnvelope( + fixture.runtime.envelopeDiskCache.storeEnvelope( SentryEnvelope(SentryId.EMPTY_ID, null, emptyList()), sessionHint, ) } - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.logger, never()) .log( @@ -330,7 +332,7 @@ abstract class ApplicationExitIntegrationTestBase { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.logger, never()) .log( @@ -346,14 +348,14 @@ abstract class ApplicationExitIntegrationTestBase { fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp, sessionFlushTimeoutMillis = 500L) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.logger) .log( any(), argThat { startsWith("Timed out waiting to flush previous session to its own file.") }, ) - assertTrue((fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush()) + assertTrue((fixture.runtime.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush()) } @Test @@ -361,7 +363,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addTrace = false) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -371,7 +373,7 @@ abstract class ApplicationExitIntegrationTestBase { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = oldTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp, addBadTrace = true) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -412,6 +414,11 @@ abstract class ApplicationExitIntegrationTestBase { val options = SentryAndroidOptions() val scopes = mock() val logger = mock() + lateinit var runtime: SentryRuntime + + fun register(integration: Integration) { + (integration as InternalIntegration).register(scopes, options, runtime) + } fun init(appContext: Context) { context = appContext @@ -436,16 +443,21 @@ abstract class ApplicationExitIntegrationTestBase { setLogger(this@ApplicationExitTestFixture.logger) isDebug = true cacheDirPath = dir?.newFolder()?.absolutePath - executorService = if (useImmediateExecutorService) ImmediateExecutorService() else mock() config.setEnabledFlag(this, enabled) this.flushTimeoutMillis = flushTimeoutMillis this.sessionFlushTimeoutMillis = sessionFlushTimeoutMillis this.isEnableAutoSessionTracking = sessionTrackingEnabled config.setReportHistoricalFlag(this, reportHistorical) addInAppInclude("io.sentry.samples") - setEnvelopeDiskCache(EnvelopeCache.create(this)) extraOptions(this) } + runtime = + SentryRuntime.builder() + .executorService( + if (useImmediateExecutorService) ImmediateExecutorService() else mock(), + ) + .envelopeDiskCache(EnvelopeCache.create(options)) + .buildForTest() options.cacheDirPath?.let { cacheDir -> File(cacheDir).mkdirs() lastReportedFile = File(cacheDir, config.lastReportedFileName) diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/MemoryLimiterIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/MemoryLimiterIntegrationTest.kt index f0227d91639..f6e2f7ae058 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/MemoryLimiterIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/MemoryLimiterIntegrationTest.kt @@ -13,6 +13,7 @@ import io.sentry.SentryLevel import io.sentry.android.core.MemoryLimiterIntegration.MemoryLimiterHint import io.sentry.android.core.cache.AndroidEnvelopeCache import io.sentry.cache.EnvelopeCache +import io.sentry.internal.SentryRuntime import io.sentry.protocol.SentryId import io.sentry.test.ImmediateExecutorService import io.sentry.util.HintUtils @@ -55,6 +56,11 @@ class MemoryLimiterIntegrationTest { val options = SentryAndroidOptions() val scopes = mock() val logger = mock() + lateinit var runtime: SentryRuntime + + fun register(integration: MemoryLimiterIntegration) { + integration.register(scopes, options, runtime) + } fun init(appContext: Context) { context = appContext @@ -79,11 +85,16 @@ class MemoryLimiterIntegrationTest { // Nothing ever releases these latches here, so every wait would run to its timeout. flushTimeoutMillis = 0L sessionFlushTimeoutMillis = 0L - executorService = if (useImmediateExecutorService) ImmediateExecutorService() else mock() isMemoryLimiterEnabled = memoryLimiterEnabled isReportHistoricalMemoryLimiterExits = reportHistoricalMemoryLimiterExits - setEnvelopeDiskCache(EnvelopeCache.create(this)) } + runtime = + SentryRuntime.builder() + .executorService( + if (useImmediateExecutorService) ImmediateExecutorService() else mock(), + ) + .envelopeDiskCache(EnvelopeCache.create(options)) + .buildForTest() options.cacheDirPath?.let { cacheDirPath -> val cacheDir = File(cacheDirPath).also { it.mkdirs() } @@ -145,9 +156,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test @@ -160,9 +171,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test @@ -175,9 +186,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test @@ -190,9 +201,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService).submit(any()) + verify(fixture.runtime.executorService).submit(any()) } @Test @@ -205,9 +216,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService).submit(any()) + verify(fixture.runtime.executorService).submit(any()) } // "Explicit records" are ApplicationExitInfo instances that use @@ -222,7 +233,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @@ -248,7 +259,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) val expectedMessage = MemoryLimiterIntegration.MEMORY_LIMITER_MESSAGE_PREFIX + " (importance: foreground)" @@ -312,7 +323,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes) .captureEvent( @@ -334,7 +345,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -348,7 +359,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -364,7 +375,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -378,7 +389,7 @@ class MemoryLimiterIntegrationTest { timestamp = newTimestamp, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -397,7 +408,7 @@ class MemoryLimiterIntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1_000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) inOrder(fixture.scopes) { verify(fixture.scopes) @@ -434,7 +445,7 @@ class MemoryLimiterIntegrationTest { fixture.addAppExitInfo(timestamp = reportedHistoricalTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()) .captureEvent(argThat { timestamp!!.time == skippedHistoricalTimestamp }, anyOrNull()) @@ -466,7 +477,7 @@ class MemoryLimiterIntegrationTest { fixture.addAppExitInfo(timestamp = newTimestamp - 1_000) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) // Verify we report only the latest MemoryLimiter exit, and none before it. verify(fixture.scopes, atMost(1)).captureEvent(any(), anyOrNull()) @@ -484,9 +495,9 @@ class MemoryLimiterIntegrationTest { useImmediateExecutorService = false, ) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) - verify(fixture.options.executorService, never()).submit(any()) + verify(fixture.runtime.executorService, never()).submit(any()) } @Test @@ -494,7 +505,7 @@ class MemoryLimiterIntegrationTest { val integration = fixture.getSut(tmpDir, lastReportedTimestamp = newTimestamp) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes, never()).captureEvent(any(), anyOrNull()) } @@ -509,7 +520,7 @@ class MemoryLimiterIntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) } @@ -524,7 +535,7 @@ class MemoryLimiterIntegrationTest { ) fixture.addAppExitInfo(timestamp = newTimestamp) - integration.register(fixture.scopes, fixture.options) + fixture.register(integration) verify(fixture.scopes).captureEvent(any(), anyOrNull()) } diff --git a/sentry-android-core/src/test/java/io/sentry/android/core/TombstoneIntegrationTest.kt b/sentry-android-core/src/test/java/io/sentry/android/core/TombstoneIntegrationTest.kt index 83514adc260..04c6a7004c2 100644 --- a/sentry-android-core/src/test/java/io/sentry/android/core/TombstoneIntegrationTest.kt +++ b/sentry-android-core/src/test/java/io/sentry/android/core/TombstoneIntegrationTest.kt @@ -107,7 +107,7 @@ class TombstoneIntegrationTest : ApplicationExitIntegrationTestBase { this.tombstone == null }) } @@ -144,7 +144,7 @@ class TombstoneIntegrationTest : ApplicationExitIntegrationTestBase()) } @@ -163,7 +163,7 @@ class TombstoneIntegrationTest : ApplicationExitIntegrationTestBase (Lio/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForgetFactory;)V public fun close ()V public fun onConnectionStatusChanged (Lio/sentry/IConnectionStatusProvider$ConnectionStatus;)V public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V + public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;Lio/sentry/internal/SentryRuntime;)V } public abstract interface class io/sentry/SendCachedEnvelopeFireAndForgetIntegration$SendFireAndForget { diff --git a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java index 586a9d6c831..f7e9dae0c0b 100644 --- a/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java +++ b/sentry/src/main/java/io/sentry/SendCachedEnvelopeFireAndForgetIntegration.java @@ -2,6 +2,9 @@ import static io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion; +import io.sentry.internal.InternalIntegration; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.transport.RateLimiter; import io.sentry.util.AutoClosableReentrantLock; import io.sentry.util.Objects; @@ -15,7 +18,10 @@ /** Sends cached events over when your App is starting or a network connection is present. */ public final class SendCachedEnvelopeFireAndForgetIntegration - implements Integration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable { + implements InternalIntegration, IConnectionStatusProvider.IConnectionStatusObserver, Closeable { + + private volatile @NotNull ISentryExecutorService executorService = + NoOpSentryExecutorService.getInstance(); private final @NotNull SendFireAndForgetFactory factory; private @Nullable IConnectionStatusProvider connectionStatusProvider; @@ -69,6 +75,15 @@ public SendCachedEnvelopeFireAndForgetIntegration( @Override public void register(final @NotNull IScopes scopes, final @NotNull SentryOptions options) { + register(scopes, options, LegacyOptionsBridge.runtimeOf(options)); + } + + @Override + public void register( + final @NotNull IScopes scopes, + final @NotNull SentryOptions options, + final @NotNull SentryRuntime runtime) { + this.executorService = runtime.getExecutorService(); this.scopes = Objects.requireNonNull(scopes, "Scopes are required"); this.options = Objects.requireNonNull(options, "SentryOptions is required"); @@ -108,59 +123,56 @@ public void onConnectionStatusChanged( private void sendCachedEnvelopes( final @NotNull IScopes scopes, final @NotNull SentryOptions options) { try (final @NotNull ISentryLifecycleToken ignored = lock.acquire()) { - options - .getExecutorService() - .submit(() -> { - try { - if (isClosed.get()) { - options - .getLogger() - .log( - SentryLevel.INFO, - "SendCachedEnvelopeFireAndForgetIntegration, not trying to send after closing."); - return; - } - - if (!isInitialized.getAndSet(true)) { - connectionStatusProvider = options.getConnectionStatusProvider(); - connectionStatusProvider.addConnectionStatusObserver(this); - - sender = factory.create(scopes, options); - } - - // skip run only if we're certainly disconnected - if (connectionStatusProvider != null - && connectionStatusProvider.getConnectionStatus() - == IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) { - options - .getLogger() - .log( - SentryLevel.INFO, - "SendCachedEnvelopeFireAndForgetIntegration, no connection."); - return; - } - - // in case there's rate limiting active, skip processing - final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); - if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { - options - .getLogger() - .log( - SentryLevel.INFO, - "SendCachedEnvelopeFireAndForgetIntegration, rate limiting active."); - return; - } - - if (sender == null) { - options.getLogger().log(SentryLevel.ERROR, "SendFireAndForget factory is null."); - return; - } - - sender.send(); - } catch (Throwable e) { - options.getLogger().log(SentryLevel.ERROR, "Failed trying to send cached events.", e); - } - }); + executorService.submit(() -> { + try { + if (isClosed.get()) { + options + .getLogger() + .log( + SentryLevel.INFO, + "SendCachedEnvelopeFireAndForgetIntegration, not trying to send after closing."); + return; + } + + if (!isInitialized.getAndSet(true)) { + connectionStatusProvider = options.getConnectionStatusProvider(); + connectionStatusProvider.addConnectionStatusObserver(this); + + sender = factory.create(scopes, options); + } + + // skip run only if we're certainly disconnected + if (connectionStatusProvider != null + && connectionStatusProvider.getConnectionStatus() + == IConnectionStatusProvider.ConnectionStatus.DISCONNECTED) { + options + .getLogger() + .log( + SentryLevel.INFO, "SendCachedEnvelopeFireAndForgetIntegration, no connection."); + return; + } + + // in case there's rate limiting active, skip processing + final @Nullable RateLimiter rateLimiter = scopes.getRateLimiter(); + if (rateLimiter != null && rateLimiter.isActiveForCategory(DataCategory.All)) { + options + .getLogger() + .log( + SentryLevel.INFO, + "SendCachedEnvelopeFireAndForgetIntegration, rate limiting active."); + return; + } + + if (sender == null) { + options.getLogger().log(SentryLevel.ERROR, "SendFireAndForget factory is null."); + return; + } + + sender.send(); + } catch (Throwable e) { + options.getLogger().log(SentryLevel.ERROR, "Failed trying to send cached events.", e); + } + }); } catch (RejectedExecutionException e) { options .getLogger()