From 7a3d6cf3f9106513befeebb4d92f6ca7558cb038 Mon Sep 17 00:00:00 2001 From: Nadav0077 <18245584+Nadav0077@users.noreply.github.com> Date: Fri, 17 Apr 2026 23:19:51 +0300 Subject: [PATCH] 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). --- accounts/keystore/key.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) 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 }