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 sentry/api/sentry.api
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
44 changes: 23 additions & 21 deletions sentry/src/main/java/io/sentry/Scopes.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand All @@ -41,20 +44,31 @@ 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(
final @NotNull IScope scope,
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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
15 changes: 8 additions & 7 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 28 additions & 0 deletions sentry/src/main/java/io/sentry/internal/SentryRuntime.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 <T> @NotNull T require(final @Nullable T value, final @NotNull String name) {
if (value == null) {
// Only a runtime from buildForTest() has unset collaborators.
Expand Down
10 changes: 6 additions & 4 deletions sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILogger>()
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(
Expand Down
30 changes: 30 additions & 0 deletions sentry/src/test/java/io/sentry/internal/SentryRuntimeTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -51,6 +57,30 @@ class SentryRuntimeTest {
assertThat(created).isEqualTo(1)
}

@Test
fun `close shuts the executor down`() {
val executor = mock<ISentryExecutorService>()
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<Runnable>())
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()
Expand Down
Loading