Skip to content

Invalidate cached Microsoft Graph request adapter on 401 Unauthorized - #72688

Merged
dabla merged 6 commits into
apache:mainfrom
dabla:fix/msgraph-token-expired
Sep 8, 2026
Merged

dabla merged 6 commits into
apache:mainfrom
dabla:fix/msgraph-token-expired

Conversation

@dabla

@dabla dabla commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Invalidate the cached Microsoft Graph RequestAdapter when a request fails with 401 Unauthorized.

KiotaRequestAdapterHook caches a RequestAdapter per conn_id and only rebuilds it when the underlying HTTP client or credential session is detected as closed. If the Graph API starts returning 401 (e.g. an expired/revoked token, tenant reconfiguration, or a token the credential wrongly believes is still valid), the same broken adapter kept being reused and every subsequent request failed due to an ExpiredToken error until the process was restarted.

DefaultResponseHandler.handle_response_async now raises PermissionError for HTTP 401 responses instead of AirflowBadRequest. KiotaRequestAdapterHook.send_request already caught RuntimeError and ValueError to evict the cached adapter on failure; it now also catches PermissionError, so a 401 evicts the stale adapter and the next request rebuilds it (and re-authenticates) from scratch.

PermissionError was chosen over a new dedicated AirflowUnauthorized exception class to keep the change minimal and avoid introducing a new public exception type for this provider.

Added unit tests covering:

  • DefaultResponseHandler.handle_response_async raises PermissionError for a 401
    response.
  • KiotaRequestAdapterHook.send_request evicts the cached request adapter and re-raises
    when the underlying call raises PermissionError (in addition to the existing
    RuntimeError coverage).

Was generative AI tooling used to co-author this PR?
  • Yes (please specify the tool below)
    Claude Sonnet 5

  • Read the Pull Request Guidelines for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
  • For fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
  • When adding dependency, check compliance with the ASF 3rd Party License Policy.
  • For significant user-facing changes create newsfragment: {pr_number}.significant.rst, in airflow-core/newsfragments. You can add this file in a follow-up commit after the PR is created so you know the PR number.

@dabla dabla self-assigned this Sep 8, 2026
@dabla
dabla merged commit 12bc014 into apache:main Sep 8, 2026
83 checks passed
regarmukesh3g pushed a commit to regarmukesh3g/airflow that referenced this pull request Sep 27, 2026
…apache#72688)

* Invalidate cached Microsoft Graph request adapter on 401 Unauthorized

* Close cached MSGraph request adapter and credential on request failure
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.

2 participants