From 6a29b8e5599b6ccf5d2827e1e8d36d9aac6213b6 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Thu, 5 Mar 2026 00:12:21 +0100 Subject: [PATCH 1/3] fix(core): Finalize previous session even when auto session tracking is disabled The `isEnableAutoSessionTracking` guard in `PreviousSessionFinalizer`, `MovePreviousSession`, and `InternalSentrySdk.deleteCurrentSessionFile` prevented finalization of manually started sessions (via `Sentry.startSession()`). The flag controls *automatic* session lifecycle, not whether sessions exist at all. Manual sessions write the same files to disk and need the same finalization. Fixes #5108 Co-Authored-By: Claude Opus 4.6 --- .../sentry/android/core/InternalSentrySdk.java | 9 +-------- .../java/io/sentry/MovePreviousSession.java | 8 -------- .../io/sentry/PreviousSessionFinalizer.java | 7 ------- .../java/io/sentry/MovePreviousSessionTest.kt | 16 ++++++++++++---- .../io/sentry/PreviousSessionFinalizerTest.kt | 17 +++-------------- 5 files changed, 16 insertions(+), 41 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index cae558f0d43..2282c5b4cd6 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -1,7 +1,7 @@ package io.sentry.android.core; import static io.sentry.Sentry.getCurrentScopes; -import static io.sentry.SentryLevel.DEBUG; + import static io.sentry.SentryLevel.INFO; import static io.sentry.SentryLevel.WARNING; @@ -291,13 +291,6 @@ private static void deleteCurrentSessionFile(final @NotNull SentryOptions option return; } - if (!options.isEnableAutoSessionTracking()) { - options - .getLogger() - .log(DEBUG, "Session tracking is disabled, bailing from deleting current session file."); - return; - } - final File sessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath); if (!sessionFile.delete()) { options.getLogger().log(WARNING, "Failed to delete the current session file."); diff --git a/sentry/src/main/java/io/sentry/MovePreviousSession.java b/sentry/src/main/java/io/sentry/MovePreviousSession.java index 99b92d92653..6b7de16d81a 100644 --- a/sentry/src/main/java/io/sentry/MovePreviousSession.java +++ b/sentry/src/main/java/io/sentry/MovePreviousSession.java @@ -1,6 +1,5 @@ package io.sentry; -import static io.sentry.SentryLevel.DEBUG; import static io.sentry.SentryLevel.INFO; import io.sentry.cache.EnvelopeCache; @@ -24,13 +23,6 @@ public void run() { return; } - if (!options.isEnableAutoSessionTracking()) { - options - .getLogger() - .log(DEBUG, "Session tracking is disabled, bailing from previous session mover."); - return; - } - final IEnvelopeCache cache = options.getEnvelopeDiskCache(); if (cache instanceof EnvelopeCache) { final File currentSessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath); diff --git a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java index 46e0c258507..4e3b2e9b3d2 100644 --- a/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java +++ b/sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java @@ -48,13 +48,6 @@ public void run() { return; } - if (!options.isEnableAutoSessionTracking()) { - options - .getLogger() - .log(DEBUG, "Session tracking is disabled, bailing from previous session finalizer."); - return; - } - final IEnvelopeCache cache = options.getEnvelopeDiskCache(); if (cache instanceof EnvelopeCache) { if (!((EnvelopeCache) cache).waitPreviousSessionFlush()) { diff --git a/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt b/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt index 0c361c3efe8..f14cbd6d016 100644 --- a/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt +++ b/sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt @@ -65,13 +65,21 @@ class MovePreviousSessionTest { } @Test - fun `when session tracking is disabled, logs and returns early`() { - val sut = fixture.getSUT(isEnableSessionTracking = false, envelopeCache = fixture.cache) + fun `when session tracking is disabled, still moves previous session`() { + val sut = fixture.getSUT(isEnableSessionTracking = false) + + val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!) + val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!) + + currentSessionFile.createNewFile() + currentSessionFile.writeText("session content") sut.run() - verify(fixture.cache, never()).movePreviousSession(any(), any()) - verify(fixture.cache, never()).flushPreviousSession() + (fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush() + + assertFalse(currentSessionFile.exists()) + assertTrue(previousSessionFile.exists()) } @Test diff --git a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt index 7a205d6d51d..4b433ffb3e1 100644 --- a/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt +++ b/sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt @@ -210,27 +210,16 @@ class PreviousSessionFinalizerTest { } @Test - fun `if session tracking is disabled, does not wait for previous session flush`() { + fun `if session tracking is disabled, still finalizes previous session`() { val finalizer = fixture.getSut( tmpDir, - flushTimeoutMillis = 500L, + session = Session(null, null, null, "io.sentry.sample@1.0"), sessionTrackingEnabled = false, - shouldAwait = true, ) finalizer.run() - verify(fixture.logger, never()) - .log( - any(), - argThat { - startsWith( - "Timed out waiting to flush previous session to its own file in session finalizer." - ) - }, - any(), - ) - verify(fixture.scopes, never()).captureEnvelope(any()) + verify(fixture.scopes).captureEnvelope(any()) } @Test From 0ab5d45e21c977219e71e3de90cfdfb79ccf7d44 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Thu, 5 Mar 2026 00:13:40 +0100 Subject: [PATCH 2/3] formatting --- .../src/main/java/io/sentry/android/core/InternalSentrySdk.java | 1 - 1 file changed, 1 deletion(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java index 2282c5b4cd6..2779f803a69 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/InternalSentrySdk.java @@ -1,7 +1,6 @@ package io.sentry.android.core; import static io.sentry.Sentry.getCurrentScopes; - import static io.sentry.SentryLevel.INFO; import static io.sentry.SentryLevel.WARNING; From f9020c974954c11b7a123e60390b7a93b62f5d25 Mon Sep 17 00:00:00 2001 From: Roman Zavarnitsyn Date: Thu, 5 Mar 2026 00:14:10 +0100 Subject: [PATCH 3/3] changelog: Add entry for #5154 Co-Authored-By: Claude Opus 4.6 --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 338bbea4457..27674640c7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Fixes + +- Finalize previous session even when auto session tracking is disabled ([#5154](https://github.com/getsentry/sentry-java/pull/5154)) + ## 8.34.0 ### Features