Skip to content

Commit 3524661

Browse files
authored
Fix invalid session creation when app is launched in background (#2543)
1 parent 1045a47 commit 3524661

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
- Ignore Shutdown in progress when closing ShutdownHookIntegration ([#2521](https://github.com/getsentry/sentry-java/pull/2521))
1313
- Fix app start span end-time is wrong if SDK init is deferred ([#2519](https://github.com/getsentry/sentry-java/pull/2519))
14+
- Fix invalid session creation when app is launched in background ([#2543](https://github.com/getsentry/sentry-java/pull/2543))
1415

1516
## 6.13.1
1617

sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ public static synchronized void init(
123123
true);
124124

125125
final @NotNull IHub hub = Sentry.getCurrentHub();
126-
if (hub.getOptions().isEnableAutoSessionTracking()) {
126+
if (hub.getOptions().isEnableAutoSessionTracking()
127+
&& ContextUtils.isForegroundImportance(context)) {
127128
hub.addBreadcrumb(BreadcrumbFactory.forSession("session.start"));
128129
hub.startSession();
129130
}

sentry-android-core/src/test/java/io/sentry/android/core/SentryAndroidTest.kt

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import io.sentry.SentryLevel
1111
import io.sentry.SentryLevel.DEBUG
1212
import io.sentry.SentryLevel.FATAL
1313
import io.sentry.SentryOptions
14+
import io.sentry.Session
1415
import io.sentry.android.core.cache.AndroidEnvelopeCache
1516
import io.sentry.android.fragment.FragmentLifecycleIntegration
1617
import io.sentry.android.timber.SentryTimberIntegration
1718
import io.sentry.cache.IEnvelopeCache
1819
import io.sentry.transport.NoOpEnvelopeCache
1920
import io.sentry.util.StringUtils
2021
import org.junit.runner.RunWith
22+
import org.mockito.Mockito
2123
import org.mockito.kotlin.any
2224
import org.mockito.kotlin.eq
2325
import org.mockito.kotlin.mock
@@ -188,12 +190,36 @@ class SentryAndroidTest {
188190
}
189191

190192
@Test
191-
fun `init starts a session if auto session tracking is enabled`() {
192-
fixture.initSut { options ->
193-
options.isEnableAutoSessionTracking = true
193+
fun `init starts a session if auto session tracking is enabled and app is in foreground`() {
194+
initSentryWithForegroundImportance(true) { session: Session? ->
195+
assertNotNull(session)
194196
}
195-
Sentry.getCurrentHub().withScope { scope ->
196-
assertNotNull(scope.session)
197+
}
198+
199+
@Test
200+
fun `init does not start a session if auto session tracking is enabled but the app is in background`() {
201+
initSentryWithForegroundImportance(false) { session: Session? ->
202+
assertNull(session)
203+
}
204+
}
205+
206+
private fun initSentryWithForegroundImportance(inForeground: Boolean, callback: (session: Session?) -> Unit) {
207+
val context = ContextUtilsTest.createMockContext()
208+
209+
Mockito.mockStatic(ContextUtils::class.java).use { mockedContextUtils ->
210+
mockedContextUtils.`when`<Any> { ContextUtils.isForegroundImportance(context) }
211+
.thenReturn(inForeground)
212+
SentryAndroid.init(context) { options ->
213+
options.release = "prod"
214+
options.dsn = "https://[email protected]/123"
215+
options.isEnableAutoSessionTracking = true
216+
}
217+
218+
var session: Session? = null
219+
Sentry.getCurrentHub().configureScope { scope ->
220+
session = scope.session
221+
}
222+
callback(session)
197223
}
198224
}
199225

0 commit comments

Comments
 (0)