Skip to content

Fix Invalid status NotActive for associated output in StrictResizeProcessor - #116478

Merged
alexey-milovidov merged 9 commits into
masterfrom
fix-strict-resize-notactive
Sep 5, 2026
Merged

alexey-milovidov merged 9 commits into
masterfrom
fix-strict-resize-notactive

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 26, 2026 •

Copy link
Copy Markdown
Member

Related: #99495

StrictResizeProcessor pairs every input port with exactly one output port. When an input finished, prepare released the paired output into waiting_outputs unconditionally. That is wrong in three ways:

  • The output may be in the NotActive state, meaning it still holds a chunk that the downstream processor has not consumed. Handing it to another input makes the next chunk arriving on that input hit Invalid status NotActive for associated output.
  • The output may have already been enqueued by the loop over updated_outputs in the same prepare call (it becomes NeedData there), so it ends up in waiting_outputs twice and is handed out to two different inputs.
  • An input that was never paired with an output has waiting_output == nullptr, and the null pointer was pushed into the queue as well.

The fix releases the output only when it is actually free, tracks queue membership in OutputPortState::is_waiting so that an output is never enqueued twice, and clears InputPortState::waiting_output as soon as the pairing is consumed by a delivered chunk — a stale pointer would otherwise release an output that had meanwhile been paired with a different input, which is exactly what the first version of this change still allowed (the regression test failed in 7 out of 100 runs in the Fast test). An output that is busy is picked up by the loop over updated_outputs as soon as the downstream processor consumes its chunk, so no output is lost.

The exception is reachable from ordinary queries, for example

SELECT sum(number) FROM (SELECT number FROM remote('127.0.0.1', view(SELECT * FROM numbers_mt(10000000) GROUP BY ALL WITH TOTALS)) GROUP BY GROUPING SETS ((number)))
SETTINGS max_threads = 49

which failed in 3-11% of the runs before this change and in 0 out of 900 runs after it. Added 05055_strict_resize_associated_output as a regression test; it reproduces the failure reliably on the unpatched binary (6 failures out of 100 runs).

An input can also finish while it is still sitting in disabled_input_ports, because an upstream OutputPort is allowed to finish even when the peer input is not needed. The stale queue entry cannot be removed from the middle of the queue, so a freed output could be paired with an already finished input: the output was stranded, and the input was counted as finished a second time, which breaks the num_finished_inputs == inputs.size() termination condition - it can terminate the processor early, dropping the data of the still live inputs, and it can also prevent it from terminating at all. Both waiting_outputs and disabled_input_ports are therefore treated as lazy queues: every consumer re-checks the current state of the entry it pops and discards the stale ones. That interleaving is not reproducible deterministically from SQL, so it is covered by a processor-level test, gtest_strict_resize_finished_disabled_input, which drives prepare through it directly.

Found by the AST fuzzer on an unrelated pull request: https://s3.amazonaws.com/clickhouse-test-reports/PRs/99495/65e4f48520cf153e261288ecd2e4a7e705d37aa4/ast_fuzzer_amd_debug_targeted/fatal.log

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix the Invalid status NotActive for associated output logical error that could happen for queries with a high max_threads, for example a GROUPING SETS aggregation over a remote table.


Workflow [PR]
Sync PR [sync-upstream/pr/116478]

