Skip to content

JSONFileCache.__setitem__ not multi-process safe. #9632

@Ramyak

Description

@Ramyak

Describe the bug

If multiple aws commands run at the same time and sets the .aws/cli/cache/*.json, the cache file could get corrupted (not a valid json)

We noticed corrupted .aws/cli/cache/*.json file.

  1. cache key is always the same - sha1 of args (which is just role arn and serial number) (makes sense) [code]
  2. JSONFileCache.__setitem__ (is not multi-process/thread safe) just does os.fdopen [code]. If multiple processes open the file and write to this file at the same time, it could get corrupted (not valid json)
2025-07-31 11:21:08,705 - MainThread - botocore.credentials - DEBUG - hii in _create_cache_key args={"RoleArn": "arn:aws:iam::<accountid>:role/role", "SerialNumber": "arn:aws:iam::<accountid>:mfa/<mfa>"}
2025-07-31 11:21:08,705 - MainThread - botocore.credentials - DEBUG - hii argument_hash=<same_key_always>
2025-07-31 11:21:14,022 - MainThread - botocore.credentials - DEBUG - hii type(self._cache)=<class 'botocore.utils.JSONFileCache'>
2025-07-31 11:21:14,022 - MainThread - botocore.utils - DEBUG - hiii __setitem__ = /Users/<user>/.aws/cli/cache/<same_key_always>.json



2025-07-31 11:20:17,696 - MainThread - botocore.credentials - DEBUG - hii in _create_cache_key args={"RoleArn": "arn:aws:iam::<accountid>:role/role", "SerialNumber": "arn:aws:iam::<accountid>:mfa/<mfa>"}
2025-07-31 11:20:17,696 - MainThread - botocore.credentials - DEBUG - hii argument_hash=<<same_key_always>>
2025-07-31 11:20:22,562 - MainThread - botocore.utils - DEBUG - hiii __setitem__ = /Users/<user>/.aws/cli/cache/<same_key_always>.json

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

/Users/<user>/.aws/cli/cache/<same_key_always>.json to always have valid json

Current Behavior

/Users/<user>/.aws/cli/cache/<same_key_always>.json under some race condition can store invalid json

Reproduction Steps

I dont have clear reproduction steps. Run many aws cli without the cache file.

Possible Solution

Make JSONFileCache.__setitem__ function multi process safe.

Additional Information/Context

No response

CLI version used

aws-cli/2.26.1

Environment details (OS name and version, etc.)

Mac

Metadata

Metadata

Assignees

Labels

bugThis issue is a bug.p3This is a minor priority issue

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions