From c62a5a6b15ee36f0cd6f6739ee8b0b2930463b7a Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 23 Nov 2023 11:44:21 +0100 Subject: [PATCH 1/3] hub passes 'GlobalSessionManager' to client --- src/Sentry/Internal/Hub.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Sentry/Internal/Hub.cs b/src/Sentry/Internal/Hub.cs index 30861ca298..f511d58568 100644 --- a/src/Sentry/Internal/Hub.cs +++ b/src/Sentry/Internal/Hub.cs @@ -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); From fd4a5770c41ba0b3434b39f4bc8f8e55a0a3369e Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 23 Nov 2023 16:56:47 +0100 Subject: [PATCH 2/3] tests --- test/Sentry.Tests/SentryClientTests.cs | 43 +++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/test/Sentry.Tests/SentryClientTests.cs b/test/Sentry.Tests/SentryClientTests.cs index 8743b0429b..8b5cc632a0 100644 --- a/test/Sentry.Tests/SentryClientTests.cs +++ b/test/Sentry.Tests/SentryClientTests.cs @@ -18,7 +18,7 @@ private class Fixture public IBackgroundWorker BackgroundWorker { get; set; } = Substitute.For(); public IClientReportRecorder ClientReportRecorder { get; } = Substitute.For(); - public ISessionManager SessionManager { get; } = Substitute.For(); + public ISessionManager SessionManager { get; set; } = Substitute.For(); public Fixture() { @@ -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 => + { + 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 => + { + 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() { From 4cc16638015acaa3049e866f1fd3b700759aa191 Mon Sep 17 00:00:00 2001 From: bitsandfoxes Date: Thu, 23 Nov 2023 17:02:41 +0100 Subject: [PATCH 3/3] Updated CHANGELOG.md --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 92f6f9373b..2efc76d470 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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))