Skip to content

fix(utils): handle malformed JSON in JSONFileCache gracefully#3682

Open
armorbreak001 wants to merge 1 commit intoboto:developfrom
armorbreak001:fix/jsoncache-malformed-json
Open

fix(utils): handle malformed JSON in JSONFileCache gracefully#3682
armorbreak001 wants to merge 1 commit intoboto:developfrom
armorbreak001:fix/jsoncache-malformed-json

Conversation

@armorbreak001
Copy link
Copy Markdown

When a cache file contains corrupted JSON (e.g., from a partial write or concurrent modification), JSONFileCache.__getitem__ previously raised a bare KeyError showing only the cache key hash, giving users no indication of the root cause.

This change splits the error handling in __getitem__:

  • OSError (file not found): raises KeyError immediately — normal cache miss
  • ValueError/JSONDecodeError (malformed JSON): logs a warning with the file path and parse error, removes the corrupted file to prevent repeated failures on every call, then raises KeyError so callers treat it as a cache miss and re-fetch credentials

The corrupted file cleanup is important because without it, the same cryptic error repeats on every subsequent call until the user manually clears the cache directory.

Fixes #3106

When a cache file contains corrupted JSON (e.g. from a partial write
or concurrent modification), JSONFileCache.__getitem__ now logs a
warning with the parse error, removes the corrupted file to prevent
repeated failures, and raises KeyError to treat it as a cache miss.

Previously, a corrupted cache file would cause a cryptic KeyError
showing only the cache key hash, with no indication of the root cause.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Credentials JSONFileCache explodes if cache file has malformed JSON

1 participant