From 0898677a3d803bf7d82f62066c322b3be817ff54 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Mon, 30 Mar 2026 13:13:08 +1300 Subject: [PATCH 1/3] fix: Check file exists before recovering sessions --- src/Sentry/GlobalSessionManager.cs | 12 +++--- .../Sentry.Tests/GlobalSessionManagerTests.cs | 38 ++++++------------- 2 files changed, 17 insertions(+), 33 deletions(-) diff --git a/src/Sentry/GlobalSessionManager.cs b/src/Sentry/GlobalSessionManager.cs index 8fb6cb8413..6951fb6293 100644 --- a/src/Sentry/GlobalSessionManager.cs +++ b/src/Sentry/GlobalSessionManager.cs @@ -151,6 +151,12 @@ private void DeletePersistedSession() } var filePath = Path.Combine(_persistenceDirectoryPath, PersistedSessionFileName); + if (!_options.FileSystem.FileExists(filePath)) + { + _options.LogDebug("A persisted session file was not found at '{0}'.", filePath); + return null; + } + try { var recoveredUpdate = _persistedSessionProvider(filePath); @@ -195,12 +201,6 @@ private void DeletePersistedSession() return sessionUpdate; } - catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException) - { - // Not a notable error - _options.LogDebug("A persisted session does not exist ({0}) at {1}.", ex.GetType().Name, filePath); - return null; - } catch (Exception ex) { _options.LogError(ex, "Failed to recover persisted session from the file system '{0}'.", filePath); diff --git a/test/Sentry.Tests/GlobalSessionManagerTests.cs b/test/Sentry.Tests/GlobalSessionManagerTests.cs index 2649319a4f..08970a727b 100644 --- a/test/Sentry.Tests/GlobalSessionManagerTests.cs +++ b/test/Sentry.Tests/GlobalSessionManagerTests.cs @@ -261,45 +261,28 @@ public void TryRecoverPersistedSession_SessionNotStarted_ReturnsNull() } [Fact] - public void TryRecoverPersistedSession_FileNotFoundException_LogDebug() + public void TryRecoverPersistedSession_NoSessionFile_LogDebug() { // Arrange + _fixture.PersistedSessionProvider = _ => throw new FileNotFoundException(); var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new FileNotFoundException()); // Act sut.TryRecoverPersistedSession(); // Assert - _fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug); - } - - [Fact] - public void TryRecoverPersistedSession_DirectoryNotFoundException_LogDebug() - { - // Arrange - var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new DirectoryNotFoundException()); - - // Act - sut.TryRecoverPersistedSession(); - - // Assert - _fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug); + _fixture.Logger.Entries.Should().Contain(e => + e.Level == SentryLevel.Debug + && e.Message.Contains("A persisted session file was not found")); } [Fact] public void TryRecoverPersistedSession_EndOfStreamException_LogError() { // Arrange + _fixture.PersistedSessionProvider = _ => throw new EndOfStreamException(); var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new EndOfStreamException()); + sut.StartSession(); // Act sut.TryRecoverPersistedSession(); @@ -383,14 +366,14 @@ public void TryRecoverPersistedSession_SessionStarted_CrashDelegateReturnsFalse_ public void TryRecoverPersistedSession_CrashDelegateReturnsTrueWithPauseTimestamp_EndsAsCrashed() { // Arrange - _fixture.Options.CrashedLastRun = () => true; - // Session was paused before persisted: - var pausedTimestamp = DateTimeOffset.Now; + var pausedTimestamp = DateTimeOffset.Now; // Session was paused before persisted: _fixture.PersistedSessionProvider = _ => new PersistedSessionUpdate( AnySessionUpdate(), pausedTimestamp); + _fixture.Options.CrashedLastRun = () => true; var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -412,6 +395,7 @@ public void TryRecoverPersistedSession_CrashDelegateIsNullWithPauseTimestamp_End pausedTimestamp); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); From 5674b156c58571de4d4f79a1493cd15d97449071 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Mon, 30 Mar 2026 14:19:40 +1300 Subject: [PATCH 2/3] Fixed tests --- test/Sentry.Tests/GlobalSessionManagerTests.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/Sentry.Tests/GlobalSessionManagerTests.cs b/test/Sentry.Tests/GlobalSessionManagerTests.cs index 08970a727b..8143a5720c 100644 --- a/test/Sentry.Tests/GlobalSessionManagerTests.cs +++ b/test/Sentry.Tests/GlobalSessionManagerTests.cs @@ -366,11 +366,12 @@ public void TryRecoverPersistedSession_SessionStarted_CrashDelegateReturnsFalse_ public void TryRecoverPersistedSession_CrashDelegateReturnsTrueWithPauseTimestamp_EndsAsCrashed() { // Arrange - var pausedTimestamp = DateTimeOffset.Now; // Session was paused before persisted: + _fixture.Options.CrashedLastRun = () => true; + // Session was paused before persisted: + var pausedTimestamp = DateTimeOffset.Now; _fixture.PersistedSessionProvider = _ => new PersistedSessionUpdate( AnySessionUpdate(), pausedTimestamp); - _fixture.Options.CrashedLastRun = () => true; var sut = _fixture.GetSut(); sut.StartSession(); @@ -552,6 +553,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndNoCrash_EndsAsUnha pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -572,6 +574,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndCrash_EscalatesToC pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -593,6 +596,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndPauseTimestamp_Esc pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); From 9b6f0c25d864c30fd7a29a5c7b4eb155ad9d37f2 Mon Sep 17 00:00:00 2001 From: James Crosswell Date: Tue, 31 Mar 2026 11:30:52 +1300 Subject: [PATCH 3/3] Use more appropriate exception --- test/Sentry.Tests/GlobalSessionManagerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Sentry.Tests/GlobalSessionManagerTests.cs b/test/Sentry.Tests/GlobalSessionManagerTests.cs index 8143a5720c..7365fa1dac 100644 --- a/test/Sentry.Tests/GlobalSessionManagerTests.cs +++ b/test/Sentry.Tests/GlobalSessionManagerTests.cs @@ -264,7 +264,7 @@ public void TryRecoverPersistedSession_SessionNotStarted_ReturnsNull() public void TryRecoverPersistedSession_NoSessionFile_LogDebug() { // Arrange - _fixture.PersistedSessionProvider = _ => throw new FileNotFoundException(); + _fixture.PersistedSessionProvider = _ => throw new UnreachableException("Unexpected attempt to read a file that does not exist."); var sut = _fixture.GetSut(); // Act