Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions compiler/rustc_data_structures/src/jobserver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct Proxy {
/// The wrapped jobserver client.
client: Client,
/// Helper thread associated with the wrapped client.
helper: OnceLock<HelperThread>,
helper: Mutex<Option<HelperThread>>,
/// The proxy's own data.
data: Mutex<ProxyData>,
/// Threads which are currently waiting for a token will wait on this condvar.
Expand All @@ -107,28 +107,28 @@ impl Proxy {
// Assume that the main thread is actively doing work when it creates the proxy.
data: Mutex::new(ProxyData { used: 1, pending: 0 }),
wake_pending: Condvar::new(),
helper: OnceLock::new(),
helper: Default::default(),
});
// Use weak reference as if we will clone `Arc` then `Proxy` will never be
// dropped as helper thread will have a strong reference to it, this may
// result in not all tokens returned to the job server, see #165506.
let weak_proxy = Arc::downgrade(&proxy);

// The helper thread is dropped explicitly (see `drop_helper_thread` below).
// If we make this a weak reference then we still can encounter a situation
// when token is not returned, as the last strong reference may be held by this
// thread meaning it may not be dropped before rustc exits (see #165854).
let proxy_ = Arc::clone(&proxy);
let helper = proxy
.client
.clone()
.into_helper_thread(move |token| {
// Reminder: this callback runs when the helper acquires a token.
if let Ok(token) = token
&& let Some(proxy) = weak_proxy.upgrade()
{
let mut data = proxy.data.lock();
if let Ok(token) = token {
let mut data = proxy_.data.lock();
if data.pending > 0 {
// The token is still needed, give it to one of the waiting threads.
token.drop_without_releasing();
assert!(data.used > 0);
data.used += 1;
data.pending -= 1;
proxy.wake_pending.notify_one();
proxy_.wake_pending.notify_one();
} else {
// The token is no longer needed, release it by dropping.
drop(data);
Expand All @@ -137,7 +137,7 @@ impl Proxy {
}
})
.expect("failed to spawn helper thread");
proxy.helper.set(helper).unwrap();
*proxy.helper.lock() = Some(helper);
proxy
}

Expand All @@ -154,7 +154,7 @@ impl Proxy {
} else {
// Request a token from the helper thread, this is a non-blocking operation.
// Then wait until this or some other request succeeds.
self.helper.get().unwrap().request_token();
self.helper.lock().as_ref().unwrap().request_token();
data.pending += 1;
self.wake_pending.wait(&mut data);
}
Expand All @@ -179,4 +179,11 @@ impl Proxy {
}
}
}

/// Explicitly drops helper thread making sure that it is dropped before
/// `rustc` exits and thus all tokens are returned to the job server (see #165854).
pub fn drop_helper_thread(&self) {
let helper_thread = self.helper.lock().take();
drop(helper_thread);
}
}
8 changes: 8 additions & 0 deletions compiler/rustc_interface/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
let current_gcx2 = current_gcx.clone();

let proxy = Proxy::new();

// Explicitly drop and join helper thread before rustc exits,
// so all job server tokens are returned.
let _drop_helper_thread = defer({
let proxy = Arc::clone(&proxy);
move || proxy.drop_helper_thread()
});

let proxy_ = Arc::clone(&proxy);

let builder = rustc_thread_pool::ThreadPoolBuilder::new()
Expand Down
Loading