Skip to content

Commit f4dfc80

Browse files
ref(cache): replace the stale-start comparison with an identity check
session.json has only two writers, the SessionStart path and persistCurrentSession. So if a start envelope finds its own session id already on disk, persistCurrentSession put it there for the live session, and that copy is necessarily at least as advanced. There is nothing to measure: comparing the unhandled flag and error count answered a question that only ever has one answer. The start path collapses to "if this envelope is about a different session than the one on disk, behave as before; otherwise leave it alone", which also avoids rotating a running session into previous_session.json. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent d5fec24 commit f4dfc80

1 file changed

Lines changed: 13 additions & 18 deletions

File tree

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

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,12 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
135135
if (HintUtils.hasType(hint, SessionStart.class)) {
136136
try (final @NotNull ISentryLifecycleToken ignored = sessionLock.acquire()) {
137137
final @Nullable Session startingSession = readSessionFromEnvelope(envelope);
138-
if (startingSession != null) {
139-
final @Nullable Session currentSession = readSessionFromDisk(currentSessionFile);
140-
if (currentSession != null && hasSameSessionId(currentSession, startingSession)) {
141-
if (!isStaleSessionStart(startingSession, currentSession)) {
142-
writeSessionToDisk(currentSessionFile, startingSession);
143-
}
144-
} else {
145-
movePreviousSession(currentSessionFile, previousSessionFile);
138+
final @Nullable Session currentSession = readSessionFromDisk(currentSessionFile);
139+
if (!isLateDuplicateStart(startingSession, currentSession)) {
140+
movePreviousSession(currentSessionFile, previousSessionFile);
141+
if (startingSession != null) {
146142
writeSessionToDisk(currentSessionFile, startingSession);
147143
}
148-
} else {
149-
movePreviousSession(currentSessionFile, previousSessionFile);
150144
}
151145
}
152146

@@ -359,15 +353,16 @@ && hasDifferentSessionId(endingSession, currentSession)
359353
}
360354

361355
/**
362-
* Whether a {@link SessionStart} envelope is a late duplicate of the session already on disk,
363-
* whose snapshot has since advanced. Writing it would roll back the recorded unhandled error or
364-
* error count. Only meaningful for two snapshots of the same session.
356+
* Whether a {@link SessionStart} envelope refers to the session already on disk. Only {@link
357+
* #persistCurrentSession(Session)} can have put it there, writing the live session, so the stored
358+
* copy is at least as advanced as this envelope. Rotating and overwriting it would file a running
359+
* session as the previous one and roll back any unhandled error it has recorded since.
365360
*/
366-
private boolean isStaleSessionStart(
367-
final @NotNull Session startingSession, final @NotNull Session currentSession) {
368-
return (currentSession.hasNonTerminatingUnhandledError()
369-
&& !startingSession.hasNonTerminatingUnhandledError())
370-
|| currentSession.errorCount() > startingSession.errorCount();
361+
private boolean isLateDuplicateStart(
362+
final @Nullable Session startingSession, final @Nullable Session currentSession) {
363+
return startingSession != null
364+
&& currentSession != null
365+
&& hasSameSessionId(currentSession, startingSession);
371366
}
372367

373368
private boolean hasSameSessionId(

0 commit comments

Comments
 (0)