Skip to content

Conversation

ssmelov
Copy link
Contributor

@ssmelov ssmelov commented Sep 19, 2025

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

@Copilot Copilot AI review requested due to automatic review settings September 19, 2025 11:42
@ssmelov ssmelov requested a review from a team as a code owner September 19, 2025 11:42
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

@dariusclay dariusclay enabled auto-merge (squash) September 23, 2025 08:36
@ssmelov
Copy link
Contributor Author

ssmelov commented Sep 23, 2025

@dotnet-policy-service agree company="Microsoft"

This was referenced Oct 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HttpRequestLatencyListener.OnEventWritten throws KeyNotFoundException for unknown event sources

3 participants