Repository navigation
Description
Problem
When multiple requests trigger an S3 fileserver cache refresh simultaneously, _write_buckets_cache_file can fail with FileNotFoundError because it attempts to remove the cache file without handling the case where the file has already been removed by another concurrent call.
The unhandled exception propagates up through the async handler, pollutes the event bus with stringified tracebacks, breaks the event iterator ('str' object has no attribute 'items'), and ultimately causes the master to stop responding to requests.
Steps to reproduce
Configure S3FS as a fileserver backend
Set a low s3_cache_expire (default is 30 seconds) or enable s3_sync_on_update: True (default)
Send multiple concurrent requests that require S3 fileserver lookups
Observe FileNotFoundError in the master log when two cache refresh operations overlap
Relevant code
salt/fileserver/s3fs.py, function _write_buckets_cache_file:
def _write_buckets_cache_file(metadata, cache_file):
"""
Write the contents of the buckets cache file
"""
cache_dir = _get_cache_dir()
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
if os.path.isfile(cache_file):
os.remove(cache_file) # <-- FileNotFoundError here
log.debug("Writing buckets cache file")
with salt.utils.files.fopen(cache_file, "wb") as fp_:
pickle.dump(metadata, fp_)
Between the os.path.isfile check and the os.remove call, another concurrent invocation of the same function may have already removed the file, causing the second os.remove to raise FileNotFoundError.
Proposed fix
Wrap os.remove in a try/except to gracefully handle the case where the file no longer exists:
def _write_buckets_cache_file(metadata, cache_file):
"""
Write the contents of the buckets cache file
"""
cache_dir = _get_cache_dir()
if not os.path.exists(cache_dir):
os.makedirs(cache_dir)
try:
os.remove(cache_file)
except FileNotFoundError:
pass
log.debug("Writing buckets cache file")
with salt.utils.files.fopen(cache_file, "wb") as fp_:
pickle.dump(metadata, fp_)
Workaround
Until the fix is merged, the issue can be mitigated by reducing the frequency of cache refreshes:
s3:
s3_cache_expire: 300 # 5 minutes instead of 30 seconds
s3_sync_on_update: False # disable forced full sync on fileserver update
Versions affected
Confirmed on: Salt 3006.7.2
Likely affects all versions with the current _write_buckets_cache_file implementation
Additional context
The default s3_cache_expire value of 30 seconds makes this issue particularly impactful — cache refreshes happen frequently enough that overlapping calls are almost guaranteed under normal load. Combined with s3_sync_on_update: True (default), the cache file is rewritten every 30–60 seconds, making the race condition systematic rather than intermittent.
Related symptoms in the master log:
FileNotFoundError: [Errno 2] No such file or directory:
/app/salt-stack/root/var/cache/salt/master/s3cache/buckets_files.cache
Event iteration failed with exception: 'str' object has no attribute 'items'
Salt request timed out. The master is not responding.
You may need to run your command with '--async' in order to bypass the congested event bus.
Activity
Fix race in s3fs _write_buckets_cache_file cache removal (#69670)