-
Notifications
You must be signed in to change notification settings - Fork 393
Fix KeyNotFoundException during retry when headers lack correlation ID #5617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
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
Copilot
AI
changed the title
[WIP] Fix KeyNotFoundException during retry for Managed Identity token
Fix KeyNotFoundException during retry when headers lack correlation ID
Dec 10, 2025
bgavrilMS
approved these changes
Dec 11, 2025
gladjohn
reviewed
Dec 11, 2025
gladjohn
approved these changes
Dec 11, 2025
2fe9b8a to
92c33ec
Compare
Member
|
@copilot - don't perform 2 dictionary lookups. use TryGetValue instead |
Contributor
Author
bgavrilMS
approved these changes
Dec 15, 2025
4gust
approved these changes
Dec 16, 2025
Co-authored-by: bgavrilMS <[email protected]>
Co-authored-by: bgavrilMS <[email protected]>
…tion objects Co-authored-by: bgavrilMS <[email protected]>
6677719 to
08a82e1
Compare
This was referenced Jan 8, 2026
This was referenced Jan 9, 2026
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.
Changes proposed in this request
The retry logic in
HttpManager.SendRequestAsyncaccessedheaders[OAuth2Header.CorrelationId]before checking key existence, throwingKeyNotFoundExceptionwhen headers were present but lacked the correlation ID (e.g., during Managed Identity token requests).Core changes:
HttpManager.cs: UseTryGetValuein the condition check to verify existence and retrieve value in a single dictionary lookup, creating only one exception object per code pathHttpManagerTests.cs: Add regression testTestRetryOnTimeoutWithHeadersButNoCorrelationIdAsyncvalidating timeout with headers but no correlation IDTesting
Added regression test that simulates retry timeout with headers that don't contain correlation ID. All existing HttpManagerTests (20 tests) pass.
Performance impact
Optimized to use a single
TryGetValuecall in the condition, eliminating redundant dictionary lookups and avoiding creation of unnecessary exception objects in the timeout path.Documentation
Original prompt
This section details on the original issue you should resolve
<issue_title>[Bug] KeyNotFoundException during retry for getting Managed Identity token</issue_title>
<issue_description>### Library version used
4.78.0
.NET version
.NET 9.0 SDK
.NET 8.0 Target Framework
Scenario
ManagedIdentityClient - managed identity
Is this a new or an existing app?
The app is in production, I haven't upgraded MSAL, but started seeing this issue
Issue description and reproduction steps
We are seeing in our logging: KeyNotFoundExceptions with the retry mechanisme for aquiring token from the Managed Identity endpoint.
The error messages/stack traces:
Relevant code snippets
Could this line of code the cause of KeyNotFoundException and fail the retry mechanisme?
https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/blob/964a07449f9d5183828fbd0e1e9d4d3e9b6d80c7/src/client/Microsoft.Identity.Client/Http/HttpManager.cs#L143C20-L143C77
Expected behavior
No response
Identity provider
Other
Regression
No response
Solution and workarounds
-</issue_description>
<agent_instructions>The problem is in HttpManager, when trying to read headers[OAuth2Header.CorrelationId]
Make sure to add a regression test for this.</agent_instructions>
Comments on the Issue (you are @copilot in this section)
@bgavrilMS Yes, thanks for raising. Looks like a recent change https://github.com//pull/5364 where we were trying to include correlation id.We will get it fixed, but please note that error would still occur.</comment_new>
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.