diff --git a/accounts/keystore/key.go b/accounts/keystore/key.go index 9b2ac14712..6a554d831c 100644 --- a/accounts/keystore/key.go +++ b/accounts/keystore/key.go @@ -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 }