Skip to content

{Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries#33728

Draft
a0x1ab with Copilot wants to merge 3 commits into
devfrom
copilot/fix-az-login-error
Draft

{Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries#33728
a0x1ab with Copilot wants to merge 3 commits into
devfrom
copilot/fix-az-login-error

Conversation

Copilot AI commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Related command
az login

Description

az login crashes with json.decoder.JSONDecodeError: Extra data when ~/.azure/msal_token_cache.json is corrupted (e.g., partial/concurrent writes). The msal_extensions.PersistedTokenCache._reload_if_necessary() only catches PersistenceNotFound, letting JSONDecodeError propagate unhandled.

Changes:

  • AzureCliTokenCache (persistence.py): New subclass of PersistedTokenCache that overrides _reload_if_necessary to catch json.JSONDecodeError, emit a warning, and reset the in-memory cache to empty via self.deserialize(None). The subsequent modify() call then writes a fresh valid cache to disk, overwriting the corrupt file.
  • load_persisted_token_cache: Updated to return AzureCliTokenCache instead of bare PersistedTokenCache.
  • SecretStore.load: Also catches json.JSONDecodeError for the service principal entries file, returning [] rather than raising.

Testing Guide

Manually corrupt ~/.azure/msal_token_cache.json by appending extra bytes, then run az login:

echo "corrupted" >> ~/.azure/msal_token_cache.json
az login

Before this fix: crashes with JSONDecodeError: Extra data.
After this fix: logs a warning and proceeds with a fresh cache.

Unit tests added in src/azure-cli-core/azure/cli/core/auth/tests/test_persistence.py covering corrupted and valid JSON for both AzureCliTokenCache and SecretStore.

History Notes

[Core] az login: Fix JSONDecodeError crash when MSAL token cache file is corrupted


This checklist is used to make sure that common guidelines for a pull request are followed.

@azure-client-tools-bot-prd

Copy link
Copy Markdown
Validation for Azure CLI Full Test Starting...

Thanks for your contribution!

@azure-client-tools-bot-prd

Copy link
Copy Markdown
Validation for Breaking Change Starting...

Thanks for your contribution!

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI changed the title [WIP] Fix az login error after authentication in browser {Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries Jul 15, 2026
Copilot AI requested a review from a0x1ab July 15, 2026 01:26
@azure-client-tools-agent

Copy link
Copy Markdown

Live test skipped

⏭️ Skipping the live test for this revision because the only test file(s) changed are azure-cli-core unit tests, which the live-test pipeline (azdev test --live) does not run — it covers command-module and extension tests only.

These azure-cli-core tests are exercised by upstream CI's unit-test jobs instead. This is informational; no action is required.


Posted by agent-assist (autonomous bug-fix pipeline).

@azure-client-tools-agent azure-client-tools-agent Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated Review Summary

PR: {Core} Handle JSONDecodeError for corrupted MSAL token cache and SP entries

  • Live test: Skipped (neutral) — this PR only changes azure-cli-core unit tests, which are not runnable via azdev test --live. No other test files were changed.
  • CI checks: All 1/1 checks passed, none pending, none failed.

No blocking issues found. This PR appears ready based on the CI status observed at this head commit (027cbcc).


Posted by agent-assist (autonomous bug-fix pipeline).

@yonzhan

yonzhan commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

Core

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Account az login/account act-identity-squad Auto-Assign Auto assign by bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

az login error after authentication in browser - json.decoder.JSONDecodeError: Extra data: line 3 column 8 (char 31)

3 participants