-
Notifications
You must be signed in to change notification settings - Fork 116
fix: use atomic writes for state.json to prevent corruption #600
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,79 @@ import ( | |
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAtomicWriteFile(t *testing.T) { | ||
| t.Run("writes file atomically", func(t *testing.T) { | ||
| t.Parallel() | ||
| tmpDir := t.TempDir() | ||
| target := filepath.Join(tmpDir, "state.json") | ||
| data := []byte(`{"status":"running"}`) | ||
|
|
||
| err := atomicWriteFile(target, data, 0o644) | ||
| assert.NoError(t, err) | ||
|
|
||
| content, err := os.ReadFile(target) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, data, content) | ||
| }) | ||
|
|
||
| t.Run("overwrites existing file", func(t *testing.T) { | ||
| t.Parallel() | ||
| tmpDir := t.TempDir() | ||
| target := filepath.Join(tmpDir, "state.json") | ||
|
|
||
| err := os.WriteFile(target, []byte("old"), 0o600) | ||
| assert.NoError(t, err) | ||
|
|
||
| newData := []byte("new content") | ||
| err = atomicWriteFile(target, newData, 0o644) | ||
| assert.NoError(t, err) | ||
|
|
||
| content, err := os.ReadFile(target) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, newData, content) | ||
| }) | ||
|
|
||
| t.Run("no temp file left on success", func(t *testing.T) { | ||
| t.Parallel() | ||
| tmpDir := t.TempDir() | ||
| target := filepath.Join(tmpDir, "state.json") | ||
|
|
||
| err := atomicWriteFile(target, []byte("data"), 0o644) | ||
| assert.NoError(t, err) | ||
|
|
||
| tmpFile := filepath.Join(tmpDir, ".state.json.tmp") | ||
| _, err = os.Stat(tmpFile) | ||
| assert.True(t, os.IsNotExist(err), "Temp file should not exist after successful write") | ||
| }) | ||
|
|
||
| t.Run("fails on invalid directory", func(t *testing.T) { | ||
| t.Parallel() | ||
| target := filepath.Join("/nonexistent/dir", "state.json") | ||
|
|
||
| err := atomicWriteFile(target, []byte("data"), 0o644) | ||
| assert.Error(t, err) | ||
| }) | ||
|
Comment on lines
+73
to
+79
|
||
|
|
||
| t.Run("rename failure cleans up temp file", func(t *testing.T) { | ||
| t.Parallel() | ||
| tmpDir := t.TempDir() | ||
|
|
||
| // Create a directory at the target path so rename fails. | ||
| target := filepath.Join(tmpDir, "state.json") | ||
| err := os.Mkdir(target, 0o755) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = atomicWriteFile(target, []byte("data"), 0o644) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "failed to rename temp file") | ||
|
|
||
| // Verify the temp file was cleaned up. | ||
| tmpFile := filepath.Join(tmpDir, ".state.json.tmp") | ||
| _, err = os.Stat(tmpFile) | ||
| assert.True(t, os.IsNotExist(err), "Temp file should be cleaned up after rename failure") | ||
| }) | ||
| } | ||
|
|
||
| func TestWritePidFile(t *testing.T) { | ||
| tmpDir := t.TempDir() // Create a temporary directory for the test | ||
| pidFilePath := filepath.Join(tmpDir, "test.pid") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.