Skip to content

fix(aspnetcore): blocking-call detection no longer runs when CaptureBlockingCalls is false - #5970

Draft
jamescrosswell wants to merge 2 commits into
mainfrom
ai/sentry-dotnet-5969-9c5034
Draft

jamescrosswell wants to merge 2 commits into
mainfrom
ai/sentry-dotnet-5969-9c5034

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

Summary

Since #5381 (6.8.0), UseSentry() registers TaskBlockingListener as a DI singleton for every app. It is an EventListener that enabled the TPL event source as soon as it was constructed and never checked CaptureBlockingCalls. The only gate was that SentryMiddleware didn't resolve it when the option was off, so anything else that resolved it (for example a startup task that warms up every registered singleton) turned blocking detection on for the whole process.

The listener is now inert until Enable() is called, and the middleware calls that only when CaptureBlockingCalls is true. Constructing the listener no longer has a side effect.

Notes

  • EventListener's base constructor calls OnEventSourceCreated for existing sources before the derived constructor body runs, so the listener can't simply take the option as a constructor argument and read it there. It remembers the TPL source and enables it on Enable(), or enables it on arrival if the source is created after Enable().
  • The listener stays a DI singleton, so the container still disposes it. A Lazy<TaskBlockingListener> registration would also have fixed the report but loses that disposal.
  • Enable() is called from the transient middleware's constructor, so once per request; after the first call it is a single volatile read.
  • InvokeAsync_BlockingCallDetectionDisabled_ListenerResolvedElsewhere_DoesNotCapture reproduces the report and fails without the fix.

Fixes #5969

🤖 Generated with Claude Code

…lockingCalls is false

TaskBlockingListener is registered as a DI singleton for every app and
enabled the TPL event source as soon as it was constructed. Anything that
resolved it (e.g. a "warm up all singletons" startup task) switched
blocking detection on for the whole process, regardless of the option.

The listener now stays inert until Enable() is called, which the
middleware only does when CaptureBlockingCalls is true.

Fixes #5969

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@sentry-junior sentry-junior Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to approve, with optional comments

Fixes the root cause of #5969: constructing TaskBlockingListener no longer enables the TPL event source, and only SentryMiddleware turns it on when CaptureBlockingCalls is true. The new integration test reproduces the reported scenario (listener resolved elsewhere while the option is off).

  • Optional: src/Sentry/Ben.BlockingDetector/TaskBlockingListener.cs: the two locks are taken in opposite orders. The runtime calls OnEventSourceCreated while holding EventListener.EventListenersLock, and that method then takes _enableLock. Enable() holds _enableLock and calls EnableEvents, which takes EventListenersLock (via EventSource.SendCommand). This can only deadlock if the TPL source is created after construction while Enable() runs at the same moment, so it's very unlikely. Still, calling EnableTplEvents outside _enableLock (EnableEvents is idempotent, so enabling twice is harmless) would remove the inversion completely.
  • Optional: test/Sentry.Tests/Ben.BlockingDetector/TaskBlockingListenerTests.cs: no test covers the path where the TPL source is created after Enable(). It's hard to trigger in-process because the source already exists, so accepting it untested seems reasonable.

This is an automated preliminary review.

OnEventSourceCreated runs under EventListener.EventListenersLock and
took _enableLock, while Enable() held _enableLock and called
EnableEvents, which takes EventListenersLock. Use two volatile fields
instead: each side writes its own and then reads the other's, so at
least one of them enables the source, and EnableEvents is idempotent.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

Mutation testing breaks the lines a PR changes on purpose, one mutation at a time, and checks that a test fails each time. A changed line no test runs fails the check, and a mutant that survives the tests only warns. Full report

Sentry: tests caught 4 of 7 mutants on the changed lines

Stryker couldn't build 2 mutants on the changed lines, so no test ran against them.

Location Mutation Replaced with Status
src/Sentry/Ben.BlockingDetector/TaskBlockingListener.cs:44 Statement mutation ; NoCoverage
src/Sentry/Ben.BlockingDetector/TaskBlockingListener.cs:52 Statement mutation ; Survived
src/Sentry/Ben.BlockingDetector/TaskBlockingListener.cs:55 Boolean mutation false Survived

Sentry.AspNetCore: tests caught 1 of 1 mutants on the changed lines

@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 77.70%. Comparing base (1fcd056) to head (c16812a).

Files with missing lines Patch % Lines
...entry/Ben.BlockingDetector/TaskBlockingListener.cs 83.33% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5970   +/-   ##
=======================================
  Coverage   77.69%   77.70%           
=======================================
  Files         535      535           
  Lines       19727    19738   +11     
  Branches     3932     3935    +3     
=======================================
+ Hits        15327    15337   +10     
  Misses       3545     3545           
- Partials      855      856    +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant