Skip to content

Handle empty cache files#5192

Merged
cobaltt7 merged 5 commits into
psf:mainfrom
CodingFeng101:codex/ignore-empty-cache-file
Jul 3, 2026
Merged

Handle empty cache files#5192
cobaltt7 merged 5 commits into
psf:mainfrom
CodingFeng101:codex/ignore-empty-cache-file

Conversation

@CodingFeng101

Copy link
Copy Markdown
Contributor

Summary

Treat empty or truncated cache files the same way as other malformed cache files.

Details

Cache.read() already ignores malformed pickle data so a later cache write can repair the file, but an empty cache file raises EOFError, which was not caught. This can happen if cache creation is interrupted or an external process truncates the file.

This change catches EOFError while reading the cache and adds a regression test for an empty cache file.

Test

  • python -m pytest tests/test_black.py::TestCaching::test_cache_empty_file tests/test_black.py::TestCaching::test_cache_broken_file -q

@cobaltt7

Copy link
Copy Markdown
Collaborator

Thanks! Please add a changelog entry.

@github-actions

github-actions Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

diff-shades results comparing this PR (30e2f72) to main (ce441f2):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

@CodingFeng101

Copy link
Copy Markdown
Contributor Author

Added the requested changelog entry in fcf00f6.

@cobaltt7

Copy link
Copy Markdown
Collaborator

Could you use one of the pre-existing categories instead of adding a new one? (Caching changes have gone in Configuration in the past)

@CodingFeng101

Copy link
Copy Markdown
Contributor Author

Moved the entry under the existing Configuration category in 66e62ac.

@DTiming24 DTiming24 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catching EOFError seems like the right symmetry with malformed pickle handling. One subtle edge case: if an external process truncates the file while Black is running, silently treating it as a cache miss is probably preferable to surfacing cache corruption to the user. The regression test covers the repair path well.

@DTiming24

Copy link
Copy Markdown

Nice fix. Since empty cache files now fall back the same way as other malformed cache data, a regression test for a zero-byte file created by an interrupted write would help lock in the new behavior.

@CodingFeng101

Copy link
Copy Markdown
Contributor Author

Thanks for taking a look. The current PR already has a zero-byte cache regression test: test_cache_empty_file creates the cache path with cache_file.touch() and asserts Cache.read(mode).file_data == {}. That is the interrupted/truncated-write case this change is meant to keep on the same recovery path as malformed pickle data.

@cobaltt7 cobaltt7 merged commit 56ba38a into psf:main Jul 3, 2026
57 checks passed
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