Repository navigation
Fix Invalid status NotActive for associated output in StrictResizeProcessor - #116478
Conversation
…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
|
Workflow [PR], commit [b914004] Summary: ✅
AI ReviewSummaryThe PR fixes three related Final VerdictStatus: ✅ Approve LLVM Coverage ReportMeasured on commit b914004.
Changed lines: Changed C/C++ lines covered: 79/89 (88.76%) · Uncovered code |
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]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units21 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. |
…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]>
…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
|
📊 Cloud Performance Report 🟢 AI verdict: 2 improved query(s). K_source=6 K_base=30 flagged=2/65 clickbench🟢 1 improved · Flagged queries (2 of 43)
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)
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
|
Related: #99495
StrictResizeProcessorpairs every input port with exactly one output port. When an input finished,preparereleased the paired output intowaiting_outputsunconditionally. That is wrong in three ways:NotActivestate, 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 hitInvalid status NotActive for associated output.updated_outputsin the samepreparecall (it becomesNeedDatathere), so it ends up inwaiting_outputstwice and is handed out to two different inputs.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_waitingso that an output is never enqueued twice, and clearsInputPortState::waiting_outputas 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 overupdated_outputsas soon as the downstream processor consumes its chunk, so no output is lost.The exception is reachable from ordinary queries, for example
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_outputas 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 upstreamOutputPortis allowed tofinisheven 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 thenum_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. Bothwaiting_outputsanddisabled_input_portsare 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 drivespreparethrough 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):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix the
Invalid status NotActive for associated outputlogical error that could happen for queries with a highmax_threads, for example aGROUPING SETSaggregation over a remote table.Workflow [PR]
Sync PR [sync-upstream/pr/116478]