Skip to content

refactor(utils): replace hand-rolled atomic file writes with one WriteFileAtomic - #1932

Merged
edenreich merged 1 commit into
mainfrom
refactor/write-file-atomic
Nov 22, 2026
Merged

edenreich merged 1 commit into
mainfrom
refactor/write-file-atomic

Conversation

@edenreich

Copy link
Copy Markdown
Contributor

Resolves #1930

Summary

The "temp file in the same directory, close, rename over the target" sequence was hand-rolled three times, and the copies had drifted. Only the memory tool fsynced before the rename, only the session-groups writer cleaned up after a failed rename, and saveLeasesLocked returned a bare os.Rename error that left its temp file behind. They now share one utils.WriteFileAtomic, next to the other one-helper-per-file utilities. no docs ticket: internal-only refactor

Changes

  • New internal/platform/utils/writefileatomic.go: WriteFileAtomic(path, data, perm) creates .<name>.tmp-* in filepath.Dir(path), then writes, syncs, closes, chmods to perm and renames. A deferred remove deletes the temp file on every failure path, a failed rename included.
  • internal/tools/memory.go: the private writeFileAtomic is deleted. Its two callers (the memory write and writeIndexEntries) call utils.WriteFileAtomic(path, []byte(content), 0o644). The issue assumed both callers already had an os.MkdirAll. They didn't - it lived inside the deleted helper - so each caller now creates the parent directory itself, as before. The helper's note on bypassing the sandboxed file writer moved to the MemoryTool doc.
  • internal/platform/storage/jsonl.go saveSessionGroupsLocked marshals and then calls utils.WriteFileAtomic(indexPath, data, 0o600), wrapped as replace session groups index: %w. The mode is the one os.CreateTemp gave the file, so permissions don't change.
  • internal/platform/storage/jsonl_leases.go saveLeasesLocked calls utils.WriteFileAtomic(path, data, 0o600), wrapped as writing the session leases: %w. Its temp file no longer leaks on a failed rename.
  • config/utils/yamlfile.go writeFileAtomic is left in place, because internal/ cannot import it without a cycle.
  • TestMemoryTool_NoTempLeftovers and the session-groups leak check matched on the old temp-name prefixes, which would have made them pass silently. They now flag any .tmp- entry.
  • New table test writefileatomic_test.go: create a new file, replace an existing file (checking content and mode), and a failed rename onto a non-empty directory that must leave no temp file.

Verification

  • go test ./... - all 129 packages pass.
  • With the deferred temp-file remove deleted, the failed-rename case fails with temp file left behind: .target.json.tmp-…. It passes once the remove is restored.
  • task precommit:run - mod tidy, mocks, fmt, lint:imports and golangci-lint pass with 0 issues.

…eFileAtomic

The temp-file-and-rename write was copied three times and the copies had
drifted. Only the memory tool fsynced before the rename, and the session
leases writer left its temp file behind when the rename failed.

- utils.WriteFileAtomic writes a synced temp file next to the target,
  applies the mode and renames it into place. The temp file is removed on
  every failure, a failed rename included.
- The memory tool, the session groups index and the session leases write
  through it with the modes they had (0644 for memory, 0600 for storage).
  The memory tool's private helper is gone, and its two callers create the
  parent directory themselves as the helper did.
- config/utils keeps its own symlink-aware writer, which internal/ cannot
  import without a cycle.
- The temp-file leak checks in the memory and storage tests match the new
  temp names.

Resolves #1930
@github-project-automation github-project-automation Bot moved this to Todo in 2026 Nov 22, 2026
@edenreich
edenreich marked this pull request as ready for review November 22, 2026 17:51
@edenreich
edenreich requested a review from a team as a code owner November 22, 2026 17:51
@edenreich
edenreich merged commit 684f6d3 into main Nov 22, 2026
14 checks passed
@edenreich
edenreich deleted the refactor/write-file-atomic branch November 22, 2026 17:52
@edenreich edenreich moved this from Todo to Done in 2026 Nov 22, 2026
@inference-gateway-releaser

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 0.266.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

@inference-gateway-releaser inference-gateway-releaser Bot added the released Shipped in a release label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released Shipped in a release

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

1 participant