diff --git a/sentry/api/sentry.api b/sentry/api/sentry.api index 7dde4d7bf7..df40665ae3 100644 --- a/sentry/api/sentry.api +++ b/sentry/api/sentry.api @@ -5472,6 +5472,7 @@ public final class io/sentry/internal/ManifestVersionReader$VersionInfoHolder { public final class io/sentry/internal/SentryRuntime { public static fun builder ()Lio/sentry/internal/SentryRuntime$Builder; + public fun close (ZJLio/sentry/ILogger;)V public static fun getCurrent ()Lio/sentry/internal/SentryRuntime; public fun getEnvelopeDiskCache ()Lio/sentry/cache/IEnvelopeCache; public fun getExecutorService ()Lio/sentry/ISentryExecutorService; diff --git a/sentry/src/main/java/io/sentry/Scopes.java b/sentry/src/main/java/io/sentry/Scopes.java index 30df253869..0a76b0656a 100644 --- a/sentry/src/main/java/io/sentry/Scopes.java +++ b/sentry/src/main/java/io/sentry/Scopes.java @@ -3,6 +3,8 @@ import io.sentry.clientreport.DiscardReason; import io.sentry.hints.SessionEndHint; import io.sentry.hints.SessionStartHint; +import io.sentry.internal.LegacyOptionsBridge; +import io.sentry.internal.SentryRuntime; import io.sentry.logger.ILoggerApi; import io.sentry.logger.LoggerApi; import io.sentry.metrics.IMetricsApi; @@ -15,7 +17,6 @@ import io.sentry.util.TracingUtils; import java.io.Closeable; import java.util.List; -import java.util.concurrent.RejectedExecutionException; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -28,6 +29,8 @@ public final class Scopes implements IScopes { private final @Nullable Scopes parentScopes; + private final @Nullable SentryRuntime runtime; + private final @NotNull String creator; private final @NotNull CompositePerformanceCollector compositePerformanceCollector; @@ -41,7 +44,16 @@ public Scopes( final @NotNull IScope isolationScope, final @NotNull IScope globalScope, final @NotNull String creator) { - this(scope, isolationScope, globalScope, null, creator); + this(scope, isolationScope, globalScope, null, null, creator); + } + + Scopes( + final @NotNull IScope scope, + final @NotNull IScope isolationScope, + final @NotNull IScope globalScope, + final @NotNull SentryRuntime runtime, + final @NotNull String creator) { + this(scope, isolationScope, globalScope, null, runtime, creator); } private Scopes( @@ -49,12 +61,14 @@ private Scopes( final @NotNull IScope isolationScope, final @NotNull IScope globalScope, final @Nullable Scopes parentScopes, + final @Nullable SentryRuntime runtime, final @NotNull String creator) { this.combinedScope = new CombinedScopeView(globalScope, isolationScope, scope); this.scope = scope; this.isolationScope = isolationScope; this.globalScope = globalScope; this.parentScopes = parentScopes; + this.runtime = runtime; this.creator = creator; final @NotNull SentryOptions options = getOptions(); @@ -113,12 +127,12 @@ public boolean isAncestorOf(final @Nullable IScopes otherScopes) { @Override public @NotNull IScopes forkedScopes(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope.clone(), globalScope, this, creator); + return new Scopes(scope.clone(), isolationScope.clone(), globalScope, this, runtime, creator); } @Override public @NotNull IScopes forkedCurrentScope(final @NotNull String creator) { - return new Scopes(scope.clone(), isolationScope, globalScope, this, creator); + return new Scopes(scope.clone(), isolationScope, globalScope, this, runtime, creator); } @Override @@ -476,23 +490,11 @@ public void close(final boolean isRestarting) { if (!isRestarting) { getOptions().getTimerExecutorService().close(getOptions().getShutdownTimeoutMillis()); } - final @NotNull ISentryExecutorService executorService = getOptions().getExecutorService(); - if (isRestarting) { - try { - executorService.submit( - () -> executorService.close(getOptions().getShutdownTimeoutMillis())); - } catch (RejectedExecutionException e) { - getOptions() - .getLogger() - .log( - SentryLevel.WARNING, - "Failed to submit executor service shutdown task during restart. Shutting down synchronously.", - e); - executorService.close(getOptions().getShutdownTimeoutMillis()); - } - } else { - executorService.close(getOptions().getShutdownTimeoutMillis()); - } + // A Scopes created without a runtime gets the collaborators on its options. + final @NotNull SentryRuntime runtime = + this.runtime != null ? this.runtime : LegacyOptionsBridge.runtimeOf(getOptions()); + runtime.close( + isRestarting, getOptions().getShutdownTimeoutMillis(), getOptions().getLogger()); // TODO: should we end session before closing client? configureScope(ScopeType.CURRENT, scope -> scope.getClient().close(isRestarting)); diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index aa8fdff4eb..42654efdf3 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -372,22 +372,23 @@ private static void init( globalScope.replaceOptions(options); + // Before the runtime, so that what it creates logs to the logger the customer configured. + initLogger(options); + // Also replaces an executor that a previous init closed, when the options are reused. + final @NotNull SentryRuntime runtime = + LegacyOptionsBridge.buildRuntime(runtimeBuilder, options); + SentryRuntime.setCurrent(runtime); + final IScope rootScope = new Scope(options); final IScope rootIsolationScope = new Scope(options); - rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init"); + rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, runtime, "Sentry.init"); - initLogger(options); initForOpenTelemetryMaybe(options); getScopesStorage().set(rootScopes); initConfigurations(options); globalScope.bindClient(new SentryClient(options)); - // Also replaces an executor that a previous init closed, when the options are reused. - final @NotNull SentryRuntime runtime = - LegacyOptionsBridge.buildRuntime(runtimeBuilder, options); - SentryRuntime.setCurrent(runtime); - if (options.getTimerExecutorService().isClosed()) { options.setTimerExecutorService( new SentryExecutorService( diff --git a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java index 9a9dc67301..194b3d5a32 100644 --- a/sentry/src/main/java/io/sentry/internal/SentryRuntime.java +++ b/sentry/src/main/java/io/sentry/internal/SentryRuntime.java @@ -1,14 +1,17 @@ package io.sentry.internal; +import io.sentry.ILogger; import io.sentry.ISentryExecutorService; import io.sentry.ISerializer; import io.sentry.JsonSerializer; import io.sentry.SentryExecutorService; +import io.sentry.SentryLevel; import io.sentry.SentryOptions; import io.sentry.cache.EnvelopeCache; import io.sentry.cache.IEnvelopeCache; import io.sentry.transport.NoOpEnvelopeCache; import io.sentry.util.LazyEvaluator; +import java.util.concurrent.RejectedExecutionException; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -64,6 +67,31 @@ public static void setCurrent(final @Nullable SentryRuntime runtime) { return require(envelopeDiskCache, "envelopeDiskCache"); } + /** + * Shuts down what this runtime created. On restart the executor finishes its queued work first, + * because the previous init may still have tasks on it. + */ + @SuppressWarnings("FutureReturnValueIgnored") + public void close( + final boolean isRestarting, final long shutdownTimeoutMillis, final @NotNull ILogger logger) { + if (executorService == null) { + return; + } + if (isRestarting) { + try { + executorService.submit(() -> executorService.close(shutdownTimeoutMillis)); + } catch (RejectedExecutionException e) { + logger.log( + SentryLevel.WARNING, + "Failed to submit executor service shutdown task during restart. Shutting down synchronously.", + e); + executorService.close(shutdownTimeoutMillis); + } + } else { + executorService.close(shutdownTimeoutMillis); + } + } + private static @NotNull T require(final @Nullable T value, final @NotNull String name) { if (value == null) { // Only a runtime from buildForTest() has unset collaborators. diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 24406d541b..4b09bc1a63 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -823,20 +823,22 @@ class SentryTest { } @Test - fun `init does not throw on executor shut down`() { + fun `init replaces a shut down executor before using it`() { val logger = mock() + val closed = SentryExecutorService() + closed.close(0) initForTest { it.dsn = dsn it.profilesSampleRate = 1.0 it.cacheDirPath = getTempPath() it.setLogger(logger) - it.executorService = SentryExecutorService() - it.executorService.close(0) + it.executorService = closed it.isDebug = true } - verify(logger) + assertNotSame(closed, Sentry.getCurrentScopes().options.executorService) + verify(logger, never()) .log( eq(SentryLevel.ERROR), eq( diff --git a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt index aba6b02a6a..f66256b7d5 100644 --- a/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt +++ b/sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt @@ -3,14 +3,20 @@ package io.sentry.internal import com.google.common.truth.Truth.assertThat import io.sentry.ISentryExecutorService import io.sentry.ISerializer +import io.sentry.NoOpLogger import io.sentry.SentryOptions import io.sentry.cache.EnvelopeCache import io.sentry.cache.IEnvelopeCache +import io.sentry.test.ImmediateExecutorService import io.sentry.transport.NoOpEnvelopeCache import java.nio.file.Files import kotlin.test.Test import kotlin.test.assertFailsWith +import org.mockito.kotlin.any +import org.mockito.kotlin.inOrder import org.mockito.kotlin.mock +import org.mockito.kotlin.spy +import org.mockito.kotlin.verify import org.mockito.kotlin.whenever class SentryRuntimeTest { @@ -51,6 +57,30 @@ class SentryRuntimeTest { assertThat(created).isEqualTo(1) } + @Test + fun `close shuts the executor down`() { + val executor = mock() + val runtime = SentryRuntime.builder().executorService(executor).buildForTest() + + runtime.close(false, 123, NoOpLogger.getInstance()) + + verify(executor).close(123) + } + + @Test + fun `close on restart lets the executor finish its queued work first`() { + val executor = ImmediateExecutorService() + val closing = spy(executor) + val runtime = SentryRuntime.builder().executorService(closing).buildForTest() + + runtime.close(true, 123, NoOpLogger.getInstance()) + + inOrder(closing) { + verify(closing).submit(any()) + verify(closing).close(123) + } + } + @Test fun `a test runtime fails on a collaborator the test didn't set`() { val runtime = SentryRuntime.builder().envelopeDiskCache(mock()).buildForTest()