fix(config): atomic write for .env to prevent API key loss on crash#842
Closed
alireza78a wants to merge 1 commit intoNousResearch:mainfrom
Closed
fix(config): atomic write for .env to prevent API key loss on crash#842alireza78a wants to merge 1 commit intoNousResearch:mainfrom
alireza78a wants to merge 1 commit intoNousResearch:mainfrom
Conversation
teknium1
pushed a commit
that referenced
this pull request
Mar 11, 2026
save_env_value() used bare open('w') which truncates .env immediately.
A crash or OOM kill between truncation and completed write silently
wipes every credential in the file.
Write now goes to a temp file first, then os.replace() swaps it
atomically. Either the old .env exists or the new one does — never
a truncated half-write. Same pattern used in cron/jobs.py.
Cherry-picked from PR #842 by alireza78a, rebased onto current main
with conflict resolution (_secure_file refactor).
Co-authored-by: alireza78a <alireza78a@users.noreply.github.com>
Contributor
|
Merged via PR #954. Your commit was cherry-picked with authorship preserved — the branch was 165 commits behind main with a conflict in the |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
save_env_value()uses bareopen('w')which truncates.envimmediately.A crash or OOM kill between truncation and the completed write silently
wipes every credential in the file.
How It Works
Write goes to a temp file in the same directory first, then
os.replace()swaps it atomically. Either the old
.envexists or the new one does —never a truncated half-write.
Same pattern already used in
save_jobs()incron/jobs.py.Impact
Prevents silent loss of
OPENROUTER_API_KEY,TELEGRAM_BOT_TOKEN,DISCORD_BOT_TOKEN,NOUS_API_KEY, and every other key stored in.env.Tests
173 hermes_cli tests pass.