fix(config): serialize .env read-modify-write to close a lost-update race (RAH-02) - #77208
Open
JoaoMarcos44 wants to merge 1 commit into
Open
fix(config): serialize .env read-modify-write to close a lost-update race (RAH-02)#77208JoaoMarcos44 wants to merge 1 commit into
JoaoMarcos44 wants to merge 1 commit into
Conversation
…race (RAH-02) save_env_value()/remove_env_value() each read the whole .env file, mutate an in-memory line list, and atomic-replace the file. atomic_replace() rules out a torn/partial write, but two concurrent callers (two CLI/desktop processes editing credentials at once) can both read the same snapshot and each write their own version — the loser's change vanishes silently, with no error and a perfectly valid resulting file. Add a cross-process advisory lock (reusing auth.py's existing fcntl/ msvcrt _file_lock helper) around the read-transform-write cycle in both functions. Verified the race reproduces reliably (3/3 runs) against the unlocked code with a threading.Barrier-synchronized test, and is closed by the lock. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Closed
6 tasks
tneemo
reviewed
Aug 3, 2026
tneemo
left a comment
There was a problem hiding this comment.
Independent verification — #77208 (config .env lost-update race)
Verified the PR head (7399ca38, 2 files) on a real checkout:
What the PR does: serializes the .env read-modify-write cycle in save_env_value() / remove_env_value() with the existing cross-process _file_lock (same lock already used for auth.json). The author reproduced the lost update 3/3 runs with a threading.Barrier-synchronized test and added a regression test for it.
Verification results:
- ✅ Head files fetched and
ast.parseclean (config.py5464 lines,_env_file_lock+ lazy_file_lockimport present as described) - ✅
test_concurrent_saves_of_distinct_keys_both_survivepasses (1 passed) — the actual race regression test works - ✅ Full
test_config.pysuite: 74 passed, 1 failed — the failure (test_default_path) is environmental (this box has a customHERMES_HOMEset, soPath.home()/.hermes≠$HERMES_HOME) and fails identically on main without the PR — not a regression - ✅ Lock scoping is correct: wraps the whole read-transform-write, no reentrancy needed, lazy import avoids the auth↔config cycle
- ✅ Only the two files claimed are changed; behavior unchanged when uncontended (the lock is advisory, cross-process, same pattern as
auth.json)
Design notes:
- Reusing
_file_lockinstead of inventing a new lock is the right call — one locking primitive for bothauth.jsonand.env - The
export KEY=handling (#6659/#40041) is preserved inside the lock — no behavior drift - 15s timeout with a clear error message is sane for an advisory lock on a tiny file
Verdict: Ready to land. Real bug (silent key loss under concurrency), surgical fix, regression test included, no regressions.
This was generated by AI, Review is declarative
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.
Summary
Closes #77187.
save_env_value()/remove_env_value()(hermes_cli/config.py) each read the whole.envfile, mutate an in-memory line list, and atomic-replace the file.atomic_replace()rules out a torn/partial write, but there is no lock around the read-modify-write cycle, so two concurrent callers can both read the same on-disk snapshot and each atomic-replace their own version. The second write wins in full; the first caller's change disappears silently — no error, no partial file, just a perfectly valid.envmissing one side's key.Reproduced with a
threading.Barrier-synchronized test: two threads save distinct keys off the same base snapshot — 3/3 runs against the unlocked code lose one key.Fix
Reused the existing cross-process advisory lock (
_file_lock, already used forauth.jsoninhermes_cli/auth.py) around the read-transform-write cycle in both functions.%%{init: {'theme': 'dark', 'themeVariables': { 'primaryColor': '#8b0000', 'mainBkg': '#0a0204', 'primaryTextColor': '#ffccd5', 'primaryBorderColor': '#ff0038', 'lineColor': '#ff0038'}}}%% graph TD A[🩸 Writer A: save RACE_A] -->|read snapshot| C[🔥 _file_lock cross-process] B[🩸 Writer B: save RACE_B] -->|read snapshot| C C -->|serialized read-modify-write| D[⚔️ Atomic Replace] D --> E[🩸 Both RACE_A and RACE_B survive]Infographic:
Test plan
TestSaveEnvValueConcurrency::test_concurrent_saves_of_distinct_keys_both_survivetests/hermes_cli/test_config.py(env save/remove tests) — passes, no regressionspython -m py_compile hermes_cli/config.py