Skip to content

Blocking-call detection runs with CaptureBlockingCalls=false when anything resolves TaskBlockingListener (6.8.0+) #5969

Closed

Description

@JehutyPT

Package

Sentry.AspNetCore

.NET Flavor

.NET

.NET Version

10.0.302 (repro below). Seen in production on .NET 8 (Linux containers).

OS

Any (not platform specific)

OS Version

macOS 26.6 (repro), Linux (production)

Development Environment

Other

Other Error Monitoring Solution

No

SDK Version

7.0.0 (also applies to 6.8.0+, and main at 2e15186)

Self-Hosted Sentry Version

No response

Workload Versions

maui 10.0.20 is installed; the repro doesn't use it.

UseSentry or SentrySdk.Init call

builder.WebHost.UseSentry(o =>
{
    o.Dsn = "...";
    o.CaptureBlockingCalls = false; // the default
});

Steps to Reproduce

Since #5381 (6.8.0), UseSentry() registers the blocking detector as DI singletons for every app, whether or not CaptureBlockingCalls is enabled (SentryWebHostBuilderExtensions.cs#L113-L114):

.AddSingleton<IBlockingMonitor, BlockingMonitor>()
.AddSingleton<TaskBlockingListener>()

SentryMiddleware only resolves TaskBlockingListener when CaptureBlockingCalls is true. But TaskBlockingListener is an EventListener, so constructing it is enough to switch on the TPL event source for the whole process, and it never checks the option. Anything else that resolves it turns blocking detection on.

Some frameworks do that on purpose. Cortside.AspNetCore's AddApiDefaults adds a WarmupServicesStartupTask that calls GetServices for every registered singleton type at startup. After upgrading to Sentry 7.0.0, a service using it started sending "Blocking call detected" events at about 20 per minute, with CaptureBlockingCalls false. The events had mechanism BlockingCallDetector and source EventListener, and came from background services (AMQP and Redis connects), so they had no request or transaction.

Minimal repro (Sentry.AspNetCore 7.0.0, Microsoft.NET.Sdk.Web, net10.0):

var blockingEvents = 0;

var builder = WebApplication.CreateBuilder(args);
builder.WebHost.UseSentry(o =>
{
    o.Dsn = "https://key@127.0.0.1:9/1";  // unreachable; events are counted in BeforeSend
    o.CaptureBlockingCalls = false;          // the default, set explicitly
    o.SetBeforeSend(e =>
    {
        if (e.SentryExceptions?.Any(x => x.Mechanism?.Type == "BlockingCallDetector") == true)
            Interlocked.Increment(ref blockingEvents);
        return null;
    });
});
builder.Services.AddHostedService<Blocker>();

var app = builder.Build();

if (args.Contains("--warmup"))
{
    // What a "warm up all singletons" startup task does (e.g. Cortside.AspNetCore's WarmupServicesStartupTask).
    foreach (var type in builder.Services
                 .Where(d => d.Lifetime == ServiceLifetime.Singleton && !d.ServiceType.ContainsGenericParameters)
                 .Select(d => d.ServiceType).Distinct())
    {
        try { _ = app.Services.GetServices(type).ToList(); } catch { }
    }
}

await app.StartAsync();
await Task.Delay(5000);
Console.WriteLine($"warmup={args.Contains("--warmup")} blockingEvents={blockingEvents}");
await app.StopAsync();

class Blocker : BackgroundService
{
    protected override async Task ExecuteAsync(CancellationToken ct)
    {
        while (!ct.IsCancellationRequested)
        {
            await Task.Run(() => Task.Delay(20).Wait(), ct); // sync-over-async on a thread-pool thread
            await Task.Delay(500, ct);
        }
    }
}
  1. dotnet run
  2. dotnet run -- --warmup

Expected Result

With CaptureBlockingCalls = false, no blocking-call events in either run, and the TPL event source stays off.

Actual Result

warmup=False blockingEvents=0
warmup=True blockingEvents=10

With the warm-up, the listener gets constructed and every sync-over-async wait on a thread-pool thread is reported, even though the option is off. In our case that rate would have burned through the monthly error quota in under two days. The process also runs a Verbose TPL listener that nobody enabled.

Possible fixes:

  • Register the listener (and monitor) with a factory that only builds them when CaptureBlockingCalls is true, e.g. AddSingleton(sp => sp.GetRequiredService<IOptions<SentryAspNetCoreOptions>>().Value.CaptureBlockingCalls ? new TaskBlockingListener(...) : null!), or only register them when the option is on.
  • Or have TaskBlockingListener not enable the TPL source until something opts in, so constructing it has no side effect.

Workaround we're using: after UseSentry(...), replace the TaskBlockingListener service descriptor with a factory that returns null unless CaptureBlockingCalls is true. That relies on an internal type, so a fix in the SDK would be much better.

Activity

  1. linear-code commented on Nov 19, 2026

    @linear-code
  2. moved this to Waiting for: Product Owner in GitHub Issues with 👀 3on Nov 19, 2026
  3. added
    Bug
    .NET
    on Nov 19, 2026
  4. jamescrosswell commented on Nov 19, 2026

    @jamescrosswell
    Collaborator
  5. moved this from Waiting for: Product Owner to No status in GitHub Issues with 👀 3on Nov 19, 2026
  6. jamescrosswell commented on Nov 19, 2026

    @jamescrosswell
    Collaborator

    Thanks for the detailed report and the repro, they made this easy to pin down.

    Fixed in #5970, which will ship in the next release. TaskBlockingListener no longer enables the TPL event source when it's constructed. Only SentryMiddleware turns it on, and only when CaptureBlockingCalls is true, so resolving it from the container (e.g. a warm-up task) has no side effect. Once you've upgraded you can drop the descriptor-replacement workaround.

  7. added a commit that references this issue on Nov 20, 2026
    9242b20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

.NETBug

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions