Repository navigation
fix(aspnetcore): blocking-call detection no longer runs when CaptureBlockingCalls is false - #5970
jamescrosswell wants to merge 2 commits into
Conversation
…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>
There was a problem hiding this comment.
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 callsOnEventSourceCreatedwhile holdingEventListener.EventListenersLock, and that method then takes_enableLock.Enable()holds_enableLockand callsEnableEvents, which takesEventListenersLock(viaEventSource.SendCommand). This can only deadlock if the TPL source is created after construction whileEnable()runs at the same moment, so it's very unlikely. Still, callingEnableTplEventsoutside_enableLock(EnableEventsis 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 afterEnable(). 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>
|
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 linesStryker couldn't build 2 mutants on the changed lines, so no test ran against them.
Sentry.AspNetCore: tests caught 1 of 1 mutants on the changed lines |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
Summary
Since #5381 (6.8.0),
UseSentry()registersTaskBlockingListeneras a DI singleton for every app. It is anEventListenerthat enabled the TPL event source as soon as it was constructed and never checkedCaptureBlockingCalls. The only gate was thatSentryMiddlewaredidn'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 whenCaptureBlockingCallsis true. Constructing the listener no longer has a side effect.Notes
EventListener's base constructor callsOnEventSourceCreatedfor 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 onEnable(), or enables it on arrival if the source is created afterEnable().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_DoesNotCapturereproduces the report and fails without the fix.Fixes #5969
🤖 Generated with Claude Code