Skip to content

Fix parallel locking when -Zfine-grain-locking is enabled#16659

Merged
epage merged 1 commit intorust-lang:masterfrom
ranger-ross:fix-locking
Feb 21, 2026
Merged

Fix parallel locking when -Zfine-grain-locking is enabled#16659
epage merged 1 commit intorust-lang:masterfrom
ranger-ross:fix-locking

Conversation

@ranger-ross
Copy link
Member

What does this PR try to resolve?

Split out of #16657

Currently we are holding a MutexGuard while waiting for the file lock, preventing multiple locks from being taken at once.
This means that a single blocking unit can cause other units from running.

For the lock manager, we only insert into hashmap during the single threaded prepare fingerprint so we take RwLock::write(). During the multithreaded Job section, we take RwLock::read() to the hashmap.

How to test and review this PR?

See the "How to test and review this PR" section of #16657

However, unlike that PR, --jobs 1 will not work with the changes in that PR.
We would need to do --jobs 2 and make sure that the first build unit blocked causing the second one to block. Though I think this is a bit difficult to setup a test for without the changes in #16657

r? @epage

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 20, 2026
tracing::Span::current().record("key", key.0.to_str());

let mut locks = self.locks.lock().unwrap();
let mut locks = self.locks.write().unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means that a single blocking unit can cause other units from running.

Isn't this still true?

If a job is blocked, they will be holding a Read Lock. New jobs will try to grab the Write lock and be blocked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LockManager::lock_shared is only called prior to reading the fingerprint (ref)
It is called for all units synchronously while preparing the jobs.

By the time we start executing jobs, all units should have already taken a shared lock and released it.
So there should be no writers after the fingerprint check phase of the build.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, for some reason, I've always assumed we did fingerprint calculations during the build and never looked too closely at the timing and operation of compile.

@epage
Copy link
Contributor

epage commented Feb 20, 2026

#16658 unblocks CI

@epage epage enabled auto-merge February 20, 2026 18:36
@epage
Copy link
Contributor

epage commented Feb 20, 2026

@ranger-ross you likely need to rebase this

This commit replaces a `Mutex` with an `RwLock` lock to avoid holding a
mutex guard while waiting on a file lock. Instead we hold a read guard
which allows multiple threads to access the `HashMap` inside of
`LockManager`.
auto-merge was automatically disabled February 21, 2026 04:27

Head branch was pushed to by a user without write access

@rustbot
Copy link
Collaborator

rustbot commented Feb 21, 2026

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@epage epage added this pull request to the merge queue Feb 21, 2026
Merged via the queue into rust-lang:master with commit 4282eec Feb 21, 2026
29 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 21, 2026
arlosi added a commit to arlosi/rust that referenced this pull request Feb 25, 2026
8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0
2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
rust-bors bot pushed a commit to rust-lang/rust that referenced this pull request Feb 26, 2026
Update cargo submodule

8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0 2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
@rustbot rustbot added this to the 1.95.0 milestone Feb 26, 2026
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Feb 26, 2026
Update cargo submodule

8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0 2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
github-actions bot pushed a commit to rust-lang/rust-analyzer that referenced this pull request Mar 2, 2026
Update cargo submodule

8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0 2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
Update cargo submodule

8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0 2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
makai410 pushed a commit to makai410/rustc_public that referenced this pull request Mar 19, 2026
Update cargo submodule

8 commits in 8cc0cb136772b8f54eafe0d163fcb7226a06af0c..f298b8c82da0cba538516b45b04a480fc501d4c0 2026-02-17 12:16:26 +0000 to 2026-02-24 21:59:20 +0000
- fix(host-config): fix panic when cross-compiling with host-config (rust-lang/cargo#16674)
- doc: improve documentation on using cfg values with build scripts (rust-lang/cargo#16671)
- Fix typo in cargo-yank docs (rust-lang/cargo#16656)
- fix(job_queue): Handle Clippy CLI arguments in `fix` message (rust-lang/cargo#16652)
- Add a test for fingerprint checking when a symlink target changes (rust-lang/cargo#16661)
- Fix parallel locking when `-Zfine-grain-locking` is enabled (rust-lang/cargo#16659)
- fix(cli): Remove `--lockfile-path` (rust-lang/cargo#16621)
- test(build-std): Update error message (rust-lang/cargo#16658)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants