Skip to content

Fix race in s3fs _write_buckets_cache_file cache removal (#69529) - #69670

Merged
dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-69529
Jul 9, 2026
Merged

dwoz merged 1 commit into
saltstack:3006.xfrom
dwoz:fix/issue-69529

Conversation

@dwoz

@dwoz dwoz commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a race in salt.fileserver.s3fs._write_buckets_cache_file where two
concurrent cache refreshes could raise an unhandled FileNotFoundError
when the second call reached os.remove after the first call had already
removed the stale cache file. The removal is now tolerant of the file
being missing.

What issues does this PR fix or reference?

Fixes #69529

Previous Behavior

Under the default s3_cache_expire: 30 and s3_sync_on_update: True,
overlapping S3 fileserver cache refreshes hit the TOCTOU between the
os.path.isfile(cache_file) guard and the os.remove(cache_file) call.
The unhandled FileNotFoundError propagated up through the async handler,
polluting the event bus with a stringified traceback ("Event iteration failed with exception: 'str' object has no attribute 'items'") and
leaving the master unresponsive until it was restarted.

New Behavior

_write_buckets_cache_file unconditionally attempts to remove the stale
cache file inside a try/except FileNotFoundError: pass. Concurrent
refreshes are now idempotent — the second call quietly no-ops on the
already-removed file and rewrites the cache atomically.

A regression test in tests/pytests/unit/fileserver/test_s3fs.py
(test_write_buckets_cache_file_race_condition) patches os.remove to
simulate the race and asserts that the write completes and the new
metadata round-trips.

Merge requirements satisfied?

  • Docs (no user-facing doc change; internal behavior only)
  • Changelog (changelog/69529.fixed.md)
  • Tests written/updated

Commits signed with GPG?

Yes

Notes

salt/fileserver/s3fs.py was purged from 3007.x onwards (commit
dc526dc2b17 "Initial purge of community extensions") in favor of
saltstack/saltext-s3fs. This PR therefore targets 3006.x only — the
oldest and only supported branch that still ships the affected file. A
follow-up PR against the extension repository will be needed to carry the
same fix forward if saltext-s3fs still has this pattern.

Under overlapping S3 fileserver cache refreshes,
_write_buckets_cache_file could race between the os.path.isfile guard and
the os.remove call: a concurrent invocation may have already deleted the
stale cache file, so the second call raised an unhandled
FileNotFoundError. The exception propagated through the async handler,
polluted the event bus with a stringified traceback, and left the master
unresponsive.

Replace the TOCTOU pattern with an unconditional os.remove wrapped in
try/except FileNotFoundError, so concurrent refreshes now no-op on the
already-removed file instead of aborting the write.

Fixes saltstack#69529
@dwoz
dwoz merged commit 12e3258 into saltstack:3006.x Jul 9, 2026
439 of 444 checks passed
@dwoz
dwoz deleted the fix/issue-69529 branch November 13, 2026 08:30

This branch was previously deployed

1 inactive deployment
ci — c92f5bb4 Deployed Jul 4, 2026 by dwoz via Build Onedir Packages / macOS (arm64) #25186
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants