Skip to content

Commit 077b8d5

Browse files
committed
Abort in deadlock handler if we fail to get a query map
1 parent 01dc45c commit 077b8d5

File tree

5 files changed

+25
-12
lines changed

5 files changed

+25
-12
lines changed

compiler/rustc_interface/src/util.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,16 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
192192
// `TyCtxt` TLS reference here.
193193
let query_map = current_gcx2.access(|gcx| {
194194
tls::enter_context(&tls::ImplicitCtxt::new(gcx), || {
195-
tls::with(|tcx| QueryCtxt::new(tcx).collect_active_jobs())
195+
tls::with(|tcx| {
196+
let (query_map, complete) = QueryCtxt::new(tcx).collect_active_jobs();
197+
if !complete {
198+
eprintln!("internal compiler error: failed to get query map in deadlock handler, aborting process");
199+
// We need to abort here as we failed to resolve the deadlock,
200+
// otherwise the compiler could just hang,
201+
process::abort();
202+
}
203+
query_map
204+
})
196205
})
197206
});
198207
let query_map = FromDyn::from(query_map);
@@ -201,7 +210,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce(CurrentGcx) -> R + Send,
201210
.name("rustc query cycle handler".to_string())
202211
.spawn(move || {
203212
let on_panic = defer(|| {
204-
eprintln!("query cycle handler thread panicked, aborting process");
213+
eprintln!("internal compiler error: query cycle handler thread panicked, aborting process");
205214
// We need to abort here as we failed to resolve the deadlock,
206215
// otherwise the compiler could just hang,
207216
process::abort();

compiler/rustc_query_impl/src/plumbing.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,15 @@ impl QueryContext for QueryCtxt<'_> {
7979
tls::with_related_context(self.tcx, |icx| icx.query)
8080
}
8181

82-
fn collect_active_jobs(self) -> QueryMap {
82+
fn collect_active_jobs(self) -> (QueryMap, bool) {
8383
let mut jobs = QueryMap::default();
84+
let mut complete = true;
8485

8586
for collect in super::TRY_COLLECT_ACTIVE_JOBS.iter() {
86-
collect(self.tcx, &mut jobs);
87+
collect(self.tcx, &mut jobs, &mut complete);
8788
}
8889

89-
jobs
90+
(jobs, complete)
9091
}
9192

9293
// Interactions with on_disk_cache
@@ -139,7 +140,8 @@ impl QueryContext for QueryCtxt<'_> {
139140
}
140141

141142
fn depth_limit_error(self, job: QueryJobId) {
142-
let (info, depth) = job.find_dep_kind_root(self.collect_active_jobs());
143+
// FIXME: `collect_active_jobs` expects no locks to be held, which doesn't hold for this call.
144+
let (info, depth) = job.find_dep_kind_root(self.collect_active_jobs().0);
143145

144146
let suggested_limit = match self.recursion_limit() {
145147
Limit(0) => Limit(2),
@@ -677,7 +679,7 @@ macro_rules! define_queries {
677679
}
678680
}
679681

680-
pub(crate) fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap) {
682+
pub(crate) fn try_collect_active_jobs<'tcx>(tcx: TyCtxt<'tcx>, qmap: &mut QueryMap, complete: &mut bool) {
681683
let make_query = |tcx, key| {
682684
let kind = rustc_middle::dep_graph::dep_kinds::$name;
683685
let name = stringify!($name);
@@ -692,6 +694,7 @@ macro_rules! define_queries {
692694
// don't `unwrap()` here, just manually check for `None` and do best-effort error
693695
// reporting.
694696
if res.is_none() {
697+
*complete = false;
695698
tracing::warn!(
696699
"Failed to collect active jobs for query with name `{}`!",
697700
stringify!($name)
@@ -756,7 +759,7 @@ macro_rules! define_queries {
756759

757760
// These arrays are used for iteration and can't be indexed by `DepKind`.
758761

759-
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap)] =
762+
const TRY_COLLECT_ACTIVE_JOBS: &[for<'tcx> fn(TyCtxt<'tcx>, &mut QueryMap, &mut bool)] =
760763
&[$(query_impl::$name::try_collect_active_jobs),*];
761764

762765
const ALLOC_SELF_PROFILE_QUERY_STRINGS: &[

compiler/rustc_query_system/src/query/job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ pub fn print_query_stack<Qcx: QueryContext>(
588588
// state if it was responsible for triggering the panic.
589589
let mut count_printed = 0;
590590
let mut count_total = 0;
591-
let query_map = qcx.collect_active_jobs();
591+
let query_map = qcx.collect_active_jobs().0;
592592

593593
if let Some(ref mut file) = file {
594594
let _ = writeln!(file, "\n\nquery stack during panic:");

compiler/rustc_query_system/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub trait QueryContext: HasDepContext {
8686
/// Get the query information from the TLS context.
8787
fn current_query_job(self) -> Option<QueryJobId>;
8888

89-
fn collect_active_jobs(self) -> QueryMap;
89+
fn collect_active_jobs(self) -> (QueryMap, bool);
9090

9191
/// Load a side effect associated to the node in the previous session.
9292
fn load_side_effect(

compiler/rustc_query_system/src/query/plumbing.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,9 @@ where
250250
Q: QueryConfig<Qcx>,
251251
Qcx: QueryContext,
252252
{
253-
let error =
254-
try_execute.find_cycle_in_stack(qcx.collect_active_jobs(), &qcx.current_query_job(), span);
253+
let (query_map, complete) = qcx.collect_active_jobs();
254+
assert!(complete, "failed to collect active queries");
255+
let error = try_execute.find_cycle_in_stack(query_map, &qcx.current_query_job(), span);
255256
(mk_cycle(query, qcx, error), None)
256257
}
257258

0 commit comments

Comments
 (0)