Skip to content

Commit faf01c9

Browse files
authored
fix: Sessions not getting finished (#2895)
1 parent af2b9f5 commit faf01c9

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- The SDK no longer fails to finish sessions while capturing an event. This fixes broken crash-free rates ([#2895](https://github.com/getsentry/sentry-dotnet/pull/2895))
8+
59
### Dependencies
610

711
- Bump Cocoa SDK from v8.16.0 to v8.16.1 ([#2891](https://github.com/getsentry/sentry-dotnet/pull/2891))

src/Sentry/Internal/Hub.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ internal Hub(
4747

4848
_options = options;
4949
_randomValuesFactory = randomValuesFactory ?? new SynchronizedRandomValuesFactory();
50-
_ownedClient = client ?? new SentryClient(options, randomValuesFactory: _randomValuesFactory);
51-
_clock = clock ?? SystemClock.Clock;
5250
_sessionManager = sessionManager ?? new GlobalSessionManager(options);
51+
_ownedClient = client ?? new SentryClient(options, randomValuesFactory: _randomValuesFactory, sessionManager: _sessionManager);
52+
_clock = clock ?? SystemClock.Clock;
5353

5454
ScopeManager = scopeManager ?? new SentryScopeManager(options, _ownedClient);
5555

test/Sentry.Tests/SentryClientTests.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private class Fixture
1818

1919
public IBackgroundWorker BackgroundWorker { get; set; } = Substitute.For<IBackgroundWorker, IDisposable>();
2020
public IClientReportRecorder ClientReportRecorder { get; } = Substitute.For<IClientReportRecorder>();
21-
public ISessionManager SessionManager { get; } = Substitute.For<ISessionManager>();
21+
public ISessionManager SessionManager { get; set; } = Substitute.For<ISessionManager>();
2222

2323
public Fixture()
2424
{
@@ -699,6 +699,47 @@ public void CaptureEvent_Processing_Order()
699699
processingOrder.Should().Equal(expectedOrder);
700700
}
701701

702+
[Fact]
703+
public void CaptureEvent_SessionRunningAndHasException_ReportsErrorButDoesNotEndSession()
704+
{
705+
_fixture.BackgroundWorker.EnqueueEnvelope(Arg.Do<Envelope>(envelope =>
706+
{
707+
var sessionItems = envelope.Items.Where(x => x.TryGetType() == "session");
708+
foreach (var item in sessionItems)
709+
{
710+
var session = (SessionUpdate)((JsonSerializable)item.Payload).Source;
711+
Assert.Equal(1, session.ErrorCount);
712+
Assert.Null(session.EndStatus);
713+
}
714+
}));
715+
_fixture.SessionManager = new GlobalSessionManager(_fixture.SentryOptions);
716+
_fixture.SessionManager.StartSession();
717+
718+
_fixture.GetSut().CaptureEvent(new SentryEvent(new Exception("test exception")));
719+
}
720+
721+
[Fact]
722+
public void CaptureEvent_SessionRunningAndHasTerminalException_ReportsErrorAndEndsSessionAsCrashed()
723+
{
724+
_fixture.BackgroundWorker.EnqueueEnvelope(Arg.Do<Envelope>(envelope =>
725+
{
726+
var sessionItems = envelope.Items.Where(x => x.TryGetType() == "session");
727+
foreach (var item in sessionItems)
728+
{
729+
var session = (SessionUpdate)((JsonSerializable)item.Payload).Source;
730+
Assert.Equal(1, session.ErrorCount);
731+
Assert.NotNull(session.EndStatus);
732+
Assert.Equal(SessionEndStatus.Crashed, session.EndStatus);
733+
}
734+
}));
735+
_fixture.SessionManager = new GlobalSessionManager(_fixture.SentryOptions);
736+
_fixture.SessionManager.StartSession();
737+
738+
var exception = new Exception("test exception");
739+
exception.SetSentryMechanism("test mechanism", handled: false);
740+
_fixture.GetSut().CaptureEvent(new SentryEvent(exception));
741+
}
742+
702743
[Fact]
703744
public void CaptureEvent_Release_SetFromOptions()
704745
{

0 commit comments

Comments
 (0)