Skip to content

[analyzer] Fix cache size counter drift in EvictingFileByteStore._cleanUpFolder#63127

Open
vedant21-oss wants to merge 1 commit intodart-lang:mainfrom
vedant21-oss:fix-analyzer-file-cache-size-tracking
Open

[analyzer] Fix cache size counter drift in EvictingFileByteStore._cleanUpFolder#63127
vedant21-oss wants to merge 1 commit intodart-lang:mainfrom
vedant21-oss:fix-analyzer-file-cache-size-tracking

Conversation

@vedant21-oss
Copy link
Copy Markdown

Problem

EvictingFileByteStore._cleanUpFolder was decrementing currentSizeBytes unconditionally,
even when file.deleteSync() threw an exception:

try {
  file.deleteSync();
} catch (_) {}
currentSizeBytes -= fileStatMap[file]!.size; // <- always runs, even on failure

If a file cannot be deleted (e.g. due to a permissions error or a race with another process),
the counter is decremented as though the deletion succeeded. This causes the cache to
underestimate its own size, which can prevent the eviction loop from running again even
though the cache is still over its maxSizeBytes limit.

Fix

Move the decrement inside the try block so it only executes when the deletion actually succeeds:

try {
  file.deleteSync();
  currentSizeBytes -= fileStatMap[file]!.size; // <- only runs on success
} catch (_) {}

Tests

Two regression tests are added to a new EvictingFileByteStoreCleanUpTest class:

  • test_cleanUpFolder_successfulDeletion_decrementsSizeCounter - basic happy-path eviction.
    • test_cleanUpFolder_failedDeletion_doesNotDecrementSizeCounter - simulates a failed
  • deletion by revoking chmod 000 on the parent directory and verifies that the files
  • remain on disk (i.e. the loop is not fooled by a corrupted size counter). Skipped on Windows.
    Fixes Analyzer cache size accounting drift in EvictingFileByteStore #63118

The _cleanUpFolder method was unconditionally decrementing currentSizeBytes
even when file.deleteSync() threw an exception. This allowed the cache
counter to underestimate the actual disk size, potentially allowing the
cache to grow beyond its configured maxSizeBytes limit without triggering
further eviction.

The fix wraps the decrement inside the try block so it only executes
when deletion succeeds.

Test: Two regression tests are added to EvictingFileByteStoreCleanUpTest:
  - Verifies successful deletion correctly evicts files.
  - Simulates a deletion failure via POSIX permissions and confirms
    the counter is NOT decremented when deletion fails.

Fixes: dart-lang#63118
@google-cla
Copy link
Copy Markdown

google-cla bot commented Apr 7, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@vedant21-oss
Copy link
Copy Markdown
Author

@google-cla check

@vedant21-oss
Copy link
Copy Markdown
Author

Submitted a fix in #63127 — moves the currentSizeBytes decrement inside the try block so it's only decremented when deletion actually succeeds.

@copybara-service
Copy link
Copy Markdown

Thank you for your contribution! This project uses Gerrit for code reviews. Your pull request has automatically been converted into a code review at:

https://dart-review.googlesource.com/c/sdk/+/494021

Please wait for a developer to review your code review at the above link; you can speed up the review if you sign into Gerrit and manually add a reviewer that has recently worked on the relevant code. See CONTRIBUTING.md to learn how to upload changes to Gerrit directly.

Additional commits pushed to this PR will update both the PR and the corresponding Gerrit CL. After the review is complete on the CL, your reviewer will merge the CL (automatically closing this PR).

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.

Analyzer cache size accounting drift in EvictingFileByteStore

1 participant