Skip to content

Commit 55c41fc

Browse files
fix: Workaround FileNotFoundException on Android when recovering sessions (#5084)
1 parent 82e0436 commit 55c41fc

File tree

2 files changed

+18
-30
lines changed

2 files changed

+18
-30
lines changed

src/Sentry/GlobalSessionManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,12 @@ private void DeletePersistedSession()
151151
}
152152

153153
var filePath = Path.Combine(_persistenceDirectoryPath, PersistedSessionFileName);
154+
if (!_options.FileSystem.FileExists(filePath))
155+
{
156+
_options.LogDebug("A persisted session file was not found at '{0}'.", filePath);
157+
return null;
158+
}
159+
154160
try
155161
{
156162
var recoveredUpdate = _persistedSessionProvider(filePath);
@@ -195,12 +201,6 @@ private void DeletePersistedSession()
195201

196202
return sessionUpdate;
197203
}
198-
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException)
199-
{
200-
// Not a notable error
201-
_options.LogDebug("A persisted session does not exist ({0}) at {1}.", ex.GetType().Name, filePath);
202-
return null;
203-
}
204204
catch (Exception ex)
205205
{
206206
_options.LogError(ex, "Failed to recover persisted session from the file system '{0}'.", filePath);

test/Sentry.Tests/GlobalSessionManagerTests.cs

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -261,45 +261,28 @@ public void TryRecoverPersistedSession_SessionNotStarted_ReturnsNull()
261261
}
262262

263263
[Fact]
264-
public void TryRecoverPersistedSession_FileNotFoundException_LogDebug()
264+
public void TryRecoverPersistedSession_NoSessionFile_LogDebug()
265265
{
266266
// Arrange
267+
_fixture.PersistedSessionProvider = _ => throw new UnreachableException("Unexpected attempt to read a file that does not exist.");
267268
var sut = _fixture.GetSut();
268-
sut = new GlobalSessionManager(
269-
_fixture.Options,
270-
persistedSessionProvider: _ => throw new FileNotFoundException());
271269

272270
// Act
273271
sut.TryRecoverPersistedSession();
274272

275273
// Assert
276-
_fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug);
277-
}
278-
279-
[Fact]
280-
public void TryRecoverPersistedSession_DirectoryNotFoundException_LogDebug()
281-
{
282-
// Arrange
283-
var sut = _fixture.GetSut();
284-
sut = new GlobalSessionManager(
285-
_fixture.Options,
286-
persistedSessionProvider: _ => throw new DirectoryNotFoundException());
287-
288-
// Act
289-
sut.TryRecoverPersistedSession();
290-
291-
// Assert
292-
_fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug);
274+
_fixture.Logger.Entries.Should().Contain(e =>
275+
e.Level == SentryLevel.Debug
276+
&& e.Message.Contains("A persisted session file was not found"));
293277
}
294278

295279
[Fact]
296280
public void TryRecoverPersistedSession_EndOfStreamException_LogError()
297281
{
298282
// Arrange
283+
_fixture.PersistedSessionProvider = _ => throw new EndOfStreamException();
299284
var sut = _fixture.GetSut();
300-
sut = new GlobalSessionManager(
301-
_fixture.Options,
302-
persistedSessionProvider: _ => throw new EndOfStreamException());
285+
sut.StartSession();
303286

304287
// Act
305288
sut.TryRecoverPersistedSession();
@@ -391,6 +374,7 @@ public void TryRecoverPersistedSession_CrashDelegateReturnsTrueWithPauseTimestam
391374
pausedTimestamp);
392375

393376
var sut = _fixture.GetSut();
377+
sut.StartSession();
394378

395379
// Act
396380
var persistedSessionUpdate = sut.TryRecoverPersistedSession();
@@ -412,6 +396,7 @@ public void TryRecoverPersistedSession_CrashDelegateIsNullWithPauseTimestamp_End
412396
pausedTimestamp);
413397

414398
var sut = _fixture.GetSut();
399+
sut.StartSession();
415400

416401
// Act
417402
var persistedSessionUpdate = sut.TryRecoverPersistedSession();
@@ -568,6 +553,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndNoCrash_EndsAsUnha
568553
pendingUnhandled: true);
569554

570555
var sut = _fixture.GetSut();
556+
sut.StartSession();
571557

572558
// Act
573559
var persistedSessionUpdate = sut.TryRecoverPersistedSession();
@@ -588,6 +574,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndCrash_EscalatesToC
588574
pendingUnhandled: true);
589575

590576
var sut = _fixture.GetSut();
577+
sut.StartSession();
591578

592579
// Act
593580
var persistedSessionUpdate = sut.TryRecoverPersistedSession();
@@ -609,6 +596,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndPauseTimestamp_Esc
609596
pendingUnhandled: true);
610597

611598
var sut = _fixture.GetSut();
599+
sut.StartSession();
612600

613601
// Act
614602
var persistedSessionUpdate = sut.TryRecoverPersistedSession();

0 commit comments

Comments
 (0)