Skip to content

Commit

Permalink
fix: Sessions not getting finished (#2895)
Browse files Browse the repository at this point in the history
  • Loading branch information
bitsandfoxes authored Nov 23, 2023
1 parent af2b9f5 commit faf01c9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- 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))

### Dependencies

- Bump Cocoa SDK from v8.16.0 to v8.16.1 ([#2891](https://github.com/getsentry/sentry-dotnet/pull/2891))
Expand Down
4 changes: 2 additions & 2 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ internal Hub(

_options = options;
_randomValuesFactory = randomValuesFactory ?? new SynchronizedRandomValuesFactory();
_ownedClient = client ?? new SentryClient(options, randomValuesFactory: _randomValuesFactory);
_clock = clock ?? SystemClock.Clock;
_sessionManager = sessionManager ?? new GlobalSessionManager(options);
_ownedClient = client ?? new SentryClient(options, randomValuesFactory: _randomValuesFactory, sessionManager: _sessionManager);
_clock = clock ?? SystemClock.Clock;

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

Expand Down
43 changes: 42 additions & 1 deletion test/Sentry.Tests/SentryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private class Fixture

public IBackgroundWorker BackgroundWorker { get; set; } = Substitute.For<IBackgroundWorker, IDisposable>();
public IClientReportRecorder ClientReportRecorder { get; } = Substitute.For<IClientReportRecorder>();
public ISessionManager SessionManager { get; } = Substitute.For<ISessionManager>();
public ISessionManager SessionManager { get; set; } = Substitute.For<ISessionManager>();

public Fixture()
{
Expand Down Expand Up @@ -699,6 +699,47 @@ public void CaptureEvent_Processing_Order()
processingOrder.Should().Equal(expectedOrder);
}

[Fact]
public void CaptureEvent_SessionRunningAndHasException_ReportsErrorButDoesNotEndSession()
{
_fixture.BackgroundWorker.EnqueueEnvelope(Arg.Do<Envelope>(envelope =>
{
var sessionItems = envelope.Items.Where(x => x.TryGetType() == "session");
foreach (var item in sessionItems)
{
var session = (SessionUpdate)((JsonSerializable)item.Payload).Source;
Assert.Equal(1, session.ErrorCount);
Assert.Null(session.EndStatus);
}
}));
_fixture.SessionManager = new GlobalSessionManager(_fixture.SentryOptions);
_fixture.SessionManager.StartSession();

_fixture.GetSut().CaptureEvent(new SentryEvent(new Exception("test exception")));
}

[Fact]
public void CaptureEvent_SessionRunningAndHasTerminalException_ReportsErrorAndEndsSessionAsCrashed()
{
_fixture.BackgroundWorker.EnqueueEnvelope(Arg.Do<Envelope>(envelope =>
{
var sessionItems = envelope.Items.Where(x => x.TryGetType() == "session");
foreach (var item in sessionItems)
{
var session = (SessionUpdate)((JsonSerializable)item.Payload).Source;
Assert.Equal(1, session.ErrorCount);
Assert.NotNull(session.EndStatus);
Assert.Equal(SessionEndStatus.Crashed, session.EndStatus);
}
}));
_fixture.SessionManager = new GlobalSessionManager(_fixture.SentryOptions);
_fixture.SessionManager.StartSession();

var exception = new Exception("test exception");
exception.SetSentryMechanism("test mechanism", handled: false);
_fixture.GetSut().CaptureEvent(new SentryEvent(exception));
}

[Fact]
public void CaptureEvent_Release_SetFromOptions()
{
Expand Down

0 comments on commit faf01c9

Please sign in to comment.