@@ -119,6 +119,10 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
119119 final File previousSessionFile = getPreviousSessionFile (directoryPath );
120120
121121 if (HintUtils .hasType (hint , SessionEnd .class )) {
122+ // A SessionEnd normally clears session.json. Its envelope may have been queued while a
123+ // newer session replaced it on disk though, and deleting would then drop that session's
124+ // unhandled error before it can be finalized. Only keep the file when the stored session
125+ // is provably a different, later one carrying the flag.
122126 try (final @ NotNull ISentryLifecycleToken ignored = sessionLock .acquire ()) {
123127 final @ Nullable Session endingSession = readSessionFromEnvelope (envelope );
124128 final @ Nullable Session currentSession = readSessionFromDisk (currentSessionFile );
@@ -149,6 +153,8 @@ private boolean storeInternal(final @NotNull SentryEnvelope envelope, final @Not
149153 if (startingSession != null ) {
150154 final @ Nullable Session currentSession = readSessionFromDisk (currentSessionFile );
151155 if (currentSession != null && hasSameSessionId (currentSession , startingSession )) {
156+ // A start for the id already on disk is a late duplicate, and that stored snapshot
157+ // may have advanced since it was written, so overwrite only if it has not.
152158 if (!isNewerUnhandledOrErrorSnapshot (currentSession , startingSession )) {
153159 writeSessionToDisk (currentSessionFile , startingSession );
154160 }
0 commit comments