Repository navigation
Conversation
rami3l
reviewed
Dec 3, 2026
Two processes that change different settings at the same time lose one of the changes, and an empty `settings.toml` makes every command fail. The tests pin both for now.
`settings.toml` is created or truncated before the settings are written to it, so a write that fails or is interrupted leaves it empty. Every command then failed to parse it until the file was removed by hand.
`Settings::add_override()` now only changes the settings, so that it can be applied to more than one copy of them without logging twice.
Changing a setting wrote back the settings that the process had read earlier, and held the lock on `settings.toml` only for that write. Of two processes changing settings at the same time, the one writing last undid the change of the other: running `rustup set profile minimal` next to `rustup set auto-self-update disable` lost one of the two settings in each of 100 runs. `utils::LockedFile` keeps the file locked from before it is read until after it is written, and `SettingsFile::with_mut()` makes the change to what the file contains at that point. The process's own copy of the settings is not replaced by what was read from the file, as its commands would then see the changes of other processes halfway through. The change is applied to that copy as well instead, which is why the closure is now an `Fn`.
`StateFile::with_mut()` now keeps the file locked from before it is read until after it is written, like `SettingsFile::with_mut()` does. The state has a single field so far, so no change could be lost yet; this keeps the two files consistent and leaves `write_locked_file()` without users.
cachebag
force-pushed
the
fix/settings-lost-update
branch
from
December 3, 2026 17:36
3199b5b to
05c516c
Compare
|
Tested this against the exact scenario from my issue #5296. Built 05c516c (debug build on Linux), terminal 1 runs rustup default nightly while it's still downloading, terminal 2 sets an override on a local linked toolchain. After terminal 1 finishes the override is still in settings.toml, and default_toolchain lands too. Same scenario on 1.29.3 and on main c5b3a7f loses the override, so this branch fixes the reported case. Reverse order (override first, then default) works fine as well. I also ran the parallel scenario (set profile default & set auto-self-update enable, fresh RUSTUP_HOME) 20 times on each build: this branch lost a setting 0 of 20, main lost one 20 of 20. Thanks for picking this up @cachebag. |
This branch has not been deployed
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.
There is no issue related to this, and I figured it would be best to just explain my motivations for this patch below instead. I am also happy to attach any reproducers upon request (though the first commit already adds a test that reproduces the lost change and passes on
mainas is).Problem
Two rustup processes that change settings at the same time lose one of the changes.
The crux of it is in
SettingsFile::with_mut(): it readssettings.toml(or reuses what it read earlier), changes its own copy, and writes the whole file back. Since #4946 the write itself is locked, but the read is not part of that lock, so whoever writes last puts back the settings as they were before the other process touched them.This can be quite easily observed:
One of the two settings was missing afterwards in 100 out of 100 runs for me. Also, five
rustup setcalls in parallel against a freshRUSTUP_HOMElost at least one setting in 50 out of 50.A lot of this work was actually picked up off of #5192 but with a more concrete case behind it, and follows the suggestion from @rami3l's review there which was essentially to lock
settings.tomlitself instead of a separate lock file, and treat an empty file as the default settings.Proposed Solution
The idea with this patch is that a change to the settings now keeps the file locked from before it is read until after it is written, and always starts from what is in the file rather than from what the process read earlier.
utils::LockedFile(next toLockFilefrom fix(dist/download): make downloads and cleanup safe across processes #5288) opens a file, takes its exclusive lock, and offersread()andwrite(). The lock is released when it is dropped.SettingsFile::with_mut()opens the settings through it, parses them again, applies the change, writes, and also applies to the process's own cached copy, which is not replaced.settings.tomlis read as the default settings.StateFile::with_mut()gets the same treatment in the last commit.What this doesn't solve
Two processes setting the same setting still end up with whichever wrote last, which seems right to me. A long-running process also still reads the settings once and keeps that copy for reads.
Impact
It is worth noting that an empty
settings.tomlused to be a parse error and now means "default settings". TBH I think that's needed either way since the file is created or truncated before the settings are written.OTOH, settings that are lost this way now reset quietly instead of loudly. I don't think that is a huge deal, since the settings were already gone in the old case too but we can add a warning for the sake of sanity if we really wanted to.
Performance
The writers read and parse the file once more under the lock. But the difference I saw with
rustup set profile minimalwas mostly noise from what I gathered when comparing it in a debug build.