Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/Sentry/Internal/UnsampledTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ internal sealed class UnsampledTransaction : NoOpTransaction
private readonly IHub _hub;
private readonly ITransactionContext _context;
private readonly SentryOptions? _options;
private InterlockedBoolean _isFinished;

public UnsampledTransaction(IHub hub, ITransactionContext context)
{
Expand All @@ -32,7 +33,6 @@ public UnsampledTransaction(IHub hub, ITransactionContext context)

internal DynamicSamplingContext? DynamicSamplingContext { get; set; }

private bool _isFinished;
public override bool IsFinished => _isFinished;

public override IReadOnlyCollection<ISpan> Spans => _spans;
Expand Down Expand Up @@ -63,11 +63,14 @@ public override string Operation

public override void Finish()
{
_options?.LogDebug("Finishing unsampled transaction");

// Ensure the transaction is really cleared from the scope
// See: https://github.com/getsentry/sentry-dotnet/issues/4198
_isFinished = true;
if (_isFinished.Exchange(true))
{
return;
}

_options?.LogDebug("Finishing unsampled transaction");

// Clear the transaction from the scope and regenerate the Propagation Context, so new events don't have a
// trace context that is "older" than the transaction that just finished
Expand Down
39 changes: 39 additions & 0 deletions test/Sentry.Tests/Internals/UnsampledTransactionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,45 @@ namespace Sentry.Tests.Internals;

public class UnsampledTransactionTests
{
[Fact]
public void Dispose_Unfinished_Finishes()
{
// Arrange
var hub = Substitute.For<IHub>();
ITransactionContext context = new TransactionContext("TestTransaction", "TestOperation",
new SentryTraceHeader(SentryId.Create(), SpanId.Create(), false)
);
var transaction = new UnsampledTransaction(hub, context);

// Act
transaction.Dispose();

// Assert
transaction.IsFinished.Should().BeTrue();
hub.Received(1).ConfigureScope(Arg.Any<Action<Scope, UnsampledTransaction>>(),
Arg.Is<UnsampledTransaction>(t => ReferenceEquals(t, transaction)));
}

[Fact]
public void Dispose_Finished_DoesNothing()
{
// Arrange
var hub = Substitute.For<IHub>();
ITransactionContext context = new TransactionContext("TestTransaction", "TestOperation",
new SentryTraceHeader(SentryId.Create(), SpanId.Create(), false)
);
var transaction = new UnsampledTransaction(hub, context);
transaction.Finish();

// Act
transaction.Dispose();

// Assert
transaction.IsFinished.Should().BeTrue();
hub.Received(1).ConfigureScope(Arg.Any<Action<Scope, UnsampledTransaction>>(),
Arg.Is<UnsampledTransaction>(t => ReferenceEquals(t, transaction)));
}

[Fact]
public void StartChild_CreatesSpan_IsTrackedByParent()
{
Expand Down
Loading