…Processor`

`StrictResizeProcessor` pairs every input port with exactly one output port. When
an input finished, `prepare` released the paired output into `waiting_outputs`
unconditionally:

    if (state.status != InputStatus::Finished)
    {
        state.status = InputStatus::Finished;
        ++num_finished_inputs;

        waiting_outputs.push(state.waiting_output);
    }

That is wrong in three ways:

- The output may be in the `NotActive` state, meaning it still holds a chunk the
  downstream processor has not consumed. Handing it to another input makes the
  next chunk on that input hit
  `Invalid status NotActive for associated output`.
- The output may have already been enqueued by the loop over `updated_outputs`
  in the same `prepare` call (it becomes `NeedData` there), so it ends up in
  `waiting_outputs` twice and is handed to two different inputs.
- An input that was never paired with an output has `waiting_output == nullptr`,
  and the null pointer was pushed into the queue as well.

Release the output only when it is actually free, and track queue membership in
`OutputPortState::is_waiting` so that an output is never enqueued twice. An
output that is busy is picked up by the loop over `updated_outputs` as soon as
the downstream processor consumes its chunk, so nothing is lost.

The exception is reachable from ordinary queries, for example
`SELECT sum(number) FROM (SELECT number FROM remote('127.0.0.1', view(SELECT * FROM numbers_mt(10000000) GROUP BY ALL WITH TOTALS)) GROUP BY GROUPING SETS ((number)))`
with `max_threads = 49`, which failed in about 3-11% of the runs before this
change and in 0 out of 900 runs after it. Found by the AST fuzzer:
https://s3.amazonaws.com/clickhouse-test-reports/PRs/99495/65e4f48520cf153e261288ecd2e4a7e705d37aa4/ast_fuzzer_amd_debug_targeted/fatal.log
#99495
@clickhouse-gh

clickhouse-gh Bot commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [b914004]

Summary: ✅


AI Review

Summary

The PR fixes three related StrictResizeProcessor queue-management bugs: it stops re-enqueuing the same free output twice, clears consumed input/output pairings before they can later release the wrong output, and treats both waiting_outputs and disabled_input_ports as lazy queues so stale finished entries are discarded before reuse. I re-checked the current code against the prior review threads and read the new SQL and processor-level regression tests; I do not see any remaining contract violations in the patched logic.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit b914004.

Metric Baseline Current Δ
Lines 88.60% 88.70% +0.10%
Functions 92.10% 92.10% +0.00%
Branches 80.90% 81.00% +0.10%

Changed lines: Changed C/C++ lines covered: 79/89 (88.76%) · Uncovered code

Full report · Diff report

alexey-milovidov and others added 3 commits August 28, 2026 09:29
The previous commit released the paired output on input finish only when the
output was free (`NeedData && !is_waiting`), but `InputPortState::waiting_output`
was never cleared after the pairing had been consumed. The stale pointer
survived while the input sat in `disabled_input_ports`; if the output was
meanwhile re-enqueued and paired with a different input, and the original input
then finished, the release condition matched the live pairing of the other
input, and the output was handed out to two inputs at once. The second chunk
then hit `Invalid status NotActive for associated output`.

This is why `05045_strict_resize_associated_output` still failed in the Fast
test on the patched binary (7 out of 100 runs):
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116478&sha=50c1473b47cb591717162723d596ee81714d0147&name_0=PR&name_1=Fast%20test
#116478

Co-Authored-By: Claude Fable 5 <[email protected]>
The `arm_tidy` build failed with `cppcoreguidelines-pro-type-member-init`:
adding the `is_waiting` default member initializer made clang-tidy require
that `status` is initialized as well.

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Processors/ResizeProcessor.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Aug 28, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing b914004d2 with master f17bffbc7 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

21 translation units recompiled, 103 s compile time in total, 21 of them have a recent master baseline.

Median compile-time ratio to the baselines is ×0.93 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio.
The matched translation units cost -9.1 s (-8%) in total before that adjustment.

Job report

alexey-milovidov and others added 2 commits August 30, 2026 20:13
…utputs`, cap the test by wall-clock time

An output that finishes after it was enqueued into `waiting_outputs` left a
stale entry in the queue: the abandoned-chunk loop pushed data into the
finished port (losing the chunk) and the input-pairing loop could hand a
finished output to a new input. Treat `waiting_outputs` as a lazy queue and
skip popped entries whose status is no longer `NeedData`.

Also cap `05045_strict_resize_associated_output` by wall-clock time: under
coverage or sanitizer instrumentation one iteration takes tens of seconds and
the full 100 iterations timed out in the flaky checks, e.g.
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=116478&sha=7aea1384951e2c1521ba2125ad9369001a261346&name_0=PR
#116478

Co-Authored-By: Claude Fable 5 <[email protected]>
Comment thread src/Processors/ResizeProcessor.cpp Resolved
…rictResizeProcessor`

An upstream `OutputPort` may `finish` even when the peer input is not needed, so an input
can become finished while it is still sitting in `disabled_input_ports`, and the entry
cannot be removed from the middle of the queue. The `Enable more inputs` loop then paired a
freed output with that already finished input: the output was stranded (nothing will ever
produce data for it) and the input was counted as finished a second time, which breaks the
`num_finished_inputs == inputs.size()` termination condition - it can both terminate the
processor early, dropping the data of the still live inputs, and prevent it from terminating
at all.

`disabled_input_ports` is now treated as a lazy queue, like `waiting_outputs`: entries whose
input has already finished are discarded instead of being paired with an output.

Added `gtest_strict_resize_finished_disabled_input` which drives `prepare` through exactly
this interleaving; the interleaving is not reproducible deterministically from SQL. The test
fails on all three assertions without the fix.
…otactive; renumber the test to `05055` to avoid a collision with the tests that landed on master
@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

🟢 AI verdict: improvement — 2 query(s) improved out of 40 analysed

2 improved query(s). K_source=6 K_base=30 flagged=2/65

clickbench

🟢 1 improved · ⚠️ 1 inconclusive

Flagged queries (2 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 31 improvement 322 285 -11.5% <0.0001
⚠️ 30 not_sure 235 198 -15.7% <0.0001

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 1 improved

Flagged queries (1 of 22)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
🟢 17 improvement 101 68 -32.7% <0.0001

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

Debug info
  • StressHouse run: 3e321ee7-f9ce-4a86-a0f3-7c282c301cbb
  • MIRAI run: 0e9f7a55-84bb-4229-91d1-f8b8bc0e5365
  • PR check IDs:
    • clickbench_1232489_1788556636
    • clickbench_1232495_1788556636
    • clickbench_1232501_1788556636
    • tpch_adapted_1_official_1232513_1788556636
    • tpch_adapted_1_official_1232524_1788556636
    • tpch_adapted_1_official_1232559_1788556636

@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 4, 2026

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Sep 5, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 5, 2026
Merged via the queue into master with commit 9cac6b9 Sep 5, 2026
175 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-strict-resize-notactive branch September 5, 2026 15:00
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants