Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- 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))

## 6.0.0-rc.1

### BREAKING CHANGES
Expand Down
8 changes: 3 additions & 5 deletions src/Sentry.AspNetCore/SentryTracingMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public async Task InvokeAsync(HttpContext context)
catch (Exception e)
{
exception = e;
// Rethrow immediately so as not to disrupt the .net 10 pipeline behaviour
// See: https://github.com/getsentry/sentry-dotnet/issues/4735
throw;
}
finally
{
Expand Down Expand Up @@ -212,11 +215,6 @@ public async Task InvokeAsync(HttpContext context)
transaction.Finish(exception, status);
}
}

if (exception is not null)
{
ExceptionDispatchInfo.Capture(exception).Throw();
}
}
}
}
53 changes: 53 additions & 0 deletions test/Sentry.AspNetCore.Tests/SentryTracingMiddlewareTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -614,6 +614,59 @@ public async Task Transaction_binds_exception_thrown()
Assert.Equal(SpanStatus.InternalError, span?.Status);
}

[Fact]
public async Task ExceptionThrownAsync_DoesNotCrashKestrel()
{
var sentryClient = Substitute.For<ISentryClient>();
var options = new SentryAspNetCoreOptions
{
Dsn = ValidDsn,
TracesSampleRate = 1
};

var hub = new Hub(options, sentryClient);

var server = new TestServer(new WebHostBuilder()
.UseSentry()
.ConfigureServices(services =>
{
services.RemoveAll(typeof(Func<IHub>));
services.AddSingleton<Func<IHub>>(() => hub);
services.AddRouting();
}).Configure(app =>
{
app.UseRouting();
app.UseSentryTracing();
app.UseEndpoints(routes =>
{
routes.Map("/", _ => Task.CompletedTask);
routes.Map("/crash", async _ =>
{
await Task.Yield();
throw new Exception();
});
});
}));

var client = server.CreateClient();

// Act
try
{
await client.GetStringAsync("/crash");
}
// Expected error.
catch
{
// ignored
}

// Assert
// Make sure Kestrel is still alive by making another request
var response = await client.GetAsync("/");
response.StatusCode.Should().Be(System.Net.HttpStatusCode.OK);
}

[Fact]
public async Task Transaction_TransactionNameProviderSetSet_TransactionNameSet()
{
Expand Down
Loading