Skip to content

Commit 41cf450

Browse files
lucas-zimermangetsentry-botbruno-garcia
authored
Fix EF Core garbage collected messages and ordering. (#1368)
Co-authored-by: Sentry Github Bot <[email protected]> Co-authored-by: Bruno Garcia <[email protected]>
1 parent 1605e97 commit 41cf450

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Fixes
1212

13+
- Fix EF Core garbage collected messages and ordering ([#1368](https://github.com/getsentry/sentry-dotnet/pull/1368))
1314
- Update X-Sentry-Auth header to include correct sdk name and version ([#1333](https://github.com/getsentry/sentry-dotnet/pull/1333))
1415

1516
## 3.12.0

src/Sentry.DiagnosticSource/Internals/DiagnosticSource/SentryEFCoreListener.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
#if NETCOREAPP3_0_OR_GREATER
12
using System;
23
using System.Collections.Generic;
34
using System.Threading;
5+
#endif
46
using Sentry.Extensibility;
57

68
namespace Sentry.Internals.DiagnosticSource
@@ -55,6 +57,9 @@ public SentryEFCoreListener(IHub hub, SentryOptions options)
5557

5658
internal bool DisableQuerySpan() => _logQueryEnabled = false;
5759

60+
private ISpan? GetParent(SentryEFSpanType type, Scope scope)
61+
=> type == SentryEFSpanType.QueryExecution ? scope.GetSpan() : scope.Transaction;
62+
5863
private ISpan? AddSpan(SentryEFSpanType type, string operation, string? description)
5964
{
6065
ISpan? span = null;
@@ -65,7 +70,7 @@ public SentryEFCoreListener(IHub hub, SentryOptions options)
6570
return;
6671
}
6772

68-
if (scope.GetSpan()?.StartChild(operation, description) is not { } startedChild)
73+
if (GetParent(type, scope)?.StartChild(operation, description) is not { } startedChild)
6974
{
7075
return;
7176
}
@@ -93,7 +98,10 @@ public SentryEFCoreListener(IHub hub, SentryOptions options)
9398
{
9499
span = startedSpan;
95100
}
96-
_options.LogWarning("Trying to close a span that was already garbage collected. {0}", type);
101+
else
102+
{
103+
_options.LogWarning("Trying to close a span that was already garbage collected. {0}", type);
104+
}
97105
}
98106
});
99107
return span;

test/Sentry.DiagnosticSource.Tests/SentryEFCoreListenerTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ public void OnNext_HappyPath_IsValid()
229229
Assert.Equal(_fixture.Tracer.SpanId, compilerSpan.ParentSpanId);
230230
Assert.Equal(_fixture.Tracer.SpanId, connectionSpan.ParentSpanId);
231231
Assert.Equal(connectionSpan.SpanId, commandSpan.ParentSpanId);
232+
_fixture.Options.DiagnosticLogger.Received(0).Log(Arg.Is(SentryLevel.Warning), Arg.Is("Trying to close a span that was already garbage collected. {0}"), null, Arg.Any<object[]>());
232233
}
233234

234235
[Fact]
@@ -319,6 +320,24 @@ public void OnNext_ThrowsException_ExceptionIsolated()
319320
Assert.False(exceptionReceived);
320321
}
321322

323+
324+
[Theory]
325+
[InlineData(EFCommandExecuted)]
326+
[InlineData(EFConnectionClosed)]
327+
[InlineData(EFQueryCompiled)]
328+
public void OnNext_TakeSpanWithoutSpan_ShowsGarbageCollectorError(string operation)
329+
{
330+
// Arrange
331+
var hub = _fixture.Hub;
332+
var interceptor = new SentryEFCoreListener(hub, _fixture.Options);
333+
334+
// Act
335+
interceptor.OnNext(new(operation, "ef Junk\r\nSELECT * FROM ..."));
336+
337+
// Assert
338+
_fixture.Options.DiagnosticLogger.Received(1).Log(Arg.Is(SentryLevel.Warning), Arg.Is("Trying to close a span that was already garbage collected. {0}"), null, Arg.Any<object[]>());
339+
}
340+
322341
[Fact]
323342
public void FilterNewLineValue_StringWithNewLine_SubStringAfterNewLine()
324343
{

0 commit comments

Comments
 (0)