Skip to content
Merged
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -95,7 +93,6 @@ class SdkInitTests : BaseUiTest() {
}
}

@Ignore("not working since re-init changes related to POTel")
@Test
fun doubleInitDoesNotWait() {
relayIdlingResource.increment()
Expand Down
1 change: 0 additions & 1 deletion sentry-graphql-22/api/sentry-graphql-22.api
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions sentry/src/main/java/io/sentry/Sentry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
45 changes: 45 additions & 0 deletions sentry/src/test/java/io/sentry/SentryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<ITransactionProfiler>()
val profiler2 = mock<ITransactionProfiler>()
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<CloseableIntegration>()
val integration2 = mock<CloseableIntegration>()
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<IScopes>()
Expand Down
Loading