Skip to content

Commit ffeef7f

Browse files
authored
fix: Don't set current thread when one is already there (#4681)
1 parent 6200d99 commit ffeef7f

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Sentry/Internal/MainSentryEventProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public SentryEvent Process(SentryEvent @event)
8383
@event.Release ??= Release;
8484
@event.Distribution ??= Distribution;
8585

86-
// if there's no exception with a stack trace, then get the current stack trace
87-
if (@event.Exception?.StackTrace is null)
86+
// If there's no current thread on the event and there is no exception with a stack trace, get the current stack trace
87+
if (@event.Exception?.StackTrace is null && @event.SentryThreads?.Any(t => t.Current == true) != true)
8888
{
8989
var stackTrace = @event.SentryExceptions?.FirstOrDefault()?.Stacktrace
9090
?? SentryStackTraceFactoryAccessor().Create();

test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,34 @@ public void Process_AttachStacktraceTrueAndExistentThreadInEvent_AddsNewThread()
491491
Assert.Equal("second", evt.SentryThreads.Last().Name);
492492
}
493493

494+
[Fact]
495+
public void Process_AttachStacktraceTrueAndCurrentThreadAlreadyExists_DoesNotAddAnotherThread()
496+
{
497+
var existingStackTrace = new SentryStackTrace();
498+
_fixture.SentryOptions.AttachStacktrace = true;
499+
var sut = _fixture.GetSut();
500+
501+
var evt = new SentryEvent
502+
{
503+
SentryThreads = new[]
504+
{
505+
new SentryThread
506+
{
507+
Name = "existing-thread",
508+
Current = true,
509+
Stacktrace = existingStackTrace
510+
}
511+
}
512+
};
513+
_ = sut.Process(evt);
514+
515+
Assert.Single(evt.SentryThreads);
516+
Assert.Equal("existing-thread", evt.SentryThreads.First().Name);
517+
Assert.True(evt.SentryThreads.First().Current);
518+
Assert.Same(existingStackTrace, evt.SentryThreads.First().Stacktrace);
519+
_ = _fixture.SentryStackTraceFactory.DidNotReceive().Create();
520+
}
521+
494522
[Fact]
495523
public void Process_CultureInfoAndCultureInfoAreEqual_OnlyCultureInfoSet()
496524
{

0 commit comments

Comments
 (0)