Skip to content

Commit dc7a9f6

Browse files
authored
Rollup merge of #122215 - Zoxc:cycle-detect-names, r=oli-obk
Some tweaks to the parallel query cycle handler This renames `deadlock` to `break_query_cycles`. The abort logic is moved next to the thread spawning and gives the thread a name. Fixes #122035. r? ```@oli-obk```
2 parents a5adac0 + 87ab9e8 commit dc7a9f6

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

compiler/rustc_interface/src/util.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,11 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
101101
threads: usize,
102102
f: F,
103103
) -> R {
104-
use rustc_data_structures::{jobserver, sync::FromDyn};
104+
use rustc_data_structures::{defer, jobserver, sync::FromDyn};
105105
use rustc_middle::ty::tls;
106106
use rustc_query_impl::QueryCtxt;
107-
use rustc_query_system::query::{deadlock, QueryContext};
107+
use rustc_query_system::query::{break_query_cycles, QueryContext};
108+
use std::process;
108109

109110
let registry = sync::Registry::new(std::num::NonZero::new(threads).unwrap());
110111

@@ -128,7 +129,19 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
128129
let query_map =
129130
FromDyn::from(tls::with(|tcx| QueryCtxt::new(tcx).collect_active_jobs()));
130131
let registry = rayon_core::Registry::current();
131-
thread::spawn(move || deadlock(query_map.into_inner(), &registry));
132+
thread::Builder::new()
133+
.name("rustc query cycle handler".to_string())
134+
.spawn(move || {
135+
let on_panic = defer(|| {
136+
eprintln!("query cycle handler thread panicked, aborting process");
137+
// We need to abort here as we failed to resolve the deadlock,
138+
// otherwise the compiler could just hang,
139+
process::abort();
140+
});
141+
break_query_cycles(query_map.into_inner(), &registry);
142+
on_panic.disable();
143+
})
144+
.unwrap();
132145
});
133146
if let Some(size) = get_stack_size() {
134147
builder = builder.stack_size(size);

compiler/rustc_query_system/src/query/job.rs

+7-15
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use std::num::NonZero;
1717
use {
1818
parking_lot::{Condvar, Mutex},
1919
rustc_data_structures::fx::FxHashSet,
20-
rustc_data_structures::{defer, jobserver},
20+
rustc_data_structures::jobserver,
2121
rustc_span::DUMMY_SP,
2222
std::iter,
23-
std::process,
2423
std::sync::Arc,
2524
};
2625

@@ -514,12 +513,7 @@ fn remove_cycle(
514513
/// There may be multiple cycles involved in a deadlock, so this searches
515514
/// all active queries for cycles before finally resuming all the waiters at once.
516515
#[cfg(parallel_compiler)]
517-
pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
518-
let on_panic = defer(|| {
519-
eprintln!("deadlock handler panicked, aborting process");
520-
process::abort();
521-
});
522-
516+
pub fn break_query_cycles(query_map: QueryMap, registry: &rayon_core::Registry) {
523517
let mut wakelist = Vec::new();
524518
let mut jobs: Vec<QueryJobId> = query_map.keys().cloned().collect();
525519

@@ -539,19 +533,17 @@ pub fn deadlock(query_map: QueryMap, registry: &rayon_core::Registry) {
539533
// X to Y due to Rayon waiting and a true dependency from Y to X. The algorithm here
540534
// only considers the true dependency and won't detect a cycle.
541535
if !found_cycle {
542-
if query_map.len() == 0 {
543-
panic!("deadlock detected without any query!")
544-
} else {
545-
panic!("deadlock detected! current query map:\n{:#?}", query_map);
546-
}
536+
panic!(
537+
"deadlock detected as we're unable to find a query cycle to break\n\
538+
current query map:\n{:#?}",
539+
query_map
540+
);
547541
}
548542

549543
// FIXME: Ensure this won't cause a deadlock before we return
550544
for waiter in wakelist.into_iter() {
551545
waiter.notify(registry);
552546
}
553-
554-
on_panic.disable();
555547
}
556548

557549
#[inline(never)]

compiler/rustc_query_system/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ pub use self::plumbing::*;
33

44
mod job;
55
#[cfg(parallel_compiler)]
6-
pub use self::job::deadlock;
6+
pub use self::job::break_query_cycles;
77
pub use self::job::{
88
print_query_stack, report_cycle, QueryInfo, QueryJob, QueryJobId, QueryJobInfo, QueryMap,
99
};

0 commit comments

Comments
 (0)