Repository navigation
Description
Summary
The "write to a temp file in the same directory, close, rename over the target" sequence is hand-rolled three times in packages that already import the shared helper package internal/platform/utils:
internal/tools/memory.gowriteFileAtomic(path, content string)- MkdirAll,os.CreateTemp(dir, ".tmp-memory-*"), write,Sync, close, chmod 0644, rename, deferred remove.internal/platform/storage/jsonl.gosaveSessionGroupsLocked- about 20 lines ofos.CreateTemp/ write / close / rename with acleanupclosure.internal/platform/storage/jsonl_leases.gosaveLeasesLocked- the same block again, repeating_ = os.Remove(tmp.Name())on each failure branch, and returning a bareos.Renameerror that leaves the temp file behind when the rename fails.
The copies have already drifted: only the memory one fsyncs before the rename, and only the session-groups one cleans up after a failed rename. A fourth, more careful copy lives in config/utils/yamlfile.go (writeFileAtomic, symlink-aware and mode-preserving), but it cannot be reused from internal/ without an import cycle (config imports config/utils, and internal/platform/utils/ratelimiter.go imports config), so it stays as is.
Principles applied: clean-code (duplication - copy-pasted function bodies, one concept implemented three ways) and clean-code (functions do one thing - saveSessionGroupsLocked and saveLeasesLocked mix encoding a domain record with low-level file replacement). internal/platform/utils already follows one helper per file (fileexists.go, homedir.go, prune.go), so this adds one more file there and deletes the three copies.
Acceptance Criteria
- New
internal/platform/utils/writefileatomic.goexportsWriteFileAtomic(path string, data []byte, perm fs.FileMode) error: temp file infilepath.Dir(path), write,Sync, close, chmod toperm, rename, and the temp file is removed on every failure path including a failed rename. It keeps the fsync the memory copy has today. A small table test covers the success path and a failed rename leaving no temp file. - Before:
internal/tools/memory.godefines its ownwriteFileAtomic. After: the private helper is deleted and its two callers (lines 199 and 545) callutils.WriteFileAtomic(path, []byte(content), 0o644)after their existingos.MkdirAll. - Before:
saveSessionGroupsLockedininternal/platform/storage/jsonl.gohand-rolls the temp/write/close/rename block. After: it marshals, then callsutils.WriteFileAtomic(indexPath, data, 0o600)(the modeos.CreateTempgives the file today, so permissions do not change) and wraps the error with its existing message. - Before:
saveLeasesLockedininternal/platform/storage/jsonl_leases.gohand-rolls the same block and leaks the temp file when the rename fails. After: it callsutils.WriteFileAtomic(path, data, 0o600)(unchanged mode) and wraps the error aswriting the session leases: %w. -
config/utils/yamlfile.gowriteFileAtomicis left in place (import cycle noted above). - Existing tests still pass.
Activity
inference-gateway-releaser commentedon Nov 22, 2026
🎉 This issue has been resolved in version 0.266.1 🎉
The release is available on:
Your semantic-release bot 📦🚀