Resilience improvements for instance discovery#5811
Conversation
There was a problem hiding this comment.
Pull request overview
Improves AAD instance discovery resilience and performance by avoiding repeated network instance discovery attempts when the endpoint is failing/unreachable, and by bounding discovery latency with a dedicated timeout.
Changes:
- Cache a fallback instance discovery entry when network instance discovery fails (non-
invalid_instance) to avoid retrying discovery on subsequent token requests. - Add a per-instance-discovery timeout (default 10s) by linking a timeout CancellationToken into the discovery request flow.
- Add unit tests covering caching-on-failure and timeout fallback behavior; add a rules doc for cross-SDK reference.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Microsoft.Identity.Test.Unit/PublicApiTests/InstanceDiscoveryTests.cs | Adds tests ensuring instance discovery failures/timeouts are cached and not retried. |
| src/client/Microsoft.Identity.Client/Internal/RequestContext.cs | Makes UserCancellationToken settable to support temporary override during instance discovery. |
| src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs | Adds instance discovery timeout and links it into the outgoing request. |
| src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs | Caches fallback metadata on discovery failure; updates warning text. |
| docs/instance-discovery-rules.md | Adds a detailed description of instance discovery behavior and error-handling rules. |
You can also share your feedback on Copilot code review. Take the survey.
src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs
Show resolved
Hide resolved
b15f64d to
64e28a7
Compare
src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkMetadataProvider.cs
Outdated
Show resolved
Hide resolved
tests/Microsoft.Identity.Test.Unit/PublicApiTests/InstanceDiscoveryTests.cs
Outdated
Show resolved
Hide resolved
907fb84 to
9066be3
Compare
9066be3 to
a7a2d5b
Compare
src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs
Outdated
Show resolved
Hide resolved
src/client/Microsoft.Identity.Client/Instance/Discovery/InstanceDiscoveryManager.cs
Outdated
Show resolved
Hide resolved
af8f6cd to
296899f
Compare
src/client/Microsoft.Identity.Client/Instance/Discovery/NetworkCacheMetadataProvider.cs
Show resolved
Hide resolved
| } | ||
| } | ||
|
|
||
| [TestMethod] |
There was a problem hiding this comment.
Can we add one regression test for caller-driven cancellation here? The new timeout path temporarily swaps RequestContext.UserCancellationToken to a linked CTS and relies on the outer catch filter to distinguish timeout from user cancellation. The current timeout test throws TaskCanceledException directly from the mock handler, so it doesn’t exercise the linked-token path or verify that a caller cancellation still bubbles instead of falling back.
|
looks good, one follow-up test needed. |
Fixes #5804 #5805
If instance discovery fails due to 404 or 502, it should not be attempted again
Instance discovery should have a reasonble timeout