Repository navigation
fix: stop recursiveRemoveDir from deleting sibling cache files - #101
Merged
Merged
Conversation
The previous implementation counted only directories when walking the parent of an evicted entry, ignoring files. When two keys shared the same parent dir (e.g. "test0" and "test1" mapping to "te/st/0" and "te/st/1"), evicting one walked all the way up to cacheRoot and then RemoveAll'd the entire "te/" subtree, taking the surviving key's files with it. ttlcache.OnEviction runs in its own goroutine, so the race surfaced non-deterministically. Linux CI failed TestDiskCacheMaxKeys with "no such file or directory", while macOS happened to finish Load before the cleanup goroutine reached recursiveRemoveDir. Replace the dir-count heuristic with os.Remove walking upward. os.Remove only succeeds on empty directories, so it naturally stops as soon as any sibling file or directory remains, and the function never touches cacheRoot itself.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The previous loop swallowed every os.Remove error except IsNotExist, hiding permission/IO failures behind the same code path as the expected "directory not empty" stop condition. Check emptiness explicitly with os.ReadDir before calling os.Remove. Real errors from ReadDir or Remove now propagate to the caller, while IsNotExist (path stem doesn't exist, or raced with another remover) keeps the walk going as before.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
ReadDir + Remove has a TOCTOU window where another goroutine can store a sibling key under the same prefix between the emptiness check and the remove call. The OS then returns ENOTEMPTY, which the previous version would surface as a real cleanup failure. Match ENOTEMPTY explicitly with errors.Is and stop walking instead of propagating it. Permission and IO errors still propagate as before.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Two robustness fixes to recursiveRemoveDir: 1. NewDiskCache now stores filepath.Clean(cacheRoot). Paths derived from filepath.Join in Store are already cleaned, but cacheRoot was kept as user input. A trailing slash on the caller's value would make the "dir != cacheRoot" stop check miss, letting the walk reach and Remove the cache root itself once it became empty. 2. Extract isDirNotEmpty so the TOCTOU stop condition stays portable. POSIX returns syscall.ENOTEMPTY; Windows returns ERROR_DIR_NOT_EMPTY (145) instead, which syscall.ENOTEMPTY does not match.
This comment has been minimized.
This comment has been minimized.
POSIX (SUSv3) allows rmdir to return either ENOTEMPTY or EEXIST for a non-empty directory. Linux and macOS use ENOTEMPTY, but legacy POSIX-compatible filesystems may surface EEXIST. Match both in isDirNotEmpty so the TOCTOU stop path stays correct everywhere.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The existing TestRecursiveRemoveDir cases only exercise sibling directories. The regression that motivated this PR is the sibling *file* case, which until now was covered only indirectly by TestDiskCacheMaxKeys, whose async eviction goroutine made the failure race-dependent and flaky on CI. Add a deterministic test that lays out 0.request/0.response and 1.request/1.response under a shared parent, removes the "0" files, and asserts that recursiveRemoveDir leaves the "1" files and the parent directory intact.
This comment has been minimized.
This comment has been minimized.
Avoid duplicating the suffix literals from diskcache.go. The test lives in the same package, so it can reference the unexported constants directly and stay aligned if they ever change.
This comment has been minimized.
This comment has been minimized.
gosec flags 0644 on os.WriteFile. The test only needs to create readable files for its own process; drop to 0600.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
filepath.Clean("") returns ".", so an empty cacheRoot would silently
be treated as the current working directory and cache files would
land somewhere the caller never intended. Reject the empty input
explicitly so the caller gets a clear error instead.
Contributor
Code Metrics Report
Details | | main (6e90955) | #101 (1ad9980) | +/- |
|--------------------|----------------|----------------|-------|
- | Coverage | 77.4% | 74.1% | -3.4% |
| Files | 2 | 2 | 0 |
| Lines | 324 | 336 | +12 |
- | Covered | 251 | 249 | -2 |
+ | Code to Test Ratio | 1:1.9 | 1:1.9 | +0.0 |
| Code | 658 | 678 | +20 |
+ | Test | 1275 | 1315 | +40 |Code coverage of files in pull request scope (77.2% → 73.1%)
Reported by octocov |
Contributor
BenchmarkDiscCache1MBBody-4
Metadata
BenchmarkEncodeDecode1MBBody-4
Metadata
BenchmarkNGINXCache1MBBody-4
Metadata
Reported by octocov |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TestDiskCacheMaxKeyswas failing on CI (Linux) because evicting one cache entry could wipe out a sibling entry's files.The old
recursiveRemoveDircounted only directories in the parent, not files. With two keys sharing the same path stem (e.g.te/st/0andte/st/1), evictingtest0walked all the way up tocacheRootandRemoveAll'd the entirete/subtree, deletingtest1's files too. Becausettlcache.OnEvictionruns in its own goroutine, the race surfaced only on slower schedulers.This change replaces the heuristic with
os.Removewalking upward, which only succeeds on empty directories and stops naturally when a sibling file or directory remains.Failing run: https://github.com/2manymws/rcutil/actions/runs/28413839254