Skip to content

fix(settings): keep changes made by concurrent rustup processes - #5297

Open
cachebag wants to merge 6 commits into
rust-lang:mainfrom
cachebag:fix/settings-lost-update
Open

cachebag wants to merge 6 commits into
rust-lang:mainfrom
cachebag:fix/settings-lost-update

Conversation

@cachebag

@cachebag cachebag commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

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 main as 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 reads settings.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:

rustup set profile minimal & rustup set auto-self-update disable & wait

One of the two settings was missing afterwards in 100 out of 100 runs for me. Also, five rustup set calls in parallel against a fresh RUSTUP_HOME lost 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.toml itself 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 to LockFile from fix(dist/download): make downloads and cleanup safe across processes #5288) opens a file, takes its exclusive lock, and offers read() and write(). 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.
  • An empty settings.toml is 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.toml used 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 minimal was mostly noise from what I gathered when comparing it in a debug build.

Comment thread src/settings.rs Resolved
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.
@rajendra0000

Copy link
Copy Markdown

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

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants