Skip to content

fix: replace panicking expect() with map_or() for edge-case file paths#2679

Closed
srpatcha wants to merge 3 commits intomozilla:mainfrom
srpatcha:fix/lru-cache-path-panic
Closed

fix: replace panicking expect() with map_or() for edge-case file paths#2679
srpatcha wants to merge 3 commits intomozilla:mainfrom
srpatcha:fix/lru-cache-path-panic

Conversation

@srpatcha
Copy link
Copy Markdown

Fix panic on edge-case file paths in LRU disk cache init

Problem

LruDiskCache::init() in src/lru_disk_cache/mod.rs calls .expect("Bad path?") on file.file_name(), which panics if the path ends in .. or is otherwise unusual. A build cache tool should handle unexpected file system states gracefully rather than crashing.

Root Cause

Path::file_name() returns None when the path terminates in .. or consists solely of a root or prefix. The .expect() call converts this into a panic. While uncommon, symlinks or filesystem corruption could produce such paths in the cache directory.

Fix

Replaced .expect("Bad path?").starts_with(TEMPFILE_PREFIX) with .map_or(false, |name| name.starts_with(TEMPFILE_PREFIX)). Paths without a valid file name component are now treated as non-temporary files (skipping the cleanup branch) rather than crashing.

Testing

  • Create a cache directory containing a path component ending in ...
  • Previously: sccache panics during cache init. Now: the entry is handled gracefully.
  • Normal cache operation should be unaffected.

Impact

Affects sccache users whose cache directories may contain unusual file paths. A panic in cache init prevents the entire build cache from working.

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Apr 18, 2026

Codecov Report

❌ Patch coverage is 2.77778% with 350 lines in your changes missing coverage. Please review.
✅ Project coverage is 25.77%. Comparing base (d11e2e0) to head (4b5da5d).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
src/stats.rs 0.00% 348 Missing ⚠️
src/lru_disk_cache/mod.rs 83.33% 1 Missing ⚠️
src/server.rs 0.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (d11e2e0) and HEAD (4b5da5d). Click for more details.

HEAD has 13 uploads less than BASE
Flag BASE (d11e2e0) HEAD (4b5da5d)
14 1
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #2679       +/-   ##
===========================================
- Coverage   74.17%   25.77%   -48.41%     
===========================================
  Files          70       60       -10     
  Lines       39207    37206     -2001     
===========================================
- Hits        29083     9588    -19495     
- Misses      10124    27618    +17494     

☔ 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.

Path::file_name() returns None for paths ending in '..' which caused
a panic. Build cache tools should handle unusual paths gracefully.
@srpatcha srpatcha force-pushed the fix/lru-cache-path-panic branch from 9ed2e8a to fbd8a8a Compare April 25, 2026 02:17
srpatcha and others added 2 commits April 24, 2026 22:07
prepare_add updated pending size before tempfile_in(), which never
rolled back on failure. This caused make_space() to panic later when
encountering negative available space. Moved state update after
successful tempfile creation and replaced .expect() with ? operator.

Signed-off-by: Srikanth Patchava <spatchava@meta.com>
Signed-off-by: Srikanth Patchava <srikanth.patchava@outlook.com>
Add comprehensive cache statistics tracking with:
- Thread-safe atomic counters for hit/miss/eviction tracking
- Cache size and entry count monitoring
- JSON and human-readable output formats
- Time-window aggregation (minute, hour, day)
- Statistics snapshot and reset API
- Fix TOCTOU race condition in DiskCache::put_raw

Includes extensive test suite with thread safety tests.

Signed-off-by: Srikanth Patchava <spatchava@meta.com>
@sylvestre
Copy link
Copy Markdown
Collaborator

you pr is doing too many different things (like fix the panic and change files permissions)
please split it into several prs

@srpatcha
Copy link
Copy Markdown
Author

Hi @sylvestre, thank you for the feedback! You're right — I'll split this into separate PRs:

  1. Fix the panicking expect() with map_or()
  2. File permission changes (separate concern)

I'll close this PR and submit them individually. Thanks for the review!

@srpatcha
Copy link
Copy Markdown
Author

Closing this PR as requested by @sylvestre. Split into focused PRs:

  1. fix: replace panicking expect() with map_or() for file_name() check #2688 - fix: replace panicking expect() with map_or() for file_name() check
  2. fix: rollback pending state on tempfile failure in prepare_add #2689 - fix: rollback pending state on tempfile failure in prepare_add

The cache statistics module will be submitted as a separate PR. Thank you for the feedback!

@srpatcha srpatcha closed this Apr 28, 2026
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