rust-lang / jobserver-rs

Apache License 2.0
69 stars 39 forks source link

feat: Make `Client::from_env` safe to call for any number of times #57

Closed NobodyXu closed 5 months ago

NobodyXu commented 1 year ago

So that it can be used in different crates without causing UB.

Motivation

opencv-rust uses jobserver::Client::from_env in its build.rs to parallelize generation of bindings, then call cc::Build::compile with features cc/parallel enabled it will also call jobserver::Client::from_env and creates a UB https://github.com/rust-lang/cc-rs/issues/844#issuecomment-1663960861

Workaround for this will require either wrapping jobserver::Client with std::mem::ManuallyDrop, or switching to my fork jobslot which supports calling Client::from_env unlimited number of times.

This PR thus port the change to upstream so that any crate using jobserver can benefit from this and can stop worrying about UB caused by calling Clieng::from_env multiple times in different crates.

weihanglo commented 5 months ago

I don't think it worth more back-and-forth. Let's merge this and improve comments later!