Skip to content

Commit 09b1de5

Browse files
authored
Merge pull request #954 from NousResearch/hermes/hermes-20ea56c0
fix(config): atomic write for .env to prevent API key loss on crash
2 parents 66c0b71 + 3667138 commit 09b1de5

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

hermes_cli/config.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import stat
1818
import subprocess
1919
import sys
20+
import tempfile
2021
from pathlib import Path
2122
from typing import Dict, Any, Optional, List, Tuple
2223

@@ -958,8 +959,19 @@ def save_env_value(key: str, value: str):
958959
lines[-1] += "\n"
959960
lines.append(f"{key}={value}\n")
960961

961-
with open(env_path, 'w', **write_kw) as f:
962-
f.writelines(lines)
962+
fd, tmp_path = tempfile.mkstemp(dir=str(env_path.parent), suffix='.tmp', prefix='.env_')
963+
try:
964+
with os.fdopen(fd, 'w', **write_kw) as f:
965+
f.writelines(lines)
966+
f.flush()
967+
os.fsync(f.fileno())
968+
os.replace(tmp_path, env_path)
969+
except BaseException:
970+
try:
971+
os.unlink(tmp_path)
972+
except OSError:
973+
pass
974+
raise
963975
_secure_file(env_path)
964976

965977
# Restrict .env permissions to owner-only (contains API keys)

0 commit comments

Comments
 (0)