Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ubaskota
Copy link
Contributor

@ubaskota ubaskota commented Nov 4, 2025

Description of changes:
Use os.replace() for atomic file operations to eliminate race conditions during concurrent access to the file cache. This ensures that writes are either fully completed or not applied at all, preventing partial writes that could leave the JSON file in an invalid state when multiple processes access the cache simultaneously.

Note: This change is based on the approach suggested in #3544 by @ranlz77. Thanks for identifying this issue and proposing the initial solution.

Tests:
I verified that all tests including the the newly added unit tests pass.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@codecov-commenter
Copy link

codecov-commenter commented Nov 4, 2025

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 50.00000% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.10%. Comparing base (8121342) to head (2b225c5).
⚠️ Report is 57 commits behind head on develop.

Files with missing lines Patch % Lines
botocore/utils.py 50.00% 12 Missing ⚠️
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #3586      +/-   ##
===========================================
- Coverage    93.17%   93.10%   -0.07%     
===========================================
  Files           68       68              
  Lines        15411    15432      +21     
===========================================
+ Hits         14359    14368       +9     
- Misses        1052     1064      +12     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines +3585 to +3586
if hasattr(os, 'fchmod'):
os.fchmod(temp_fd, 0o600)
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be necessary – mkstemp's documentation says "The file is readable and writable only by the creating user ID.".

if hasattr(os, 'fchmod'):
os.fchmod(temp_fd, 0o600)
with os.fdopen(temp_fd, 'w') as f:
temp_fd = None
Copy link
Contributor

Choose a reason for hiding this comment

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

os.fdopen is an alias of open. Exiting the context manager (for example when unwinding from an exception) should close the underlying fd, so I'm not sure the temp_fd = None + cleanup dance in except Exception below is necessary?

Comment on lines +3590 to +3591
f.flush()
os.fsync(f.fileno())
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a particular reason to add an explicit flush + fsync?

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.

3 participants