diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c483f49b71..4ed5ebe23c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -57,6 +57,8 @@ - Transactions are dropped if trace context is missing - Remove internal annotation on `SpanOptions` ([#3722](https://github.com/getsentry/sentry-java/pull/3722)) - `SentryLogbackInitializer` is now public ([#3723](https://github.com/getsentry/sentry-java/pull/3723)) +- Fix order of calling `close` on previous Sentry instance when re-initializing ([#3750](https://github.com/getsentry/sentry-java/pull/3750)) + - Previously some parts of Sentry were immediately closed after re-init that should have stayed open and some parts of the previous init were never closed ### Behavioural Changes diff --git a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt index fe7b0a271b3..b615406a3d7 100644 --- a/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt +++ b/sentry-android-integration-tests/sentry-uitest-android/src/androidTest/java/io/sentry/uitest/android/SdkInitTests.kt @@ -10,7 +10,6 @@ import io.sentry.android.core.SentryAndroidOptions import io.sentry.assertEnvelopeTransaction import io.sentry.protocol.SentryTransaction import org.junit.runner.RunWith -import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue @@ -36,7 +35,6 @@ class SdkInitTests : BaseUiTest() { transaction2.finish() } - @Ignore("not working since re-init changes related to POTel") @Test fun doubleInitWithSameOptionsDoesNotThrow() { val options = SentryAndroidOptions() @@ -95,7 +93,6 @@ class SdkInitTests : BaseUiTest() { } } - @Ignore("not working since re-init changes related to POTel") @Test fun doubleInitDoesNotWait() { relayIdlingResource.increment() diff --git a/sentry-graphql-22/api/sentry-graphql-22.api b/sentry-graphql-22/api/sentry-graphql-22.api index ce7469cce09..b456fd98bf4 100644 --- a/sentry-graphql-22/api/sentry-graphql-22.api +++ b/sentry-graphql-22/api/sentry-graphql-22.api @@ -1,5 +1,4 @@ public final class io/sentry/graphql22/BuildConfig { - public static final field SENTRY_GRAPHQL_SDK_NAME Ljava/lang/String; public static final field SENTRY_GRAPHQL22_SDK_NAME Ljava/lang/String; public static final field VERSION_NAME Ljava/lang/String; } diff --git a/sentry/src/main/java/io/sentry/Sentry.java b/sentry/src/main/java/io/sentry/Sentry.java index 7196ded8170..13fb8ace60c 100644 --- a/sentry/src/main/java/io/sentry/Sentry.java +++ b/sentry/src/main/java/io/sentry/Sentry.java @@ -294,17 +294,18 @@ private static void init(final @NotNull SentryOptions options, final boolean glo SentryLevel.WARNING, "Sentry has been already initialized. Previous configuration will be overwritten."); } - globalScope.replaceOptions(options); final IScopes scopes = getCurrentScopes(); + scopes.close(true); + + globalScope.replaceOptions(options); + final IScope rootScope = new Scope(options); final IScope rootIsolationScope = new Scope(options); rootScopes = new Scopes(rootScope, rootIsolationScope, globalScope, "Sentry.init"); getScopesStorage().set(rootScopes); - scopes.close(true); - initConfigurations(options); globalScope.bindClient(new SentryClient(options)); diff --git a/sentry/src/test/java/io/sentry/SentryTest.kt b/sentry/src/test/java/io/sentry/SentryTest.kt index 58cade0251a..867382985fb 100644 --- a/sentry/src/test/java/io/sentry/SentryTest.kt +++ b/sentry/src/test/java/io/sentry/SentryTest.kt @@ -33,6 +33,7 @@ import org.mockito.kotlin.mock import org.mockito.kotlin.never import org.mockito.kotlin.verify import org.mockito.kotlin.whenever +import java.io.Closeable import java.io.File import java.io.FileReader import java.nio.file.Files @@ -78,6 +79,50 @@ class SentryTest { verify(scopes).close(eq(true)) } + @Test + fun `init multiple times calls close on previous options not new`() { + val profiler1 = mock() + val profiler2 = mock() + Sentry.init { + it.dsn = dsn + it.setTransactionProfiler(profiler1) + } + verify(profiler1, never()).close() + + Sentry.init { + it.dsn = dsn + it.setTransactionProfiler(profiler2) + } + verify(profiler2, never()).close() + verify(profiler1).close() + + Sentry.close() + verify(profiler2).close() + } + + @Test + fun `init multiple times calls close on previous integrations not new`() { + val integration1 = mock() + val integration2 = mock() + Sentry.init { + it.dsn = dsn + it.addIntegration(integration1) + } + verify(integration1, never()).close() + + Sentry.init { + it.dsn = dsn + it.addIntegration(integration2) + } + verify(integration2, never()).close() + verify(integration1).close() + + Sentry.close() + verify(integration2).close() + } + + interface CloseableIntegration : Integration, Closeable + @Test fun `global client is enabled after restart`() { val scopes = mock()