Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate session start for React Native #3504

Merged
merged 4 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Move fragment auto span finish to onFragmentStarted ([#3424](https://github.com/getsentry/sentry-java/pull/3424))
- Remove profiling timeout logic and disable profiling on API 21 ([#3478](https://github.com/getsentry/sentry-java/pull/3478))
- Properly reset metric flush flag on metric emission ([#3493](https://github.com/getsentry/sentry-java/pull/3493))
- Fix duplicate session start for React Native ([#3504](https://github.com/getsentry/sentry-java/pull/3504))

## 7.10.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.sentry.Sentry;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.Session;
import io.sentry.android.core.internal.util.BreadcrumbFactory;
import io.sentry.android.core.performance.AppStartMetrics;
import io.sentry.android.core.performance.TimeSpan;
Expand All @@ -19,7 +20,9 @@
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/** Sentry initialization class */
public final class SentryAndroid {
Expand Down Expand Up @@ -146,8 +149,21 @@ public static synchronized void init(

final @NotNull IHub hub = Sentry.getCurrentHub();
if (hub.getOptions().isEnableAutoSessionTracking() && ContextUtils.isForegroundImportance()) {
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
hub.startSession();
// The LifecycleWatcher of AppLifecycleIntegration may already started a session
// so only start a session if it's not already started
// This e.g. happens on React Native, or e.g. on deferred SDK init
final AtomicBoolean sessionStarted = new AtomicBoolean(false);
hub.configureScope(
scope -> {
final @Nullable Session currentSession = scope.getSession();
if (currentSession != null && currentSession.getStarted() != null) {
sessionStarted.set(true);
}
});
if (!sessionStarted.get()) {
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
hub.startSession();
}
}
} catch (IllegalAccessException e) {
logger.log(SentryLevel.FATAL, "Fatal error during SentryAndroid.init(...)", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import io.sentry.Breadcrumb
import io.sentry.Hint
import io.sentry.ILogger
import io.sentry.ISentryClient
import io.sentry.Sentry
import io.sentry.SentryEnvelope
import io.sentry.SentryLevel
Expand Down Expand Up @@ -50,6 +51,7 @@ import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.spy
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.annotation.Config
Expand Down Expand Up @@ -314,8 +316,24 @@ class SentryAndroidTest {
}
}

@Test
fun `init does not start a session if one is already running`() {
val client = mock<ISentryClient>()

initSentryWithForegroundImportance(true, { options ->
options.addIntegration { hub, _ ->
hub.bindClient(client)
// usually done by LifecycleWatcher
hub.startSession()
}
}) {}

verify(client, times(1)).captureSession(any(), any())
}

private fun initSentryWithForegroundImportance(
inForeground: Boolean,
optionsConfig: (SentryAndroidOptions) -> Unit = {},
callback: (session: Session?) -> Unit
) {
val context = ContextUtilsTestHelper.createMockContext()
Expand All @@ -327,6 +345,7 @@ class SentryAndroidTest {
options.release = "prod"
options.dsn = "https://[email protected]/123"
options.isEnableAutoSessionTracking = true
optionsConfig(options)
}

var session: Session? = null
Expand Down
Loading