Repository navigation
Description
Summary
DiskCache accounts a file only after the temporary file has been fully copied, synced, closed, and renamed. Concurrent and unknown-length writes can therefore consume substantially more physical disk than the configured cache capacity.
Root cause
DiskCache.writeFile creates a temp file and streams the entire reader into it (pkg/fileservice/disk_cache.go:536-587). FIFO accounting is updated only after rename (disk_cache.go:592-601).
The pre-write eviction estimate is non-zero only when the reader is an *os.File with a known size (disk_cache.go:559-568). S3 streaming readers and other unknown-length readers can write with no reservation. Multiple writers can all observe the same indexed usage because temp and in-progress bytes are absent from d.cache.Used().
ENOSPC handling is reactive: it attempts eviction after the write has already failed. It cannot keep concurrent temp files within the configured capacity or protect a shared filesystem from transient exhaustion.
Impact
- concurrent full-object/range cache fills can exceed DiskCache capacity;
- temp files can exhaust the CN volume and affect logs, spill, checkpoints, or other colocated services;
- accounting and operational metrics under-report physical cache consumption;
- unknown-length streams have no admission bound.
Proposed design
Introduce cancellable byte reservations for temp and in-progress writes:
- reserve known sizes before creating/writing the temp file;
- for unknown sizes, grow a reservation in bounded chunks and stop when the next chunk cannot be reserved;
- convert the reservation to resident accounting only after successful rename/index insertion;
- release it on every open/copy/sync/close/rename/cancel failure;
- track resident, reserved/in-flight, temp, and orphan bytes separately;
- define how reservation interacts with eviction and shared-volume headroom.
This issue intentionally does not propose a partial PR until the reservation and rollback contract is agreed.
Acceptance criteria
- physical
resident + temp + in-flightcache bytes stay within the configured bound plus explicitly documented filesystem headroom; - reservations are cancellable and cannot leak on any error path;
- concurrent known- and unknown-length writer tests cannot overcommit quota;
- ENOSPC/fault-injection tests leave no unindexed temp files.
Activity
LeftHandCold commentedon Jul 15, 2026
Why this issue does not have a small fix PR yet
The problem is not an isolated missing
Evictcall. The current DiskCache accounting model only knows about resident, indexed files, while the failure mode is caused by temporary and in-progress physical bytes that exist before a file becomes resident.Current write/accounting sequence
DiskCache.writeFilecurrently follows this sequence:pkg/fileservice/disk_cache.go:536-545);*os.File(disk_cache.go:559-568);Syncit (disk_cache.go:570-580);disk_cache.go:583-594);disk_cache.go:599-601).Until step 6, the bytes are physically present on disk but absent from
d.cache.Used(). S3 full-object streaming and other non-file readers commonly have no size estimate at step 3.For example, if the configured cache has 1 GiB available and four goroutines concurrently stream 1 GiB objects, all four can create and grow temp files before any of them updates FIFO usage. Physical usage can grow by roughly 4 GiB even though every writer individually observes the same 1 GiB capacity state.
The existing ENOSPC path is reactive: it attempts eviction only after a write has already failed. That can help a later request but cannot protect the shared volume from transient exhaustion.
Why the obvious small fixes are insufficient
Statfor the source size: this only works for*os.File, not the S3 streaming path that matters for full-object caching.These mitigations may reduce probability, but none establishes the invariant that resident + temp + in-flight cache bytes remain within a defined physical budget.
Required correctness model
A correct implementation needs a reservation token with an explicit lifecycle, for example:
The implementation must define and preserve the following invariants:
updatingPathsor FIFO callbacks;Modification cost and risk
This crosses several contracts rather than one function:
Used.SetFile/S3 streaming must use the same reservation contract.io.Copy,Sync,Stat,Close,Rename, duplicate detection, and cache admission needs an audited release/commit decision.updatingPaths, and FIFO callbacks must not form a lock cycle or starve cache writers.writeFilecurrently logs and masks cache-write errors. We need to decide which quota/cancellation errors remain best-effort cache misses and which must be observable through metrics or return values.The highest regression risks are quota-token leaks that permanently block caching, double release/commit that corrupts accounting, and lock-order mistakes that turn disk pressure into a service hang.
Proposed implementation sequence
This is implementable, but it should be a dedicated design-and-fix PR rather than a small patch that only lowers the probability of overcommit. A partial PR that claims to fix the capacity invariant without covering the lifecycle above would be more dangerous than keeping the limitation explicit and tracked here.
LeftHandCold commentedon Jul 31, 2026
2026-08-01 current-main re-evaluation
Rechecked against
main@ecc389d4206b310784595ef4e1c131c97908ab4a. The defect is still present, and the decision remains deferred / no partial PR.Current code still has the same accounting gap
DiskCache.writeFilestill:Before step 4, physical temporary and in-flight bytes are absent from
d.cache.Used(). The pre-write estimate is still available only for an*os.File; the production S3 full-object streaming path passes afullObjectDiskCacheReader, so it remains unknown-length and unreserved. Different paths may write concurrently.Live error paths close and remove their temporary file, but that does not provide admission control. After a process crash, startup keeps cache temp files younger than eight hours, and those bytes are also outside FIFO accounting.
Q1-Q3 result
updatingPaths/ eviction callback wait chain.resident + temp + in-flightbytes.Why the small alternatives remain unsafe
Statdoes not cover S3/unknown-length streams;A complete fix still needs an atomic
Reserve/Grow/Commit/Releaselifecycle covering range/full-file producers, dynamic capacity, eviction, cancellation, duplicate paths, every write failure, and restart/orphan cleanup. The highest regression risks remain leaked quota that permanently disables caching and lock-order mistakes that turn disk pressure into a service hang.Therefore I am keeping this Issue deferred until that contract and its fault-injection matrix can be implemented as one dedicated change.
fix(fileservice): harden disk cache async update lifecycle (#27368)
fix(fileservice): harden disk cache async update lifecycle (matrixori…
fix(fileservice): harden disk cache async update lifecycle (matrixori…
fix(fileservice): harden disk cache async update lifecycle (matrixori…
loveRhythm1990 commentedon Oct 14, 2026
Cross-linking a related-but-distinct issue: #30167 — observed a production ENOSPC on
DiskCachewrites caused by ext4 per-directory htree capacity exhaustion (unsharded cache directory), not the concurrent/unknown-length byte-accounting gap described here. Also notesisDiskFull()doesn't recognize ENOSPC, so the reactive eviction mentioned in this issue's root-cause section never actually engages for that error string.