Clean up temporary file when filesystem PutContent Commit fails
perfloop-oss/distribution · Partial state after failure
https://perfloop.ai/t/oss/case_j864hmq2ez
Verdict
VERIFIED · settled 2026-11-27 · merged as distribution/distribution#5102
What happened: The check fails before this change and passes with it.
Hypothesis
When the filesystem storage driver's PutContent fails while committing a write to disk, it leaves behind an orphaned temporary file (<name>.<uuid>.tmp) inside the live storage directory. Because PutContent buffers writes in a bufio.Writer (default 4 KiB, configurable up to megabytes), nearly all registry PutContent writes (71-byte digest links, upload timestamps, hash state checkpoints, and small manifests) never hit disk during io.Copy and instead perform their first write(2) and fsync(2) inside writer.Commit; under disk-full (ENOSPC), quota/fsize, or I/O errors, every failed PutContent leaks a permanent .tmp file into the registry tree (violating the cleanup invariant asserted in TestPutContent and TestPutContentMoveFailure). Real-world impact and reach (engineering prior): filesystem is the default storage driver, and disk-full or I/O failures during Commit are rare_input runtime failure states whose orphaned files can accumulate under pressure and pollute directory listings until manual cleanup; production frequency is unmeasured here.
In registry/storage/driver/filesystem/driver.go (driver.PutContent, lines 182-218), lines 202-204 return err directly when writer.Commit(ctx) fails without calling writer.Cancel or d.Delete(ctx, tempPath). In fileWriter.Commit (lines 475-494), fw.bw.Flush() (line 484) and fw.file.Sync() (line 488) run before fw.committed = true (line 492); when Flush or Sync fails, PutContent returns immediately and the deferred writer.Close() closes the file descriptor without removing tempPath, leaving the temporary file on disk.
The attached deterministic check TestPutContentCommitFailureLeaksTempFile (provisional test placement) reproduces the temp-file leak in 0.00s using RLIMIT_FSIZE=64 with a 128-byte payload (below the 4 KiB bufio buffer), confirming that startedat.<uuid>.tmp remains on disk after PutContent returns an error. Upstream GitHub open PRs were checked; PR #5094 touches filesystem/driver.go only to reject unknown configuration parameters and does not touch PutContent.
To confirm the fix, the attached test must pass with zero leaked files after Commit failure while preserving existing TestPutContent and TestPutContentMoveFailure behavior.
Change to test: Clean up the temporary file when writer.Commit fails in filesystem.driver.PutContent (such as calling writer.Cancel or closing and removing tempPath before returning), so failed commits leave no orphaned .tmp file on disk.
Where it lives
perfloop-oss/distribution · registry/storage/driver/filesystem/driver.go
Evidence
The check fails before this change and passes with it: `Filesystem PutContent removes its temporary file after a buffered Commit write failure and preserves an existing destination when the temporary file can be closed and removed.`
Checks: 2 of 2 passed. Verification: no defect found.