Skip to content

Commit 438355c

Browse files
Rollup merge of #165854 - aerooneqq:explicitly-drop-helper-thread, r=petrochenkov
Explicitly drop job server helper thread An adaptation of an LLM patch that was generated in https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Interaction.20of.20Cargo.20jobserver.20tests.20and.20parallel.20frontend/with/639482582. The original patch consisted from two fixes: explicit drop of a helper thread and additional logic for job server token request counting. This PR implements only the explicit drop part as I don't see how additional requests counting logic should help the issue. This reworks #165676, as the [rationale](https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/Interaction.20of.20Cargo.20jobserver.20tests.20and.20parallel.20frontend/near/639152114) behind explicit drop of a helper thread seems reasonable for me: > From my rough understanding, while helper is handling a token, its upgrade() is often the last strong ref I guess. So, when run_in_thread_pool_with_globals returns and rustc exits, the token may still be in helper's hands. This fix didn't fail after several launches in #165506 and locally. r? @petrochenkov
2 parents 167989d + bb1742b commit 438355c

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)