-
Notifications
You must be signed in to change notification settings - Fork 214
direct: write deployment state atomically #6329
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+28
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Write the deployment state atomically so an interrupted save cannot leave a state file that the CLI refuses to read. | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -574,6 +574,14 @@ func (db *DeploymentState) ExportState(ctx context.Context) resourcestate.Export | |
| return ExportStateFromData(db.Data) | ||
| } | ||
|
|
||
| // unlockedSave persists the in-memory state to db.Path by writing a temp file in | ||
| // the same directory and renaming it over the destination, so an interrupted save | ||
| // cannot leave a half-written state file behind. | ||
| // | ||
| // Writing in place would be unrecoverable: replayWAL saves the merged state and | ||
| // only then removes the WAL, and Open parses the state file before it looks at | ||
| // the WAL. A torn write would therefore leave a state file that Open rejects | ||
| // next to an intact WAL it never reads. | ||
| func (db *DeploymentState) unlockedSave() error { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a unit test for the failure paths would be good to assert this actually works |
||
| data, err := json.MarshalIndent(db.Data, "", " ") | ||
| if err != nil { | ||
|
|
@@ -585,8 +593,26 @@ func (db *DeploymentState) unlockedSave() error { | |
| return fmt.Errorf("failed to create directory %#v: %w", dir, err) | ||
| } | ||
|
|
||
| err = os.WriteFile(db.Path, data, 0o600) | ||
| // CreateTemp creates the file with mode 0o600, matching the state file. | ||
| tmp, err := os.CreateTemp(dir, "."+filepath.Base(db.Path)+".tmp-*") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create temp file for %#v: %w", db.Path, err) | ||
| } | ||
| tmpPath := tmp.Name() | ||
| // Cleans up the temp file on failure; a no-op once the rename succeeded. | ||
| defer os.Remove(tmpPath) | ||
|
|
||
| if _, err := tmp.Write(data); err != nil { | ||
| tmp.Close() | ||
| return fmt.Errorf("failed to write %#v: %w", tmpPath, err) | ||
| } | ||
|
|
||
| // Close before the rename: on Windows the file must not be open for writing. | ||
| if err := tmp.Close(); err != nil { | ||
| return fmt.Errorf("failed to close %#v: %w", tmpPath, err) | ||
| } | ||
|
|
||
| if err := os.Rename(tmpPath, db.Path); err != nil { | ||
| return fmt.Errorf("failed to save resources state to %#v: %w", db.Path, err) | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR link