fix(git): stop leaking temp files when the cache write fails - #564
Merged
Conversation
writePersistentCache wrote a uniquely named .tmp file and renamed it over the cache path inside a swallow-all catch. When the write or rename fails (notably EPERM on Windows while a virus scanner or sync client holds a handle), the temp file was never unlinked, leaking one file per cache write - observed at ~400k orphaned files in ~/.cache/ccstatusline/git-cache. Clean up the temp file on failure, mirroring config.ts:writeSettingsJson minus the rethrow since this cache is best-effort. Because the same held handle can make the cleanup unlink fail too, the temp name is now stable (cachePath + '.tmp') instead of unique: the worst case becomes a single orphan per repo that the next write truncates. A torn concurrent write only produces malformed JSON, which readPersistentCache already treats as a cache miss. Deliberately no sweep for pre-existing orphans: a readdir over a directory that large on the render path costs more than it saves. Deleting the git-cache directory once by hand is enough - it regenerates itself.
niklasgs
force-pushed
the
fix/git-cache-tmp-leak
branch
from
August 28, 2026 06:56
18dbbab to
de0c9c6
Compare
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.
Fixes #563
writePersistentCachenever deleted its temp file when the rename over the cache path failed, leaking one uniquely named.tmpper cache write (~400k observed on Windows, where EPERM from a scanner/sync client holding a handle is common).Fix:
config.ts:writeSettingsJson(minus the rethrow — this cache is best-effort).<cachePath>.tmp) instead of pid+timestamp: the same held handle can defeat the unlink too, so reuse bounds the worst case to one orphan per repo that the next write truncates. A torn concurrent write reads as malformed JSON, which is already treated as a cache miss..tmpis left behind.Deliberately no sweep of pre-existing orphans — a readdir over a 400k-entry directory on the render path costs more than it saves. Affected users: delete
~/.cache/ccstatusline/git-cache/once; it regenerates.bun run lintclean, 1869 tests pass.