Skip to content

Commit 00a36b6

Browse files
ref(cache): delete the current session file unconditionally again
The SessionEnd guard only paid off in a narrow window: a SessionEnd still queued while a newer session had already started and recorded an unhandled error. Dropping it degrades to the behaviour on main, because the queued SessionStart rewrites the file moments later, just without the flag. That cost a session.json read and deserialize on every SessionEnd for every SDK. The SessionStart guard stays. Its window is far wider, since app start is when the transport is busiest flushing the previous run's cache, and its failure mode is worse than a lost flag: movePreviousSession files the running session as the previous one. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 01ecf6a commit 00a36b6

2 files changed

Lines changed: 3 additions & 137 deletions

File tree

‎sentry/src/main/java/io/sentry/cache/EnvelopeCache.java‎

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,8 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
118118
final File previousSessionFile = getPreviousSessionFile(directoryPath);
119119

120120
if (HintUtils.hasType(hint, SessionEnd.class)) {
121-
try (final @NotNull ISentryLifecycleToken ignored = sessionLock.acquire()) {
122-
final @Nullable Session endingSession = readSessionFromEnvelope(envelope);
123-
final @Nullable Session currentSession = readSessionFromDisk(currentSessionFile);
124-
if (!isStaleSessionEnd(endingSession, currentSession) && !currentSessionFile.delete()) {
125-
options.getLogger().log(WARNING, "Current envelope doesn't exist.");
126-
}
121+
if (!currentSessionFile.delete()) {
122+
options.getLogger().log(WARNING, "Current envelope doesn't exist.");
127123
}
128124
}
129125

@@ -289,7 +285,7 @@ private void writeCrashMarkerFile() {
289285
private @Nullable Session readSessionFromEnvelope(final @NotNull SentryEnvelope envelope) {
290286
final Iterable<SentryEnvelopeItem> items = envelope.getItems();
291287

292-
// we know that a session envelope has a single item inside
288+
// we know that an envelope with a SessionStart hint has a single item inside
293289
if (items.iterator().hasNext()) {
294290
final SentryEnvelopeItem item = items.iterator().next();
295291

@@ -338,20 +334,6 @@ private void writeCrashMarkerFile() {
338334
}
339335
}
340336

341-
/**
342-
* Whether a {@link SessionEnd} envelope was queued while a newer session replaced it on disk.
343-
* Deleting the file would then drop the newer session's unhandled error before it can be
344-
* finalized, so it is kept instead.
345-
*/
346-
private boolean isStaleSessionEnd(
347-
final @Nullable Session endingSession, final @Nullable Session currentSession) {
348-
return endingSession != null
349-
&& currentSession != null
350-
&& currentSession.hasNonTerminatingUnhandledError()
351-
&& hasDifferentSessionId(endingSession, currentSession)
352-
&& startedLaterThan(currentSession, endingSession);
353-
}
354-
355337
/**
356338
* Whether a {@link SessionStart} envelope refers to the session already on disk. Only {@link
357339
* #persistCurrentSession(Session)} can have put it there, writing the live session, so the stored
@@ -372,20 +354,6 @@ private boolean hasSameSessionId(
372354
&& Objects.equals(firstSession.getSessionId(), secondSession.getSessionId());
373355
}
374356

375-
private boolean hasDifferentSessionId(
376-
final @NotNull Session firstSession, final @NotNull Session secondSession) {
377-
return firstSession.getSessionId() != null
378-
&& secondSession.getSessionId() != null
379-
&& !Objects.equals(firstSession.getSessionId(), secondSession.getSessionId());
380-
}
381-
382-
@SuppressWarnings("JavaUtilDate")
383-
private boolean startedLaterThan(final @NotNull Session session, final @NotNull Session other) {
384-
return session.getStarted() != null
385-
&& other.getStarted() != null
386-
&& session.getStarted().after(other.getStarted());
387-
}
388-
389357
private boolean writeEnvelopeToDisk(
390358
final @NotNull File file, final @NotNull SentryEnvelope envelope) {
391359
if (file.exists()) {

‎sentry/src/test/java/io/sentry/cache/EnvelopeCacheTest.kt‎

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -266,108 +266,6 @@ class EnvelopeCacheTest {
266266
assertThat(persistedPrevious.sessionId).isEqualTo(currentSession.sessionId)
267267
}
268268

269-
@Test
270-
fun `matching SessionEnd deletes current session`() {
271-
val cache = fixture.getSUT()
272-
val session = createSession()
273-
cache.persistCurrentSession(session)
274-
275-
val envelope = SentryEnvelope.from(fixture.options.serializer, session, null)
276-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
277-
278-
assertThat(EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!).exists())
279-
.isFalse()
280-
}
281-
282-
@Test
283-
fun `mismatching SessionEnd preserves newer unhandled current session`() {
284-
val cache = fixture.getSUT()
285-
val endingSession = createSession(started = Date(1_000))
286-
val currentSession = createSession(started = Date(2_000))
287-
currentSession.recordNonTerminatingUnhandledError()
288-
cache.persistCurrentSession(currentSession)
289-
290-
val envelope = SentryEnvelope.from(fixture.options.serializer, endingSession, null)
291-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
292-
293-
val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!)
294-
val persistedSession =
295-
fixture.options.serializer.deserialize(
296-
currentSessionFile.bufferedReader(),
297-
Session::class.java,
298-
)!!
299-
assertThat(persistedSession.sessionId).isEqualTo(currentSession.sessionId)
300-
}
301-
302-
@Test
303-
fun `mismatching newer SessionEnd deletes stale unhandled current session`() {
304-
val cache = fixture.getSUT()
305-
val currentSession = createSession(started = Date(1_000))
306-
currentSession.recordNonTerminatingUnhandledError()
307-
cache.persistCurrentSession(currentSession)
308-
val endingSession = createSession(started = Date(2_000))
309-
310-
val envelope = SentryEnvelope.from(fixture.options.serializer, endingSession, null)
311-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
312-
313-
assertThat(EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!).exists())
314-
.isFalse()
315-
}
316-
317-
@Test
318-
fun `mismatching SessionEnd deletes current session without unhandled error`() {
319-
val cache = fixture.getSUT()
320-
val currentSession = createSession()
321-
cache.persistCurrentSession(currentSession)
322-
val endingSession = createSession()
323-
324-
val envelope = SentryEnvelope.from(fixture.options.serializer, endingSession, null)
325-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
326-
327-
assertThat(EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!).exists())
328-
.isFalse()
329-
}
330-
331-
@Test
332-
fun `null SIDs on SessionEnd delete current session`() {
333-
val cache = fixture.getSUT()
334-
val currentSession = createSession(sessionId = null)
335-
cache.persistCurrentSession(currentSession)
336-
val endingSession = createSession(sessionId = null)
337-
338-
val envelope = SentryEnvelope.from(fixture.options.serializer, endingSession, null)
339-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
340-
341-
assertThat(EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!).exists())
342-
.isFalse()
343-
}
344-
345-
@Test
346-
fun `malformed SessionEnd deletes current session`() {
347-
val cache = fixture.getSUT()
348-
val currentSession = createSession()
349-
cache.persistCurrentSession(currentSession)
350-
351-
val envelope = SentryEnvelope(null, null, emptyList())
352-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
353-
354-
assertThat(EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!).exists())
355-
.isFalse()
356-
}
357-
358-
@Test
359-
fun `unreadable current session on SessionEnd is deleted`() {
360-
val cache = fixture.getSUT()
361-
val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!)
362-
currentSessionFile.writeText("not-a-session")
363-
364-
val endingSession = createSession()
365-
val envelope = SentryEnvelope.from(fixture.options.serializer, endingSession, null)
366-
cache.storeEnvelope(envelope, HintUtils.createWithTypeCheckHint(SessionEndHint()))
367-
368-
assertThat(currentSessionFile.exists()).isFalse()
369-
}
370-
371269
@Test
372270
fun `updates current file on session update and read it back`() {
373271
val cache = fixture.getSUT()

0 commit comments

Comments
 (0)