-
-
Notifications
You must be signed in to change notification settings - Fork 223
fix: Removed redundant scope sync after transaction finish #4479
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice - thanks @bitsandfoxes !
My only question is about how we want to approach the fix. What you've done will work... it does depend on us always doing this:
scope.ResetTransaction(transactionTracer);
scope.SetPropagationContext(new SentryPropagationContext());
What's more, if there's a race and this line returns false:
sentry-dotnet/src/Sentry/Scope.cs
Line 818 in 34d8490
if (ReferenceEquals(_transaction.Value, expectedCurrentTransaction)) |
Then we could end up running this multiple times:
scope.SetPropagationContext(new SentryPropagationContext());
Alternative solution
We could possibly rewrite ResetTransaction
to do this:
if (ReferenceEquals(_transaction.Value, expectedCurrentTransaction))
{
_transaction.Value = null;
SetPropagationContext(new SentryPropagationContext());
}
So both the scope sync and the resetting of the propagation context always happen if you call RestTransaction
(but only once, since it's in a lock that will prevent races).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Trace Context Sync Issue in ResetTransaction
Removing the scope synchronization from ResetTransaction
creates an implicit dependency on the calling code to synchronize the native layer's trace context. Without a subsequent call to SetPropagationContext
, the native layer can become out of sync with the managed scope, potentially leading to incorrect trace context in native-generated events.
src/Sentry/Scope.cs#L812-L827
sentry-dotnet/src/Sentry/Scope.cs
Lines 812 to 827 in 7336a87
internal void ResetTransaction(ITransactionTracer? expectedCurrentTransaction) | |
{ | |
_transactionLock.EnterWriteLock(); | |
try | |
{ | |
if (ReferenceEquals(_transaction.Value, expectedCurrentTransaction)) | |
{ | |
_transaction.Value = null; | |
} | |
} | |
finally | |
{ | |
_transactionLock.ExitWriteLock(); | |
} | |
} |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4479 +/- ##
==========================================
+ Coverage 73.38% 73.40% +0.01%
==========================================
Files 479 479
Lines 17506 17504 -2
Branches 3479 3477 -2
==========================================
+ Hits 12846 12848 +2
+ Misses 3780 3778 -2
+ Partials 880 878 -2 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Problem Description
When a transaction gets finished it also resets itself on the scope. This triggers a "manual" scope sync to the native layer. After finishing a transaction the
PropagationContext
get renewed to keep new events from having an old trace context.The SDK syncs the trace in immediate succession, overwriting itself every time.
Context
The logs are taken from a Unity game but should apply to anywhere there is a transaction that gets finished
After a transaction gets finished it gets reset on the
Scope
and afterwards thePropagationContext
gets regenerated so that new events don't have a trace context older than the just finished transaction. We even point this out in the comments.There are two places the transaction gets reset on the scope:
UnsampledTransaction
sentry-dotnet/src/Sentry/Internal/UnsampledTransaction.cs
Lines 65 to 71 in 6eedb71
TransactionTracer
sentry-dotnet/src/Sentry/TransactionTracer.cs
Lines 386 to 392 in 6eedb71
Proposal
The SDK does not need to manually restore the trace context on the native layer, as it gets overwritten basically in the next line.