Job server strongly conflicts with multithreaded linkers #14651
Labels
A-jobserver
Area: jobserver, concurrency, parallelism
A-linkage
Area: linker issues, dylib, cdylib, shared libraries, so
C-feature-request
Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted`
Problem
Normally, cargo is only ever indirectly running one linker at a time, at the very end of compilation, and so, multithreaded linkers don't really matter. However, for
cargo test
, these linkers might be run in parallel, and cargo makes the assumption that all of these linkers are single-threaded, which is not the case formold
.This can result in a potentially quadratic number of threads being run at the same time, which can crash systems.
Proposed Solution
One simple solution for the specific case of
mold
as a linker is to pass theMOLD_JOBS=1
environment variable torustc
, which will then be passed to the linker, which will then tellmold
to ensure that the number of instances of the linker running at the same time is only ever one. This still means that the number of concurrent threads can be double what the CPU supports, since there can still be concurrentrustc
jobs at the same time as the linker, but it won't end up being quadratic. However, this makes more sense as a mitigation done by users instead of something done byCargo
, since it affects the environment for more users than it needs to affect.A potentially better solution is to delay all linking tasks until the very end of compilation, and have the option to run them serially instead of in parallel. This could be detected automatically via
-Clink-args=-fuse-ld=mold
, but more it would probably be fine just as a configurable flag initially. It's not really possible to separate the linking fromrustc
at the moment, but at least limiting therustc
calls that are expected to call a linker until the end seems reasonable.Notes
At some point, it might be desirable to have the linker fully controlled by cargo, so that there isn't a problem not running them in parallel at the end of compilation. However, this is substantially larger of a change than the one proposed, so, it's left out for now.
The text was updated successfully, but these errors were encountered: