accounts/keystore: fsync temp keyfile before rename

Without this, a crash or power loss between the write and the rename can leave the final UTC--... keyfile on disk with zero bytes, which permanently loses the newly generated key. Mirrors the fix in core/rawdb for freezer index files (573d94013).
This commit is contained in:
Nadav0077 2026-04-17 23:19:51 +03:00
parent 573d94013c
commit 7a3d6cf3f9
No known key found for this signature in database

View file

@ -205,7 +205,18 @@ func writeTemporaryKeyFile(file string, content []byte) (string, error) {
os.Remove(f.Name())
return "", err
}
f.Close()
// Persist the contents to disk before the caller renames it into place,
// so that a crash or power loss between the write and the rename cannot
// leave a zero-length keyfile at the final path.
if err := f.Sync(); err != nil {
f.Close()
os.Remove(f.Name())
return "", err
}
if err := f.Close(); err != nil {
os.Remove(f.Name())
return "", err
}
return f.Name(), nil
}