Fix KeyNotFoundException during retry when headers lack correlation ID #5617
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: CheckContainsKeybefore dictionary accessHttpManagerTests.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
None. Adds one dictionary lookup check per timeout exception 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.