Skip to content

[TASK] Refactor hand-rolled atomic file writes onto one platform/utils helper #1930

Closed

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.go writeFileAtomic(path, content string) - MkdirAll, os.CreateTemp(dir, ".tmp-memory-*"), write, Sync, close, chmod 0644, rename, deferred remove.
  • internal/platform/storage/jsonl.go saveSessionGroupsLocked - about 20 lines of os.CreateTemp / write / close / rename with a cleanup closure.
  • internal/platform/storage/jsonl_leases.go saveLeasesLocked - the same block again, repeating _ = os.Remove(tmp.Name()) on each failure branch, and returning a bare os.Rename error 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.go exports WriteFileAtomic(path string, data []byte, perm fs.FileMode) error: temp file in filepath.Dir(path), write, Sync, close, chmod to perm, 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.go defines its own writeFileAtomic. After: the private helper is deleted and its two callers (lines 199 and 545) call utils.WriteFileAtomic(path, []byte(content), 0o644) after their existing os.MkdirAll.
  • Before: saveSessionGroupsLocked in internal/platform/storage/jsonl.go hand-rolls the temp/write/close/rename block. After: it marshals, then calls utils.WriteFileAtomic(indexPath, data, 0o600) (the mode os.CreateTemp gives the file today, so permissions do not change) and wraps the error with its existing message.
  • Before: saveLeasesLocked in internal/platform/storage/jsonl_leases.go hand-rolls the same block and leaks the temp file when the rename fails. After: it calls utils.WriteFileAtomic(path, data, 0o600) (unchanged mode) and wraps the error as writing the session leases: %w.
  • config/utils/yamlfile.go writeFileAtomic is left in place (import cycle noted above).
  • Existing tests still pass.

Activity

  1. moved this from Todo to In progress in 2026on Nov 22, 2026
  2. moved this from In progress to QA in 2026on Nov 22, 2026
  3. moved this from QA to Done in 2026on Nov 22, 2026
  4. inference-gateway-releaser commented on Nov 22, 2026

    @inference-gateway-releaser
    Contributor

    🎉 This issue has been resolved in version 0.266.1 🎉

    The release is available on:

    Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    refactorreleased

    Type

    Projects

    • Status
      Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions