Skip to content

Commit 0d67b4e

Browse files
committed
OCPBUGS-60273: Ensure revision.json persists on ungraceful shutdown
Add os.O_SYNC flag to OpenFile call in trySaveRevision to force synchronous writes to disk. This prevents the revision.json file from being empty if the process terminates unexpectedly before buffered data is flushed.
1 parent 0357803 commit 0d67b4e

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

pkg/cmd/rev/rev.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,23 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
189189
}
190190

191191
tmpPath := fmt.Sprintf("%s.tmp", outputFile)
192-
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
192+
file, err := os.OpenFile(tmpPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC|os.O_SYNC, 0644)
193193
if err != nil {
194194
klog.Errorf("error opening file: %v", err)
195195
return
196196
}
197197
defer func() {
198-
if err = file.Close(); err != nil {
199-
klog.Errorf("error closing file: %v", err)
198+
if closeErr := file.Close(); closeErr != nil {
199+
klog.Errorf("error closing file: %v", closeErr)
200+
}
201+
// Only rename if open and write succeeded
202+
if err != nil {
203+
klog.Errorf("could not create temporary revision.json, keeping current version: %v", err)
204+
return
205+
}
206+
if err = os.Rename(tmpPath, outputFile); err != nil {
207+
klog.Errorf("error during rename to destination file: %v", err)
208+
return
200209
}
201210
}()
202211

@@ -205,11 +214,6 @@ func trySaveRevision(ctx context.Context, endpoints []string, outputFile string,
205214
klog.Errorf("error writing result to file: %v", err)
206215
return
207216
}
208-
209-
if err = os.Rename(tmpPath, outputFile); err != nil {
210-
klog.Errorf("error during rename to destination file: %v", err)
211-
return
212-
}
213217
}
214218

215219
func newETCD3Client(ctx context.Context, endpoints []string, tlsInfo transport.TLSInfo) (*clientv3.Client, error) {

0 commit comments

Comments
 (0)