Skip to content

Commit 872b084

Browse files
romtsnclaude
andauthored
fix(sessions): Finalize previous session even when auto session tracking is disabled (#5154)
* 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 <noreply@anthropic.com> * formatting * changelog: Add entry for #5154 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 37a4609 commit 872b084

File tree

6 files changed

+16
-41
lines changed

6 files changed

+16
-41
lines changed

CHANGELOG.md

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

55
### Fixes
66

7+
- Common: Finalize previous session even when auto session tracking is disabled ([#5154](https://github.com/getsentry/sentry-java/pull/5154))
78
- Android: Add proguard rules to prevent error about missing Replay classes ([#5153](https://github.com/getsentry/sentry-java/pull/5153))
89

910
## 8.34.0

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.sentry.android.core;
22

33
import static io.sentry.Sentry.getCurrentScopes;
4-
import static io.sentry.SentryLevel.DEBUG;
54
import static io.sentry.SentryLevel.INFO;
65
import static io.sentry.SentryLevel.WARNING;
76

@@ -291,13 +290,6 @@ private static void deleteCurrentSessionFile(final @NotNull SentryOptions option
291290
return;
292291
}
293292

294-
if (!options.isEnableAutoSessionTracking()) {
295-
options
296-
.getLogger()
297-
.log(DEBUG, "Session tracking is disabled, bailing from deleting current session file.");
298-
return;
299-
}
300-
301293
final File sessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath);
302294
if (!sessionFile.delete()) {
303295
options.getLogger().log(WARNING, "Failed to delete the current session file.");

sentry/src/main/java/io/sentry/MovePreviousSession.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.sentry;
22

3-
import static io.sentry.SentryLevel.DEBUG;
43
import static io.sentry.SentryLevel.INFO;
54

65
import io.sentry.cache.EnvelopeCache;
@@ -24,13 +23,6 @@ public void run() {
2423
return;
2524
}
2625

27-
if (!options.isEnableAutoSessionTracking()) {
28-
options
29-
.getLogger()
30-
.log(DEBUG, "Session tracking is disabled, bailing from previous session mover.");
31-
return;
32-
}
33-
3426
final IEnvelopeCache cache = options.getEnvelopeDiskCache();
3527
if (cache instanceof EnvelopeCache) {
3628
final File currentSessionFile = EnvelopeCache.getCurrentSessionFile(cacheDirPath);

sentry/src/main/java/io/sentry/PreviousSessionFinalizer.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ public void run() {
4848
return;
4949
}
5050

51-
if (!options.isEnableAutoSessionTracking()) {
52-
options
53-
.getLogger()
54-
.log(DEBUG, "Session tracking is disabled, bailing from previous session finalizer.");
55-
return;
56-
}
57-
5851
final IEnvelopeCache cache = options.getEnvelopeDiskCache();
5952
if (cache instanceof EnvelopeCache) {
6053
if (!((EnvelopeCache) cache).waitPreviousSessionFlush()) {

sentry/src/test/java/io/sentry/MovePreviousSessionTest.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,21 @@ class MovePreviousSessionTest {
6565
}
6666

6767
@Test
68-
fun `when session tracking is disabled, logs and returns early`() {
69-
val sut = fixture.getSUT(isEnableSessionTracking = false, envelopeCache = fixture.cache)
68+
fun `when session tracking is disabled, still moves previous session`() {
69+
val sut = fixture.getSUT(isEnableSessionTracking = false)
70+
71+
val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!)
72+
val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!)
73+
74+
currentSessionFile.createNewFile()
75+
currentSessionFile.writeText("session content")
7076

7177
sut.run()
7278

73-
verify(fixture.cache, never()).movePreviousSession(any(), any())
74-
verify(fixture.cache, never()).flushPreviousSession()
79+
(fixture.options.envelopeDiskCache as EnvelopeCache).waitPreviousSessionFlush()
80+
81+
assertFalse(currentSessionFile.exists())
82+
assertTrue(previousSessionFile.exists())
7583
}
7684

7785
@Test

sentry/src/test/java/io/sentry/PreviousSessionFinalizerTest.kt

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,27 +210,16 @@ class PreviousSessionFinalizerTest {
210210
}
211211

212212
@Test
213-
fun `if session tracking is disabled, does not wait for previous session flush`() {
213+
fun `if session tracking is disabled, still finalizes previous session`() {
214214
val finalizer =
215215
fixture.getSut(
216216
tmpDir,
217-
flushTimeoutMillis = 500L,
217+
session = Session(null, null, null, "io.sentry.sample@1.0"),
218218
sessionTrackingEnabled = false,
219-
shouldAwait = true,
220219
)
221220
finalizer.run()
222221

223-
verify(fixture.logger, never())
224-
.log(
225-
any(),
226-
argThat {
227-
startsWith(
228-
"Timed out waiting to flush previous session to its own file in session finalizer."
229-
)
230-
},
231-
any<Any>(),
232-
)
233-
verify(fixture.scopes, never()).captureEnvelope(any())
222+
verify(fixture.scopes).captureEnvelope(any())
234223
}
235224

236225
@Test

0 commit comments

Comments
 (0)