Repository navigation
fix(git): stop leaking temp files when the cache write fails - #564
Merged
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
tahodev
pushed a commit
to tahodev/ccstatusline
that referenced
this pull request
Oct 20, 2026
…oc#564) 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. Co-authored-by: Matthew Breedlove <sirmalloc@gmail.com>
sirmalloc
pushed a commit
that referenced
this pull request
Nov 5, 2026
… temp files (#783) * fix(terminal): stop leaking temp files when the width cache write fails writeCachedWidth wrote a temp file named after the process id and renamed it over terminal-width.json inside a catch that swallowed every error. When the rename failed (EPERM on Windows while a virus scanner or sync client holds a handle on the file), the temp file stayed behind. Every render is a new process with a new pid, so each failed write left one more terminal-width.json.<pid>.tmp in ~/.cache/ccstatusline, and nothing ever removed them. On Windows this write is routine: probing is disabled there, so the no-width entry is rewritten whenever it passes the TTL, and on every render while the rename keeps failing, since a failed write leaves the old entry stale. Handle it the way the git cache does since #564: remove the temp file when the write or rename fails, and use one stable name (terminal-width.json.tmp), so a cleanup blocked by the same handle leaves at most one file, which the next write truncates. Writers use it under the cache lock; one that proceeds unlocked can at worst leave malformed JSON, which readCache already treats as a miss. * fix(custom-command): stop leaking temp files when the cache write fails writePersistentCache wrote a temp file named after the process id and the current time, and renamed it over the cache file inside a catch that swallowed every error. When the rename failed (EPERM on Windows while a virus scanner or sync client holds a handle on the file), the temp file stayed behind. With customCommandCacheTtlSeconds above 0, nothing reaches the cache while the rename keeps failing, so every repaint misses, runs the command again and leaves one more cmd-<hash>.json.<pid>.<ms>.tmp in ~/.cache/ccstatusline/custom-command-cache. Nothing ever removed them. Handle it the way the git cache does since #564: remove the temp file when the write or rename fails, and use one stable name (cmd-<hash>.json.tmp), so a cleanup blocked by the same handle leaves at most one file per cache file, which the next write truncates. The cost is the one the git cache accepted: two sessions writing the same directory's cache at the same moment can tear the shared temp file, and readPersistentCache treats the malformed JSON as a miss.
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.