From 84cbf15fb84a805f8c2c8138cc2eeaa4f98b7482 Mon Sep 17 00:00:00 2001 From: semuserable Date: Mon, 31 May 2021 20:07:18 +0300 Subject: [PATCH 1/3] IsEnabled set to false when Hub disposed --- src/Sentry/Internal/Hub.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Internal/Hub.cs b/src/Sentry/Internal/Hub.cs index 48326fce9f..b8afce957d 100644 --- a/src/Sentry/Internal/Hub.cs +++ b/src/Sentry/Internal/Hub.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using Sentry.Extensibility; using Sentry.Integrations; @@ -19,7 +20,8 @@ internal class Hub : IHub, IDisposable internal SentryScopeManager ScopeManager { get; } - public bool IsEnabled => true; + private int _isEnabled = 1; + public bool IsEnabled => _isEnabled == 1; internal Hub(ISentryClient client, SentryOptions options) { @@ -258,6 +260,7 @@ public void Dispose() } } + Interlocked.Exchange(ref _isEnabled, 0); (_ownedClient as IDisposable)?.Dispose(); _rootScope.Dispose(); ScopeManager.Dispose(); From 78023c2d85b4206b6508092ee293283e156b0142 Mon Sep 17 00:00:00 2001 From: semuserable Date: Wed, 2 Jun 2021 10:57:03 +0300 Subject: [PATCH 2/3] Review changes --- src/Sentry/Internal/Hub.cs | 6 +++++- test/Sentry.Tests/HubTests.cs | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Sentry/Internal/Hub.cs b/src/Sentry/Internal/Hub.cs index b8afce957d..f05814bb67 100644 --- a/src/Sentry/Internal/Hub.cs +++ b/src/Sentry/Internal/Hub.cs @@ -249,6 +249,11 @@ public void Dispose() { _options.DiagnosticLogger?.LogInfo("Disposing the Hub."); + if (Interlocked.Exchange(ref _isEnabled, 0) != 1) + { + return; + } + if (_integrations?.Length > 0) { foreach (var integration in _integrations) @@ -260,7 +265,6 @@ public void Dispose() } } - Interlocked.Exchange(ref _isEnabled, 0); (_ownedClient as IDisposable)?.Dispose(); _rootScope.Dispose(); ScopeManager.Dispose(); diff --git a/test/Sentry.Tests/HubTests.cs b/test/Sentry.Tests/HubTests.cs index fb2900a43a..9062b90c9b 100644 --- a/test/Sentry.Tests/HubTests.cs +++ b/test/Sentry.Tests/HubTests.cs @@ -639,5 +639,26 @@ public void CaptureTransaction_AfterTransactionFinishes_ResetsTransactionOnScope // Assert hub.WithScope(scope => scope.Transaction.Should().BeNull()); } + + [Theory] + [InlineData(1)] + [InlineData(2)] + public void Dispose_IsEnabled_SetToFalse(int disposeCount) + { + // Arrange + var hub = new Hub(new SentryOptions + { + Dsn = DsnSamples.ValidDsnWithSecret + }); + + // Act + for (var i = 0; i < disposeCount; i++) + { + hub.Dispose(); + } + + // Assert + hub.IsEnabled.Should().BeFalse(); + } } } From 1ed456bc189c2fc657b95f94fda44e17a3906c04 Mon Sep 17 00:00:00 2001 From: semuserable Date: Thu, 3 Jun 2021 11:45:18 +0300 Subject: [PATCH 3/3] Hub over-dispose test --- test/Sentry.Tests/HubTests.cs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/test/Sentry.Tests/HubTests.cs b/test/Sentry.Tests/HubTests.cs index 9062b90c9b..b456f4838e 100644 --- a/test/Sentry.Tests/HubTests.cs +++ b/test/Sentry.Tests/HubTests.cs @@ -640,10 +640,8 @@ public void CaptureTransaction_AfterTransactionFinishes_ResetsTransactionOnScope hub.WithScope(scope => scope.Transaction.Should().BeNull()); } - [Theory] - [InlineData(1)] - [InlineData(2)] - public void Dispose_IsEnabled_SetToFalse(int disposeCount) + [Fact] + public void Dispose_IsEnabled_SetToFalse() { // Arrange var hub = new Hub(new SentryOptions @@ -651,14 +649,30 @@ public void Dispose_IsEnabled_SetToFalse(int disposeCount) Dsn = DsnSamples.ValidDsnWithSecret }); + hub.IsEnabled.Should().BeTrue(); + // Act - for (var i = 0; i < disposeCount; i++) - { - hub.Dispose(); - } + hub.Dispose(); // Assert hub.IsEnabled.Should().BeFalse(); } + + [Fact] + public void Dispose_CalledSecondTime_ClientDisposedOnce() + { + var client = Substitute.For(); + var hub = new Hub(client, new SentryOptions + { + Dsn = DsnSamples.ValidDsnWithSecret + }); + + // Act + hub.Dispose(); + hub.Dispose(); + + // Assert + (client as IDisposable).Received(1).Dispose(); + } } }