Skip to content

Commit dbfcd3f

Browse files
ref(cache): fold the session-id comparison into one predicate
Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 0a2dda4 commit dbfcd3f

2 files changed

Lines changed: 7 additions & 63 deletions

File tree

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

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -339,19 +339,16 @@ private void writeCrashMarkerFile() {
339339
* #persistCurrentSession(Session)} can have put it there, writing the live session, so the stored
340340
* copy is at least as advanced as this envelope. Rotating and overwriting it would file a running
341341
* session as the previous one and roll back any unhandled error it has recorded since.
342+
*
343+
* <p>A null session id never matches, so sessions we cannot tell apart are rotated as before.
342344
*/
343345
private boolean isLateDuplicateStart(
344346
final @Nullable Session startingSession, final @Nullable Session currentSession) {
345-
return startingSession != null
346-
&& currentSession != null
347-
&& hasSameSessionId(currentSession, startingSession);
348-
}
349-
350-
private boolean hasSameSessionId(
351-
final @NotNull Session firstSession, final @NotNull Session secondSession) {
352-
return firstSession.getSessionId() != null
353-
&& secondSession.getSessionId() != null
354-
&& Objects.equals(firstSession.getSessionId(), secondSession.getSessionId());
347+
if (startingSession == null || currentSession == null) {
348+
return false;
349+
}
350+
final @Nullable String startingSessionId = startingSession.getSessionId();
351+
return startingSessionId != null && startingSessionId.equals(currentSession.getSessionId());
355352
}
356353

357354
private boolean writeEnvelopeToDisk(

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

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -452,36 +452,6 @@ class EnvelopeCacheTest {
452452
assertEquals(sessionExitedWithAbnormal, updatedSession!!.timestamp!!.time)
453453
}
454454

455-
@Test
456-
fun `AbnormalExit hint keeps persisted unhandled session as abnormal`() {
457-
val cache = fixture.getSUT()
458-
459-
val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!)
460-
val session = createSession().apply { recordNonTerminatingUnhandledError() }
461-
fixture.options.serializer.serialize(session, previousSessionFile.bufferedWriter())
462-
463-
val envelope = SentryEnvelope.from(fixture.options.serializer, SentryEvent(), null)
464-
val abnormalHint =
465-
object : AbnormalExit {
466-
override fun mechanism(): String = "abnormal_mechanism"
467-
468-
override fun ignoreCurrentThread(): Boolean = false
469-
470-
override fun timestamp(): Long = session.started!!.time + TimeUnit.HOURS.toMillis(1)
471-
}
472-
val hints = HintUtils.createWithTypeCheckHint(abnormalHint)
473-
cache.storeEnvelope(envelope, hints)
474-
475-
val updatedSession =
476-
fixture.options.serializer.deserialize(
477-
previousSessionFile.bufferedReader(),
478-
Session::class.java,
479-
)
480-
assertEquals(State.Abnormal, updatedSession!!.status)
481-
assertEquals("abnormal_mechanism", updatedSession.abnormalMechanism)
482-
assertTrue(updatedSession.hasNonTerminatingUnhandledError())
483-
}
484-
485455
@Test
486456
fun `when AbnormalExit happened before previous session start, does not mark as abnormal`() {
487457
val cache = fixture.getSUT()
@@ -536,29 +506,6 @@ class EnvelopeCacheTest {
536506
assertEquals(nativeCrashTimestamp, updatedSession.timestamp!!.time)
537507
}
538508

539-
@Test
540-
fun `NativeCrashExit hint keeps persisted unhandled session as crashed`() {
541-
val cache = fixture.getSUT()
542-
543-
val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!)
544-
val session = createSession().apply { recordNonTerminatingUnhandledError() }
545-
fixture.options.serializer.serialize(session, previousSessionFile.bufferedWriter())
546-
547-
val nativeCrashTimestamp = session.started!!.time + TimeUnit.HOURS.toMillis(1)
548-
val envelope = SentryEnvelope.from(fixture.options.serializer, SentryEvent(), null)
549-
val hints = HintUtils.createWithTypeCheckHint(NativeCrashExit { nativeCrashTimestamp })
550-
cache.storeEnvelope(envelope, hints)
551-
552-
val updatedSession =
553-
fixture.options.serializer.deserialize(
554-
previousSessionFile.bufferedReader(),
555-
Session::class.java,
556-
)
557-
assertEquals(State.Crashed, updatedSession!!.status)
558-
assertEquals(nativeCrashTimestamp, updatedSession.timestamp!!.time)
559-
assertFalse(updatedSession.hasNonTerminatingUnhandledError())
560-
}
561-
562509
@Test
563510
fun `when NativeCrashExit happened before previous session start, does not mark as crashed`() {
564511
val cache = fixture.getSUT()

0 commit comments

Comments
 (0)