Skip to content

[Identity] Adjust refresh logic - #45496

Merged
Paul Van Eck (pvaneck) merged 7 commits into
Azure:mainfrom
pvaneck:identity-should-refresh-mixin
Mar 12, 2026
Merged

Paul Van Eck (pvaneck) merged 7 commits into
Azure:mainfrom
pvaneck:identity-should-refresh-mixin

Conversation

@pvaneck

@pvaneck Paul Van Eck (pvaneck) commented Mar 3, 2026 •

Copy link
Copy Markdown
Member
  • Add a TokenRefreshStatus enum class and a get_refresh_status method for more granular information on if a token should be refreshed.
  • Ensure expired tokens always trigger refresh, even during retry delay
  • Move _last_request_time assignment to only occur on failed proactive refresh. Before, it could cause cross-token throttling. Since _last_request_time is per-credential instance, a successful refresh for one scope/tenant would suppress proactive refresh of a different token on the same credential for up to 30 seconds, even though the second token was never attempted.

- Move _should_refresh from sync/async GetTokenMixin into a shared
  should_refresh() function in utils.py to eliminate duplication
- Ensure expired tokens always trigger refresh, even during retry delay

Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 refactors token refresh decision logic in azure-identity to reduce duplication across sync/async credentials, while fixing edge cases around proactive refresh throttling and ensuring expired tokens always force a refresh.

Changes:

  • Centralized refresh decision logic into a shared should_refresh() helper in _internal/utils.py.
  • Updated sync/async GetTokenMixin to use the shared helper and to only record _last_request_time on failed proactive refresh attempts (avoids cross-token throttling).
  • Added new unit tests validating refresh_on behavior, retry-delay interactions, and “expired token ignores retry delay”.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
sdk/identity/azure-identity/azure/identity/_internal/utils.py Adds shared should_refresh() helper implementing refresh/expiry/retry-delay logic.
sdk/identity/azure-identity/azure/identity/_internal/get_token_mixin.py Uses should_refresh() and updates _last_request_time semantics for proactive refresh failures.
sdk/identity/azure-identity/azure/identity/aio/_internal/get_token_mixin.py Uses should_refresh() and updates _last_request_time semantics for proactive refresh failures (async).
sdk/identity/azure-identity/azure/identity/_internal/init.py Re-exports should_refresh from _internal.
sdk/identity/azure-identity/tests/test_get_token_mixin.py Adds sync tests for refresh_on, retry delay, and expired-token behavior.
sdk/identity/azure-identity/tests/test_get_token_mixin_async.py Adds async equivalents of the new refresh behavior tests.

Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
Signed-off-by: Paul Van Eck <paulvaneck@microsoft.com>
@pvaneck

Copy link
Copy Markdown
Member Author

/check-enforcer override

@pvaneck
Paul Van Eck (pvaneck) merged commit 7972883 into Azure:main Mar 12, 2026
18 of 20 checks passed
@pvaneck
Paul Van Eck (pvaneck) deleted the identity-should-refresh-mixin branch March 12, 2026 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants