Skip to content

Commit bb1742b

Browse files
committed
Explicitly drop helper thread
1 parent 6b05f82 commit bb1742b

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

‎compiler/rustc_data_structures/src/jobserver.rs‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub struct Proxy {
9393
/// The wrapped jobserver client.
9494
client: Client,
9595
/// Helper thread associated with the wrapped client.
96-
helper: OnceLock<HelperThread>,
96+
helper: Mutex<Option<HelperThread>>,
9797
/// The proxy's own data.
9898
data: Mutex<ProxyData>,
9999
/// Threads which are currently waiting for a token will wait on this condvar.
@@ -107,28 +107,28 @@ impl Proxy {
107107
// Assume that the main thread is actively doing work when it creates the proxy.
108108
data: Mutex::new(ProxyData { used: 1, pending: 0 }),
109109
wake_pending: Condvar::new(),
110-
helper: OnceLock::new(),
110+
helper: Default::default(),
111111
});
112-
// Use weak reference as if we will clone `Arc` then `Proxy` will never be
113-
// dropped as helper thread will have a strong reference to it, this may
114-
// result in not all tokens returned to the job server, see #165506.
115-
let weak_proxy = Arc::downgrade(&proxy);
112+
113+
// The helper thread is dropped explicitly (see `drop_helper_thread` below).
114+
// If we make this a weak reference then we still can encounter a situation
115+
// when token is not returned, as the last strong reference may be held by this
116+
// thread meaning it may not be dropped before rustc exits (see #165854).
117+
let proxy_ = Arc::clone(&proxy);
116118
let helper = proxy
117119
.client
118120
.clone()
119121
.into_helper_thread(move |token| {
120122
// Reminder: this callback runs when the helper acquires a token.
121-
if let Ok(token) = token
122-
&& let Some(proxy) = weak_proxy.upgrade()
123-
{
124-
let mut data = proxy.data.lock();
123+
if let Ok(token) = token {
124+
let mut data = proxy_.data.lock();
125125
if data.pending > 0 {
126126
// The token is still needed, give it to one of the waiting threads.
127127
token.drop_without_releasing();
128128
assert!(data.used > 0);
129129
data.used += 1;
130130
data.pending -= 1;
131-
proxy.wake_pending.notify_one();
131+
proxy_.wake_pending.notify_one();
132132
} else {
133133
// The token is no longer needed, release it by dropping.
134134
drop(data);
@@ -137,7 +137,7 @@ impl Proxy {
137137
}
138138
})
139139
.expect("failed to spawn helper thread");
140-
proxy.helper.set(helper).unwrap();
140+
*proxy.helper.lock() = Some(helper);
141141
proxy
142142
}
143143

@@ -154,7 +154,7 @@ impl Proxy {
154154
} else {
155155
// Request a token from the helper thread, this is a non-blocking operation.
156156
// Then wait until this or some other request succeeds.
157-
self.helper.get().unwrap().request_token();
157+
self.helper.lock().as_ref().unwrap().request_token();
158158
data.pending += 1;
159159
self.wake_pending.wait(&mut data);
160160
}
@@ -179,4 +179,11 @@ impl Proxy {
179179
}
180180
}
181181
}
182+
183+
/// Explicitly drops helper thread making sure that it is dropped before
184+
/// `rustc` exits and thus all tokens are returned to the job server (see #165854).
185+
pub fn drop_helper_thread(&self) {
186+
let helper_thread = self.helper.lock().take();
187+
drop(helper_thread);
188+
}
182189
}

‎compiler/rustc_interface/src/util.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
270270
let current_gcx2 = current_gcx.clone();
271271

272272
let proxy = Proxy::new();
273+
274+
// Explicitly drop and join helper thread before rustc exits,
275+
// so all job server tokens are returned.
276+
let _drop_helper_thread = defer({
277+
let proxy = Arc::clone(&proxy);
278+
move || proxy.drop_helper_thread()
279+
});
280+
273281
let proxy_ = Arc::clone(&proxy);
274282

275283
let builder = rustc_thread_pool::ThreadPoolBuilder::new()

0 commit comments

Comments
 (0)