Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that a query has not completed and is not executing before starting it #108845

Merged
merged 1 commit into from
Mar 12, 2023

Conversation

Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Mar 7, 2023

This fixes a race in the query system where we only checked if the query was currently executing, but not if it was already completed, causing queries to re-execute.

r? @cjgillot

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 7, 2023
Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, the race happens because we remove executed queries from the execution state when they complete. Could you add a comment explaining how the race happens?
Should we just avoid removing the executed queries from the execution state?

Comment on lines 365 to 378
// For the parallel compiler we need to check both the query cache and query state structures
// while holding the state lock to ensure that 1) the query has not yet completed and 2) the
// query is not still executing.
if cfg!(parallel_compiler) && qcx.is_sync() {
if let Some((value, index)) = Q::query_cache(qcx).lookup(&key) {
qcx.dep_context().profiler().query_cache_hit(index.into());
return (value, Some(index));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check should happen in JobOwner::try_start which should return a JobCompleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try_start isn't generic over query values, presumably for compile time reasons. Also JobCompleted is currently used as "job completed after blocking". It might make sense to refactor try_execute_query and try_start, but I'll wait for some PRs to merge before trying that.

@Zoxc
Copy link
Contributor Author

Zoxc commented Mar 7, 2023

I added a more extended explanation.

Should we just avoid removing the executed queries from the execution state?

That sounds like an expensive way to avoid this check. It is very likely hot in cache for the common get_query case from the previous lookup.

// re-executing the query since `try_start` only checks that the query is not currently
// executing, but another thread may have already completed the query and stores it result
// in the query cache.
if cfg!(parallel_compiler) && qcx.is_sync() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The session is already accessible, no need for an extra method.

Suggested change
if cfg!(parallel_compiler) && qcx.is_sync() {
if cfg!(parallel_compiler) && qcx.dep_context().sess().threads() > 1 {

r=me with the is_sync method removed.

@cjgillot cjgillot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 7, 2023
@bors
Copy link
Contributor

bors commented Mar 7, 2023

☔ The latest upstream changes (presumably #108167) made this pull request unmergeable. Please resolve the merge conflicts.

@cjgillot
Copy link
Contributor

cjgillot commented Mar 8, 2023

@bors r+ rollup=iffy

@bors
Copy link
Contributor

bors commented Mar 8, 2023

📌 Commit 9555499 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 8, 2023
@bors
Copy link
Contributor

bors commented Mar 12, 2023

⌛ Testing commit 9555499 with merge 938afba...

@bors
Copy link
Contributor

bors commented Mar 12, 2023

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 938afba to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 12, 2023
@bors bors merged commit 938afba into rust-lang:master Mar 12, 2023
@rustbot rustbot added this to the 1.70.0 milestone Mar 12, 2023
@Zoxc Zoxc deleted the par-fix-2 branch March 12, 2023 20:26
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (938afba): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
3.4% [3.4%, 3.4%] 1
Regressions ❌
(secondary)
2.4% [1.8%, 2.8%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 3.4% [3.4%, 3.4%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants