Skip to content

Conversation

@Zoxc
Copy link
Contributor

@Zoxc Zoxc commented Jan 22, 2026

This fixes a race where a duplicate dep node gets written to the dep graph if a node was marked as green and promoted during execution, then marked as red after execution.

This can occur when a no_hash query A depends on a query B which cannot be forced so it was not colored when starting execution of query A. During the execution of query A it will execute query B and color it green. Before A finishes another thread tries to mark A green, this time succeeding as B is now green, and A gets promoted and written to metadata. Execution of A then finishes and because it's no_hash we assume the result changed and thus we color the node again, now as red and write it to metadata again. This doesn't happen with non-no_hash queries as they will be green if all their dependencies are green.

This changes the code coloring nodes red to also use compare_exchange to deal with this race ensuring that the coloring of nodes only happens once.

r? @ghost

@rustbot rustbot added A-query-system Area: The rustc query system (https://rustc-dev-guide.rust-lang.org/query.html) S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 22, 2026
@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 22, 2026

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Jan 22, 2026
Handle race when coloring nodes concurrently as both green and red
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 22, 2026
@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 22, 2026

Looks fairly neutral locally.

BenchmarkBeforeBeforeAfterBeforeBeforeAfterBeforeBeforeAfter
TimeTime%Time%Physical MemoryPhysical Memory%Physical Memory%Committed MemoryCommitted Memory%Committed Memory%
🟣 clap:check:unchanged0.2478s0.2474s -0.15%0.2474s -0.17%99.35 MiB99.41 MiB 0.06%99.52 MiB 0.17%165.65 MiB165.71 MiB 0.03%165.74 MiB 0.05%
🟣 hyper:check:unchanged0.1119s0.1133s💔 1.30%0.1127s 0.76%63.07 MiB63.09 MiB 0.02%63.25 MiB 0.29%121.50 MiB121.51 MiB 0.01%121.59 MiB 0.08%
🟣 regex:check:unchanged0.2081s0.2058s💚 -1.12%0.2074s -0.35%81.38 MiB81.35 MiB -0.04%81.55 MiB 0.20%143.39 MiB143.39 MiB -0.01%143.55 MiB 0.11%
🟣 syn:check:unchanged0.4318s0.4327s 0.21%0.4308s -0.23%120.88 MiB121.01 MiB 0.10%121.46 MiB 0.48%187.29 MiB187.42 MiB 0.07%187.85 MiB 0.30%
Total0.9996s0.9992s -0.03%0.9983s -0.13%364.69 MiB364.85 MiB 0.05%365.78 MiB 0.30%617.83 MiB618.02 MiB 0.03%618.73 MiB 0.15%
Summary1.0000s1.0006s 0.06%1.0000s 0.00%1 byte1.00 bytes 0.04%1.00 bytes 0.29%1 byte1.00 bytes 0.03%1.00 bytes 0.13%

@cjgillot
Copy link
Contributor

cjgillot commented Jan 22, 2026

Here's what's bugging me. I'm under the impression that this PR allows a node to change colour from green to red. It should not. A node has a single colour and should never change.

So my recommendation would be:

  • fix the race by using proper atomics, yes;
  • change the logic around no_hash nodes to mark them green iff !eval_always && all parents are green.

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 23, 2026

☀️ Try build successful (CI)
Build commit: f9b6979 (f9b6979e5b37291f050e6355d9997a871b20de55, parent: 39052daf937d46373ac29778e1b8853c52c0cc25)

@rust-timer

This comment has been minimized.

@zetanumbers
Copy link
Contributor

Here's what's bugging me. I'm under the impression that this PR allows a node to change colour from green to red. It should not. A node has a single colour and should never change.

So my recommendation would be:

  • fix the race by using proper atomics, yes;

  • change the logic around no_hash nodes to mark them green iff !eval_always && all parents are green.

Wouldn't #150156 already address these concerns?

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f9b6979): comparison URL.

Overall result: ✅ improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 3
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.2% [-0.2%, -0.2%] 3

Max RSS (memory usage)

Results (primary -1.9%, secondary -5.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.9% [-1.9%, -1.9%] 1
Improvements ✅
(secondary)
-5.7% [-5.7%, -5.7%] 1
All ❌✅ (primary) -1.9% [-1.9%, -1.9%] 1

Cycles

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

Binary size

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

Bootstrap: 473.794s -> 471.369s (-0.51%)
Artifact size: 383.23 MiB -> 383.22 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 23, 2026
@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 23, 2026

I'm under the impression that this PR allows a node to change colour from green to red. It should not.

No, that's the current state of rustc. This PR prevents that.

@zetanumbers
Copy link
Contributor

I honestly feel like @Zoxc often deals with his own meaning of compiler's code and not the code's actual behavior. It comes up when I try to convince him about anything.

@teor2345
Copy link
Contributor

Would demonstrating what the code does help resolve this discussion?
(I understand it can be hard to find a reliable test/reproducer in concurrent code with race conditions.)

@Zoxc Zoxc marked this pull request as ready for review January 28, 2026 19:12
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 28, 2026
@Zoxc
Copy link
Contributor Author

Zoxc commented Jan 28, 2026

As discussed in Solving big #141540 issue and hunt for bad metadata files, I think the trait selection cache can leave nodes uncolored after execution. This means that we're not able to tell if dependencies are all green in all cases and #150156 would be insufficient to fix the race.

@rust-bors
Copy link
Contributor

rust-bors bot commented Jan 31, 2026

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

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) 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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants