fix(cli): use atomic write in save_config_value to prevent config loss on interrupt#4276
Closed
binhnt92 wants to merge 1 commit into
Closed
fix(cli): use atomic write in save_config_value to prevent config loss on interrupt#4276binhnt92 wants to merge 1 commit into
binhnt92 wants to merge 1 commit into
Conversation
…s on interrupt save_config_value() uses bare open(config_path, 'w') + yaml.dump() to write config.yaml. The 'w' mode truncates the file to zero bytes immediately on open, before any data is written. If the process is interrupted (Ctrl+C, OOM kill, power loss) between truncation and completion of yaml.dump(), config.yaml is left empty — losing all user configuration including API keys, model settings, and preferences. The gateway config write path was already fixed to use atomic_yaml_write() (temp file + fsync + os.replace) but the CLI path was missed. Every /set, /reasoning, /thinking, /personality, and /skin command goes through save_config_value().
This was referenced Mar 31, 2026
Contributor
|
Merged via PR #4320. Your fix was cherry-picked onto current main with your authorship preserved in git log. Tests were replaced with ones that test save_config_value() directly (mock verification + functional tests) rather than source-code parsing. Thanks for the catch — save_config_value() was the only remaining non-atomic config writer! |
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.
save_config_value()uses bareopen(config_path, 'w')+yaml.dump()to write config.yaml. The'w'mode truncates the file to zero bytes immediately on open, before any data is written. If the process is interrupted between truncation and completion ofyaml.dump(), config.yaml is left empty — all user configuration (API keys, model settings, preferences) is gone.The gateway config write path already uses
atomic_yaml_write()(temp file + fsync +os.replace) but the CLI path was missed. Every/set,/reasoning,/thinking,/personality, and/skincommand goes throughsave_config_value().Changes Made
Replaced the bare
open(path, 'w')+yaml.dump()insave_config_value()with the existingatomic_yaml_write()utility fromutils.py.How to Test
5 tests: atomic write preserves existing keys, creates new files, handles existing files, source verification (no bare
open('w'),atomic_yaml_writepresent).Checklist