Fix parallel locking when -Zfine-grain-locking is enabled#16659
Fix parallel locking when -Zfine-grain-locking is enabled#16659epage merged 1 commit intorust-lang:masterfrom
-Zfine-grain-locking is enabled#16659Conversation
c4c6dd1 to
0bb2474
Compare
| tracing::Span::current().record("key", key.0.to_str()); | ||
|
|
||
| let mut locks = self.locks.lock().unwrap(); | ||
| let mut locks = self.locks.write().unwrap(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
#16658 unblocks CI |
|
@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`.
Head branch was pushed to by a user without write access
0bb2474 to
31d3b42
Compare
|
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. |
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)
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)
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)
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)
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)
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)
What does this PR try to resolve?
Split out of #16657
Currently we are holding a
MutexGuardwhile 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 multithreadedJobsection, we takeRwLock::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 1will not work with the changes in that PR.We would need to do
--jobs 2and 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 #16657r? @epage