Skip to content

Commit cb9213a

Browse files
romtsnclaude
andcommitted
fix(core): Write the live session even if the previous session move fails
Skipping the write only delayed the loss of the last run's session: a rename that fails keeps failing, and the SessionStart path then writes over the old file anyway. Write the live session and mark the move as done once the write succeeds, so the init-time move cannot take it. After a failed write, delete the truncated file only if the move worked. If the move failed, the file can still be the last run's session, so it stays for a later move to retry. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 33fc2b6 commit cb9213a

2 files changed

Lines changed: 37 additions & 14 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
@@ -402,18 +402,15 @@ public void persistCurrentSession(final @NotNull Session session) {
402402
// overwriting it
403403
if (!previousSessionMoved) {
404404
moveCurrentToPreviousSession(currentSessionFile, getPreviousSessionFile(directoryPath));
405-
if (!previousSessionMoved) {
406-
options
407-
.getLogger()
408-
.log(WARNING, "Previous session could not be moved, not overwriting it.");
409-
lastPersistedSessionId = null;
410-
return;
411-
}
412405
}
413406
final boolean written = writeSessionToDisk(currentSessionFile, session);
414-
// a failed write truncates the file, so there is no good copy left to protect. Delete it, so
415-
// a later move does not put it over the last run's session in the previous session file.
416-
if (!written && !currentSessionFile.delete()) {
407+
// A written file holds this session, even if the move failed, so the init-time move must
408+
// skip it. After a failed write, delete the truncated file only if the move worked: then it
409+
// is ours, and a later move must not put it over the last run's session. If the move failed,
410+
// the file can still hold the last run's session, so it stays for a later move to retry.
411+
if (written) {
412+
previousSessionMoved = true;
413+
} else if (previousSessionMoved && !currentSessionFile.delete()) {
417414
options
418415
.getLogger()
419416
.log(WARNING, "Unable to delete the current session file after a failed write.");

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -865,23 +865,49 @@ class EnvelopeCacheTest {
865865
}
866866

867867
@Test
868-
fun `failed move in persistCurrentSession keeps the last run's session for a later move`() {
868+
fun `failed move in persistCurrentSession still writes the live session for the init-time move to skip`() {
869869
val cache = fixture.getSUT()
870870
val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!)
871871
val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!)
872872
currentSessionFile.writeText("session content from last run")
873873
// a non-empty directory where the previous session file belongs makes the move fail
874874
assertTrue(File(previousSessionFile, "blocker").mkdirs())
875+
val liveSession = createSession()
876+
877+
cache.persistCurrentSession(liveSession)
878+
assertTrue(previousSessionFile.deleteRecursively())
879+
cache.movePreviousSession(currentSessionFile, previousSessionFile)
880+
881+
assertThat(previousSessionFile.exists()).isFalse()
882+
val persistedCurrent =
883+
fixture.options.serializer.deserialize(
884+
currentSessionFile.bufferedReader(),
885+
Session::class.java,
886+
)!!
887+
assertThat(persistedCurrent.sessionId).isEqualTo(liveSession.sessionId)
888+
}
875889

876-
cache.persistCurrentSession(createSession())
890+
@Test
891+
fun `failed move and failed write in persistCurrentSession leave the file for a later move`() {
892+
val liveSession = createSession()
893+
val serializer = mock<ISerializer>()
894+
whenever(serializer.serialize(same(liveSession), any<Writer>()))
895+
.thenThrow(RuntimeException("forced ex"))
896+
val cache = fixture.getSUT { options -> options.setSerializer(serializer) }
897+
val currentSessionFile = EnvelopeCache.getCurrentSessionFile(fixture.options.cacheDirPath!!)
898+
val previousSessionFile = EnvelopeCache.getPreviousSessionFile(fixture.options.cacheDirPath!!)
899+
currentSessionFile.writeText("session content from last run")
900+
assertTrue(File(previousSessionFile, "blocker").mkdirs())
877901

878-
assertThat(currentSessionFile.readText()).isEqualTo("session content from last run")
902+
cache.persistCurrentSession(liveSession)
903+
// the file may still be the last run's session, so it is not deleted
904+
assertThat(currentSessionFile.exists()).isTrue()
879905

880906
assertTrue(previousSessionFile.deleteRecursively())
881907
cache.movePreviousSession(currentSessionFile, previousSessionFile)
882908

883909
assertThat(currentSessionFile.exists()).isFalse()
884-
assertThat(previousSessionFile.readText()).isEqualTo("session content from last run")
910+
assertThat(previousSessionFile.exists()).isTrue()
885911
}
886912

887913
@Test

0 commit comments

Comments
 (0)