Write settings.json atomically - #1636
Merged
Merged
Conversation
`SetFlag` does a read-modify-write of `$HOME/.omsimulator/settings.json` for the two flags that carry `settings` (`--tempDir` and `--workingDir`). `SaveSettings` opened a `std::ofstream` directly on that path, which truncates it. Every OMSimulator process on the machine shares the file, so a concurrent run could parse it while it was empty or half written: ``` warning: Failed to save settings: [json.exception.parse_error.101] parse error at line 1, column 1: attempting to parse an empty input; check that your input string or stream contains the expected JSON ``` This shows up in the OpenModelica testsuite, where 11 `.mos` tests pass `--tempDir` to OMSimulator and rtest runs them in parallel under one `HOME`. 40 concurrent `--tempDir=... --version` runs produced 23 such warnings, one of them from a reader that caught a partially written file (`line 3, column 2: syntax error ... unexpected '}'`). Write to `settings.json.<pid>.tmp` in the same directory and rename it into place instead, so a reader always sees one complete version of the file. `LoadSettings` now treats an empty file as "no settings" rather than throwing, which also covers a file left behind by a process killed mid-write. The same 40-way race is clean afterwards. Assisted-by: Claude Opus 5 (1M context)
sjoelund
force-pushed
the
atomic-settings-json
branch
from
August 26, 2026 08:03
27b6f2e to
6a5bf70
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #1636 +/- ##
==========================================
- Coverage 29.26% 29.24% -0.02%
==========================================
Files 68 68
Lines 12942 12960 +18
Branches 8378 8389 +11
==========================================
+ Hits 3787 3790 +3
- Misses 7965 7979 +14
- Partials 1190 1191 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
SetFlagdoes a read-modify-write of$HOME/.omsimulator/settings.jsonfor the two flags that carrysettings(--tempDirand--workingDir).SaveSettingsopened astd::ofstreamdirectly on that path, which truncates it. Every OMSimulator process on the machine shares the file, so a concurrent run could parse it while it was empty or half written:This shows up in the OpenModelica testsuite, where 11
.mostests pass--tempDirto OMSimulator and rtest runs them in parallel under oneHOME. 40 concurrent--tempDir=... --versionruns produced 23 such warnings, one of them from a reader that caught a partially written file (line 3, column 2: syntax error ... unexpected '}').Write to
settings.json.<pid>.tmpin the same directory and rename it into place instead, so a reader always sees one complete version of the file.LoadSettingsnow treats an empty file as "no settings" rather than throwing, which also covers a file left behind by a process killed mid-write. The same 40-way race is clean afterwards.Assisted-by: Claude Opus 5 (1M context)
Related Issues
Purpose
Approach