Repository navigation
refactor(utils): replace hand-rolled atomic file writes with one WriteFileAtomic - #1932
Merged
Merged
Conversation
…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
Contributor
|
🎉 This PR is included in version 0.266.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
saveLeasesLockedreturned a bareos.Renameerror that left its temp file behind. They now share oneutils.WriteFileAtomic, next to the other one-helper-per-file utilities. no docs ticket: internal-only refactorChanges
internal/platform/utils/writefileatomic.go:WriteFileAtomic(path, data, perm)creates.<name>.tmp-*infilepath.Dir(path), then writes, syncs, closes, chmods topermand renames. A deferred remove deletes the temp file on every failure path, a failed rename included.internal/tools/memory.go: the privatewriteFileAtomicis deleted. Its two callers (the memory write andwriteIndexEntries) callutils.WriteFileAtomic(path, []byte(content), 0o644). The issue assumed both callers already had anos.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 theMemoryTooldoc.internal/platform/storage/jsonl.gosaveSessionGroupsLockedmarshals and then callsutils.WriteFileAtomic(indexPath, data, 0o600), wrapped asreplace session groups index: %w. The mode is the oneos.CreateTempgave the file, so permissions don't change.internal/platform/storage/jsonl_leases.gosaveLeasesLockedcallsutils.WriteFileAtomic(path, data, 0o600), wrapped aswriting the session leases: %w. Its temp file no longer leaks on a failed rename.config/utils/yamlfile.gowriteFileAtomicis left in place, becauseinternal/cannot import it without a cycle.TestMemoryTool_NoTempLeftoversand 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.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.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.