Skip to content

Commit 96d9d6c

Browse files
fix: Sentry Tracing middleware crashes ASP.NET Core in .NET 10 in version 6.0.0-rc1 and earlier (#4747)
1 parent 04e3890 commit 96d9d6c

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- Sentry Tracing middleware crashed ASP.NET Core in .NET 10 in 6.0.0-rc.1 and earlier ([#4747](https://github.com/getsentry/sentry-dotnet/pull/4747))
8+
59
### Dependencies
610

711
- Bump Java SDK from v8.24.0 to v8.26.0 ([#4728](https://github.com/getsentry/sentry-dotnet/pull/4728))

src/Sentry.AspNetCore/SentryTracingMiddleware.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ public async Task InvokeAsync(HttpContext context)
140140
catch (Exception e)
141141
{
142142
exception = e;
143+
// Rethrow immediately so as not to disrupt the .net 10 pipeline behaviour
144+
// See: https://github.com/getsentry/sentry-dotnet/issues/4735
145+
throw;
143146
}
144147
finally
145148
{
@@ -212,11 +215,6 @@ public async Task InvokeAsync(HttpContext context)
212215
transaction.Finish(exception, status);
213216
}
214217
}
215-
216-
if (exception is not null)
217-
{
218-
ExceptionDispatchInfo.Capture(exception).Throw();
219-
}
220218
}
221219
}
222220
}

test/Sentry.AspNetCore.Tests/SentryTracingMiddlewareTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,59 @@ public async Task Transaction_binds_exception_thrown()
614614
Assert.Equal(SpanStatus.InternalError, span?.Status);
615615
}
616616

617+
[Fact]
618+
public async Task ExceptionThrownAsync_DoesNotCrashKestrel()
619+
{
620+
var sentryClient = Substitute.For<ISentryClient>();
621+
var options = new SentryAspNetCoreOptions
622+
{
623+
Dsn = ValidDsn,
624+
TracesSampleRate = 1
625+
};
626+
627+
var hub = new Hub(options, sentryClient);
628+
629+
var server = new TestServer(new WebHostBuilder()
630+
.UseSentry()
631+
.ConfigureServices(services =>
632+
{
633+
services.RemoveAll(typeof(Func<IHub>));
634+
services.AddSingleton<Func<IHub>>(() => hub);
635+
services.AddRouting();
636+
}).Configure(app =>
637+
{
638+
app.UseRouting();
639+
app.UseSentryTracing();
640+
app.UseEndpoints(routes =>
641+
{
642+
routes.Map("/", _ => Task.CompletedTask);
643+
routes.Map("/crash", async _ =>
644+
{
645+
await Task.Yield();
646+
throw new Exception();
647+
});
648+
});
649+
}));
650+
651+
var client = server.CreateClient();
652+
653+
// Act
654+
try
655+
{
656+
await client.GetStringAsync("/crash");
657+
}
658+
// Expected error.
659+
catch
660+
{
661+
// ignored
662+
}
663+
664+
// Assert
665+
// Make sure Kestrel is still alive by making another request
666+
var response = await client.GetAsync("/");
667+
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
668+
}
669+
617670
[Fact]
618671
public async Task Transaction_TransactionNameProviderSetSet_TransactionNameSet()
619672
{

0 commit comments

Comments
 (0)