Skip to content

Commit

Permalink
Hub.IsEnabled set to false when Hub disposed (#1021)
Browse files Browse the repository at this point in the history
* IsEnabled set to false when Hub disposed

* Review changes

* Hub over-dispose test

Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
semuserable and bruno-garcia authored Jun 3, 2021
1 parent 92dd408 commit 29d301a
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -247,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)
Expand Down
35 changes: 35 additions & 0 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -639,5 +639,40 @@ public void CaptureTransaction_AfterTransactionFinishes_ResetsTransactionOnScope
// Assert
hub.WithScope(scope => scope.Transaction.Should().BeNull());
}

[Fact]
public void Dispose_IsEnabled_SetToFalse()
{
// Arrange
var hub = new Hub(new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

hub.IsEnabled.Should().BeTrue();

// Act
hub.Dispose();

// Assert
hub.IsEnabled.Should().BeFalse();
}

[Fact]
public void Dispose_CalledSecondTime_ClientDisposedOnce()
{
var client = Substitute.For<ISentryClient, IDisposable>();
var hub = new Hub(client, new SentryOptions
{
Dsn = DsnSamples.ValidDsnWithSecret
});

// Act
hub.Dispose();
hub.Dispose();

// Assert
(client as IDisposable).Received(1).Dispose();
}
}
}

0 comments on commit 29d301a

Please sign in to comment.