Skip to content

Add a jobserver proxy to ensure at least one token is always held #140145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Apr 22, 2025

This adds a jobserver proxy to ensure at least one token is always held by rustc. Currently with -Z threads rustc can temporarily give up all its tokens, causing cargo to spawn additional rustc instances beyond the job limit.

The current behavior causes an issue with cargo fix which has a global lock preventing concurrent rustc instances, but it also holds a jobserver token, causing a deadlock when rustc gives up its token. That is fixed by this PR.

Fixes #133873 and #140093.

@rustbot
Copy link
Collaborator

rustbot commented Apr 22, 2025

r? @jieyouxu

rustbot has assigned @jieyouxu.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 22, 2025
@jieyouxu
Copy link
Member

r? @SparrowLii

@rustbot rustbot assigned SparrowLii and unassigned jieyouxu Apr 22, 2025
@SparrowLii
Copy link
Member

cc @matthiaskrgr

@SparrowLii
Copy link
Member

Thanks!
@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 22, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 22, 2025
Add a jobserver proxy to ensure at least one token is always held

This adds a jobserver proxy to ensure at least one token is always held by `rustc`. Currently with `-Z threads` `rustc` can temporarily give up all its tokens, causing `cargo` to spawn additional `rustc` instances beyond the job limit.

The current behavior causes an issue with `cargo fix` which has a global lock preventing concurrent `rustc` instances, but it also holds a jobserver token, causing a deadlock when `rustc` gives up its token. That is fixed by this PR.

Fixes rust-lang#133873 and rust-lang#140093.
@bors
Copy link
Collaborator

bors commented Apr 22, 2025

⌛ Trying commit 141055f with merge c4cd906f6dc68c4343777d06a86285dbccb53e58...

self.wake_needer.notify_one();
} else {
drop(data);
self.client.release_raw().ok();
Copy link
Member

@SparrowLii SparrowLii Apr 22, 2025

Choose a reason for hiding this comment

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

We can add a comment to explain that another thread was released so we don't need the new thread.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't follow your comment here.

@bors
Copy link
Collaborator

bors commented Apr 22, 2025

☀️ Try build successful - checks-actions
Build commit: c4cd906 (c4cd906f6dc68c4343777d06a86285dbccb53e58)

@rust-timer

This comment has been minimized.

@bjorn3
Copy link
Member

bjorn3 commented Apr 22, 2025

cg_clif has https://github.com/rust-lang/rustc_codegen_cranelift/blob/master/src/concurrency_limiter.rs which I think does something similar to this PR.

@Zoxc
Copy link
Contributor Author

Zoxc commented Apr 22, 2025

@bjorn3 What's the purpose of ConcurrencyLimiter there? It's a bit unclear to me.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c4cd906): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.4% [0.4%, 0.4%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary -0.7%, secondary -2.2%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
1.1% [0.9%, 1.2%] 2
Regressions ❌
(secondary)
1.8% [1.8%, 1.8%] 1
Improvements ✅
(primary)
-0.9% [-1.7%, -0.5%] 12
Improvements ✅
(secondary)
-2.9% [-4.6%, -1.9%] 5
All ❌✅ (primary) -0.7% [-1.7%, 1.2%] 14

Cycles

Results (primary -0.5%, secondary -2.8%)

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.5% [-0.5%, -0.4%] 2
Improvements ✅
(secondary)
-2.8% [-3.4%, -2.2%] 12
All ❌✅ (primary) -0.5% [-0.5%, -0.4%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 775.257s -> 775.095s (-0.02%)
Artifact size: 365.06 MiB -> 365.08 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 22, 2025
@Zoxc Zoxc marked this pull request as draft April 22, 2025 10:45
@bjorn3
Copy link
Member

bjorn3 commented Apr 22, 2025

@bjorn3 What's the purpose of ConcurrencyLimiter there? It's a bit unclear to me.

It is responsible for requesting jobserver tokens and ensuring that the implicit token isn't returned to the jobserver. It also caches tokens that are expected to be used soon (it gets the total amount of cgus and only returns tokens if the amount of remaining cgus is less than the amount of tokens the compilation session has claimed in total).

@rustbot rustbot added the A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) label Apr 22, 2025
@rust-log-analyzer

This comment has been minimized.

@Zoxc Zoxc force-pushed the job-server-proxy branch from 21b5490 to 3010d39 Compare April 22, 2025 13:04
@rust-log-analyzer

This comment has been minimized.

@Zoxc Zoxc force-pushed the job-server-proxy branch from 3010d39 to 4d7eaa7 Compare April 22, 2025 13:36
@Zoxc Zoxc force-pushed the job-server-proxy branch from 4d7eaa7 to 2dfc7cd Compare April 22, 2025 13:44
@Zoxc
Copy link
Contributor Author

Zoxc commented Apr 23, 2025

I changed the code to use the jobserver's helper thread to help deal with a race which could leak a jobserver token when exiting the process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

-Zthreads causes rustfix/jobserver to deadlock
8 participants