@@ -76,6 +76,12 @@ public class EnvelopeCache extends CacheStrategy implements IEnvelopeCache {
7676 protected final @ NotNull AutoClosableReentrantLock cacheLock = new AutoClosableReentrantLock ();
7777 protected final @ NotNull AutoClosableReentrantLock sessionLock = new AutoClosableReentrantLock ();
7878
79+ /**
80+ * Session id last written to the current session file by {@link #persistCurrentSession(Session)},
81+ * which bypasses the transport queue that every other write to that file goes through.
82+ */
83+ private volatile @ Nullable String lastPersistedSessionId ;
84+
7985 public static @ NotNull IEnvelopeCache create (final @ NotNull SentryOptions options ) {
8086 final String cacheDirPath = options .getCacheDirPath ();
8187 final int maxCacheItems = options .getMaxCacheItems ();
@@ -131,8 +137,7 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
131137 if (HintUtils .hasType (hint , SessionStart .class )) {
132138 try (final @ NotNull ISentryLifecycleToken ignored = sessionLock .acquire ()) {
133139 final @ Nullable Session startingSession = readSessionFromEnvelope (envelope );
134- final @ Nullable Session currentSession = readSessionFromDisk (currentSessionFile );
135- if (!isLateDuplicateStart (startingSession , currentSession )) {
140+ if (!isLateDuplicateStart (startingSession )) {
136141 movePreviousSession (currentSessionFile , previousSessionFile );
137142 if (startingSession != null ) {
138143 writeSessionToDisk (currentSessionFile , startingSession );
@@ -321,34 +326,21 @@ private void writeCrashMarkerFile() {
321326 return null ;
322327 }
323328
324- private @ Nullable Session readSessionFromDisk (final @ NotNull File sessionFile ) {
325- if (!sessionFile .exists ()) {
326- return null ;
327- }
328- try (final Reader reader =
329- new BufferedReader (new InputStreamReader (new FileInputStream (sessionFile ), UTF_8 ))) {
330- return serializer .getValue ().deserialize (reader , Session .class );
331- } catch (Exception e ) {
332- options .getLogger ().log (ERROR , "Failed to read session from disk." , e );
333- return null ;
334- }
335- }
336-
337329 /**
338- * Whether a {@link SessionStart} envelope refers to the session already on disk. Only {@link
339- * #persistCurrentSession(Session)} can have put it there, writing the live session, so the stored
340- * copy is at least as advanced as this envelope. Rotating and overwriting it would file a running
341- * session as the previous one and roll back any unhandled error it has recorded since.
330+ * Whether a {@link SessionStart} envelope refers to the session {@link
331+ * #persistCurrentSession(Session)} already wrote to the current session file. That copy is the
332+ * live session, so it is at least as advanced as this envelope. Rotating and overwriting it would
333+ * file a running session as the previous one and roll back any unhandled error it has recorded
334+ * since.
342335 *
343336 * <p>A null session id never matches, so sessions we cannot tell apart are rotated as before.
344337 */
345- private boolean isLateDuplicateStart (
346- final @ Nullable Session startingSession , final @ Nullable Session currentSession ) {
347- if (startingSession == null || currentSession == null ) {
338+ private boolean isLateDuplicateStart (final @ Nullable Session startingSession ) {
339+ if (startingSession == null ) {
348340 return false ;
349341 }
350342 final @ Nullable String startingSessionId = startingSession .getSessionId ();
351- return startingSessionId != null && startingSessionId .equals (currentSession . getSessionId () );
343+ return startingSessionId != null && startingSessionId .equals (lastPersistedSessionId );
352344 }
353345
354346 private boolean writeEnvelopeToDisk (
@@ -392,6 +384,7 @@ private void writeSessionToDisk(final @NotNull File file, final @NotNull Session
392384 public void persistCurrentSession (final @ NotNull Session session ) {
393385 try (final @ NotNull ISentryLifecycleToken ignored = sessionLock .acquire ()) {
394386 writeSessionToDisk (getCurrentSessionFile (directory .getOrCreate ().getAbsolutePath ()), session );
387+ lastPersistedSessionId = session .getSessionId ();
395388 }
396389 }
397390
0 commit comments