Fix DiagnosticListener IsEnabled cyclical dispatch and lost activity hooks - #130582
Merged
Conversation
…hooks Fixes two bugs in DiagnosticListener reported in dotnet#125306: 1. The Subscribe overloads taking a Func<string, object?, object?, bool> isEnabled predicate passed 'name => IsEnabled(name, null, null)' as the 1-arg IsEnabled callback. This routed a plain IsEnabled(string) call back through the public IsEnabled(string, object?, object?) method, causing unnecessary indirection and incorrectly invoking subclass overrides of the 3-arg IsEnabled during a 1-arg call. The callback now closes over the caller's delegate directly. 2. DiagnosticSubscription.Remove rebuilt linked-list nodes without copying the OnActivityImport and OnActivityExport fields, silently dropping those activity hooks when an earlier-subscribed node was removed. Remove now copies both fields. Adds regression tests covering both scenarios. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @steveisok, @dotnet/area-system-diagnostics-tracing |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes two correctness issues in System.Diagnostics.DiagnosticSource’s DiagnosticListener subscription plumbing: (1) avoids routing the 1-arg IsEnabled(string) fast-path through the virtual 3-arg IsEnabled(string, object?, object?) override when a 3-arg predicate was supplied, and (2) preserves activity import/export hooks when rebuilding the immutable subscription linked list during removal. Adds targeted regression tests.
Changes:
- Update
Subscribe(..., Func<string, object?, object?, bool> ...)overloads to close over the caller-provided predicate directly for the 1-argIsEnabledcallback, avoiding unintended virtual dispatch/indirection. - Fix
DiagnosticSubscription.Removeto copyOnActivityImport/OnActivityExportwhen rebuilding nodes. - Add regression tests covering both the
IsEnabledrouting bug and the activity-hook loss on subscription removal.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/libraries/System.Diagnostics.DiagnosticSource/tests/DiagnosticSourceTests.cs | Adds regression tests for IsEnabled(string) routing and activity hook preservation across subscription removal. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticSourceActivity.cs | Fixes the activity-hook Subscribe overload’s 1-arg IsEnabled callback to invoke the provided predicate directly. |
| src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/DiagnosticListener.cs | Fixes the core Subscribe overload’s 1-arg IsEnabled callback and preserves activity hooks during subscription list rebuild in Remove. |
noahfalk
approved these changes
Jul 14, 2026
eiriktsarpalis
pushed a commit
that referenced
this pull request
Jul 15, 2026
…hooks (#130582) Fixes two bugs in DiagnosticListener reported in #125306: 1. The Subscribe overloads taking a Func<string, object?, object?, bool> isEnabled predicate passed 'name => IsEnabled(name, null, null)' as the 1-arg IsEnabled callback. This routed a plain IsEnabled(string) call back through the public IsEnabled(string, object?, object?) method, causing unnecessary indirection and incorrectly invoking subclass overrides of the 3-arg IsEnabled during a 1-arg call. The callback now closes over the caller's delegate directly. 2. DiagnosticSubscription.Remove rebuilt linked-list nodes without copying the OnActivityImport and OnActivityExport fields, silently dropping those activity hooks when an earlier-subscribed node was removed. Remove now copies both fields. Adds regression tests covering both scenarios. Fixes #125306 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes two bugs in DiagnosticListener reported in #125306:
The Subscribe overloads taking a Func<string, object?, object?, bool> isEnabled predicate passed 'name => IsEnabled(name, null, null)' as the 1-arg IsEnabled callback. This routed a plain IsEnabled(string) call back through the public IsEnabled(string, object?, object?) method, causing unnecessary indirection and incorrectly invoking subclass overrides of the 3-arg IsEnabled during a 1-arg call. The callback now closes over the caller's delegate directly.
DiagnosticSubscription.Remove rebuilt linked-list nodes without copying the OnActivityImport and OnActivityExport fields, silently dropping those activity hooks when an earlier-subscribed node was removed. Remove now copies both fields.
Adds regression tests covering both scenarios.
Fixes #125306