fix: atomically write config.json via temp+rename#1387
Merged
Conversation
Avoid truncated or half-written ~/.aliyun/config.json when CloudSSO/OAuth STS refresh races with configure set, or the process is interrupted mid-write. Use same-directory unique temp files and os.Rename (Windows-safe replace).
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #1387 +/- ##
==========================================
+ Coverage 84.91% 85.11% +0.20%
==========================================
Files 69 69
Lines 8981 9018 +37
==========================================
+ Hits 7626 7676 +50
+ Misses 915 899 -16
- Partials 440 443 +3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR updates configuration persistence to use an atomic temp-file + rename strategy when writing config.json, reducing the risk of corrupted/truncated configs during concurrent writes (e.g., refresh flows racing with configure set).
Changes:
- Switch
SaveConfiguration/SaveConfigurationWithContextto write viaatomicWriteFile(same-directory temp file thenos.Rename). - Normalize
config.jsonpath building withfilepath.Joinin the affected save paths. - Add unit tests covering overwrite behavior and ensuring temp files are not left behind after successful writes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| config/configuration.go | Introduces atomicWriteFile and routes config saves through it for atomic replace semantics. |
| config/configuration_test.go | Adds tests validating overwrite behavior and that temp files are cleaned up. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+254
to
+268
| func atomicWriteFile(path string, data []byte, perm os.FileMode) error { | ||
| dir := filepath.Dir(path) | ||
| base := filepath.Base(path) | ||
| tempFile := filepath.Join(dir, "."+base+".tmp-"+strconv.Itoa(os.Getpid())+"-"+strconv.FormatInt(time.Now().UnixNano(), 10)) | ||
|
|
||
| if err := os.WriteFile(tempFile, data, perm); err != nil { | ||
| return fmt.Errorf("failed to write temp config %q: %w", tempFile, err) | ||
| } | ||
|
|
||
| if err := os.Rename(tempFile, path); err != nil { | ||
| _ = os.Remove(tempFile) | ||
| return fmt.Errorf("failed to rename temp config to %q: %w", path, err) | ||
| } | ||
| return nil | ||
| } |
Use collision-safe temporary files, sync content before replacement, and preserve existing config symlinks so interrupted writes do not put user configuration at risk.
Cover symlink/create/inspect/temp-file failure paths and SaveConfigurationWithContext custom path creation.
Contributor
Author
Follow-up
|
Parent-is-file paths return IsNotExist on Windows, so inject Lstat to cover the inspect-error branch on all platforms.
CodeSpaceiiii
approved these changes
Jul 17, 2026
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.
Summary