Fix KeyNotFoundException on HttpRequestLatencyListener.OnEventWritten for uknown event sources#6821
Merged
dariusclay merged 3 commits intodotnet:mainfrom Sep 23, 2025
Conversation
… for unknown event sources
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes a KeyNotFoundException that occurs in HttpRequestLatencyListener.OnEventWritten when handling unknown event sources. The issue arises when events like EventCounters bypass the enabled events check, causing the method to attempt dictionary access with a non-existent key.
- Adds safe dictionary access to prevent KeyNotFoundException when unknown event sources are encountered
- Includes test coverage to verify the fix handles unknown events correctly
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| HttpRequestLatencyListener.cs | Implements safe dictionary access using TryGetValue to prevent KeyNotFoundException |
| HttpRequestLatencyListenerTest.cs | Adds test to verify unknown events don't cause exceptions or add checkpoints |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
...crosoft.Extensions.Http.Diagnostics.Tests/Latency/Internal/HttpRequestLatencyListenerTest.cs
Outdated
Show resolved
Hide resolved
evgenyfedorov2
approved these changes
Sep 19, 2025
dariusclay
approved these changes
Sep 22, 2025
…ency/Internal/HttpRequestLatencyListenerTest.cs Co-authored-by: Copilot <[email protected]>
…n_for_unknown_events
Contributor
Author
|
@dotnet-policy-service agree company="Microsoft" |
This was referenced Oct 14, 2025
Closed
Merged
This was referenced Oct 22, 2025
Closed
Merged
Merged
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Description
The current implementation of HttpRequestLatencyListener.OnEventWritten always expects that eventSourceName should be available in the _eventToTokenMap dictionary.
Certain events (e.g., EventCounters) bypass the enabled events check, which results in OnEventWritten throwing a KeyNotFoundException.
https://github.com/dotnet/runtime/blob/ac8354e3e94f04fae2d6d942427d025395197f76/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Tracing/EventSource.cs#L2187
Microsoft Reviewers: Open in CodeFlow
Fixes #6822