Skip to content
Open
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
13 changes: 11 additions & 2 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,17 @@ private SentryId CaptureEvent(SentryEvent evt, SentryHint? hint, Scope scope)
{
// Event contains a terminal exception -> finish any current transaction as aborted
// Do this *after* the event was captured, so that the event is still linked to the transaction.
_options.LogDebug("Ending transaction as Aborted, due to unhandled exception.");
transaction.Finish(SpanStatus.Aborted);
// Skip for OpenTelemetry transactions - these get handled by the SpanProcessor instead. See https://github.com/getsentry/sentry-dotnet/pull/5310
if (transaction is IBaseTracer { IsOtelInstrumenter: true })
{
_options.LogDebug(
"Not ending OpenTelemetry transaction as Aborted; it is finished by the SentrySpanProcessor.");
}
else
{
Comment on lines +673 to +677

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: If SentrySpanProcessor.OnEnd is not called after an exception, the associated OTel transaction will never be finished or sent, leading to lost data.
Severity: MEDIUM

Suggested Fix

Implement a fallback mechanism, such as a timeout, to ensure transactions are eventually finished and reported even if SentrySpanProcessor.OnEnd is not called. Consider adding documentation about the implicit dependency on the OnEnd event firing.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.

Location: src/Sentry/Internal/Hub.cs#L673-L677

Potential issue: The code change skips finishing OpenTelemetry-instrumented transactions
during unhandled exceptions, instead relying on `SentrySpanProcessor.OnEnd` to complete
them. However, the `OnEnd` event is not guaranteed to fire in all scenarios, especially
in non-ASP.NET Core contexts or with certain framework-level exceptions. If `OnEnd` is
not called, the transaction will never be finished or sent to Sentry, remaining in
memory until garbage collected. This leads to the silent loss of performance data for
failed requests without any fallback mechanism.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OnEnd is already the only path that finishes OTel transactions — every successful request relies on it, not just failed ones, so this doesn't add a new dependency. The cases where it wouldn't fire are (1) the Activity being sampled out, where we deliberately don't send a transaction, and (2) a process-terminating crash, where the flush is unreliable regardless and the only thing we'd have sent is the malformed transaction this PR removes.

For the ASP.NET Core path in the issue, the host always stops the activity, so OnEnd runs. A timeout fallback would just resurrect the early-finish bug.

_options.LogDebug("Ending transaction as Aborted, due to unhandled exception.");
transaction.Finish(SpanStatus.Aborted);
}
}

return id;
Expand Down
30 changes: 30 additions & 0 deletions test/Sentry.Tests/HubTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,36 @@ public void CaptureEvent_TerminalUnhandledException_AbortsActiveTransaction()
transaction.IsFinished.Should().BeTrue();
}

[Fact]
public void CaptureEvent_TerminalUnhandledException_DoesNotAbortOpenTelemetryTransaction()
{
// OpenTelemetry-instrumented transactions are owned and finished by the SentrySpanProcessor
// when the underlying Activity ends (which is also where the transaction name, operation and
// otel/response contexts get populated). Finishing them early here would capture them with the
// raw activity name and no otel context. See https://github.com/getsentry/sentry-dotnet/issues/5091

// Arrange
_fixture.Options.TracesSampleRate = 1.0;
_fixture.Options.Instrumenter = Instrumenter.OpenTelemetry;
var hub = _fixture.GetSut();

var transactionContext = new TransactionContext("test", "operation")
{
Instrumenter = Instrumenter.OpenTelemetry
};
var transaction = hub.StartTransaction(transactionContext);
hub.ConfigureScope(scope => scope.Transaction = transaction);

var exception = new Exception("test");
exception.SetSentryMechanism("test", handled: false, terminal: true);

// Act
hub.CaptureEvent(new SentryEvent(exception));

// Assert
transaction.IsFinished.Should().BeFalse();
}

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