Skip to content

Fix Pipeline stuck deadlock in PingPongProcessor with full_sorting_merge join - #98454

Merged
alexey-milovidov merged 7 commits into
masterfrom
fix-pipeline-stuck-pingpong-deadlock
Mar 3, 2026
Merged

alexey-milovidov merged 7 commits into
masterfrom
fix-pipeline-stuck-pingpong-deadlock

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Fix a "Pipeline stuck" LOGICAL_ERROR exception caused by a circular dependency deadlock in PingPongProcessor (ReadHeadBalancedProcessor) when used with the FilterBySetOnTheFly optimization in full_sorting_merge joins.

The deadlock

The ReadHeadBalancedProcessor uses a ping-pong protocol between two instances (Order::First for the left side, Order::Second for the right side) to synchronize how much data each side reads before the filtering sets are exchanged. The protocol:

  1. Second processes data, counting rows via consume
  2. When enough rows are consumed, Second sends a ping to First
  3. First receives the ping and starts processing
  4. First sends a ping back after consuming enough rows

The bug: Order::First blocked in prepare (line 153) waiting for the ping before ever calling processRegularPorts. This meant the left-side sources never started. When the downstream MergeJoinTransform (a Y-shaped processor that needs data from both sides) couldn't get left-side data, it stopped consuming from the right side. This caused backpressure that prevented Order::Second from pushing data through FilterBySetOnTheFlyTransform, so consume was never called, ready_to_send stayed false, and the ping was never sent.

n10 (First, left)  ──waits for ping──→  n35 (Second, right)
        ↑                                       │
   needs left data                        needs to push downstream
        │                                       │
n18 (MergeJoinTransform)  ←──blocked──  n34 (FilterBySetOnTheFly)

This was triggered by the AST fuzzer with join_algorithm = 'full_sorting_merge', max_rows_in_set_to_optimize_join = 100000, and max_block_size = 10 — the small block size meant only ~20-30 rows could buffer between Second and MergeJoinTransform, far fewer than the 100000 needed to trigger the ping.

The fix

Remove the blocking return Status::NeedData before processRegularPorts in the pre-processing ping check. Instead, attempt receivePing non-blockingly and fall through to process regular ports. The finish-time ping exchange (required for proper cleanup synchronization) is preserved.

This is safe because:

  • The ping-pong protocol only controls flow (balanced reading), not correctness
  • FilterBySetOnTheFlyTransform already bypasses rows when the set is still in Creating state
  • Order::First still won't try to send its ping until it receives Second's (gated by is_received)
  • Both the new regression test and the existing 02383_join_and_filtering_set test pass

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98355&sha=5183838a81ab41b4f3434e73aaca3fe02f3d3c59&name_0=PR&name_1=AST%20fuzzer%20%28amd_ubsan%29
#98355

Closes #57728

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 "Pipeline stuck" exception in full_sorting_merge joins caused by a deadlock in PingPongProcessor when the FilterBySetOnTheFly optimization created a circular dependency with MergeJoinTransform.

🤖 Generated with Claude Code

Version info

  • Merged into: 26.3.1.341

…ng_merge` join

The `ReadHeadBalancedProcessor` (`Order::First`) blocked in `prepare`
waiting for a ping from `Order::Second` before processing any regular
data. When the downstream `MergeJoinTransform` needed data from both
sides, the right side (`Second`) couldn't consume enough rows to send
the ping because the join was waiting for left-side data — a circular
dependency that deadlocked the pipeline.

The fix: don't block `Order::First` before `processRegularPorts`.
Instead, try to receive the ping non-blockingly and let regular ports
process data. The finish-time ping exchange (required for cleanup) is
preserved. This is safe because `FilterBySetOnTheFlyTransform` already
bypasses rows when the set is still in `Creating` state.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98355&sha=5183838a81ab41b4f3434e73aaca3fe02f3d3c59&name_0=PR&name_1=AST%20fuzzer%20%28amd_ubsan%29

Closes #57728

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bfe64b2]

Summary: ✅

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Mar 2, 2026
alexey-milovidov and others added 4 commits March 2, 2026 19:04
When `start_server` retries after the first `clickhouse start` attempt
fails to respond, `clickhouse start` sees the existing PID file and
reports "already running" — making all retries no-ops. This happens when
the server started but failed to bind some ports (e.g. 9181, 9009) still
held by a previous instance that hadn't fully terminated yet, especially
under sanitizer builds which are slower.

The fix: on retry attempts, force-stop any non-responsive server before
starting a fresh one. On the first attempt nothing changes. On retries
we've already waited 20 seconds — if `SELECT 1` still fails, the server
won't recover and needs a restart with ports now freed.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98454&sha=48200cc58dec962ea1624acc23da4372e73fa52c&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
After `safeExit` (forceful shutdown due to stuck client connections),
the kernel may still be cleaning up sockets, especially under sanitizer
builds where `SYS_exit_group` is used. The `safeExit` path skips the
normal `SCOPE_EXIT_SAFE` handler that closes interserver (9009) and
Keeper (9181) listening sockets, leaving them to kernel cleanup which
can race with the next server start.

Add `wait_server_port_free` to check port availability before starting,
so `start_server` waits for ports 9000, 9009, and 9181 to be released.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=98454&sha=48200cc58dec962ea1624acc23da4372e73fa52c&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@nihalzp

nihalzp commented Mar 2, 2026

Copy link
Copy Markdown
Member

The test query does not reproduce the bug.

Let's use this https://fiddle.clickhouse.com/f1022c9f-a474-4ca1-a55e-9f69fd851cb1 which reproduces.

DROP TABLE IF EXISTS t;
DROP TABLE IF EXISTS t2;

CREATE TABLE t (`c` UInt64) ENGINE = MergeTree ORDER BY c;
INSERT INTO t SELECT * FROM numbers(1000000);

CREATE TABLE t2 (`c` UInt64) ENGINE = MergeTree ORDER BY c;
INSERT INTO t2 SELECT * FROM numbers(1000000);

SELECT count()
FROM (SELECT * FROM t) AS t1,
     (SELECT * FROM t2 ORDER BY c ASC LIMIT 500000) AS t2s
WHERE t1.c = t2s.c
SETTINGS
    max_rows_in_set_to_optimize_join = 100000000,
    join_algorithm = 'full_sorting_merge',
    query_plan_join_swap_table = 0,
    max_threads = 2;

The previous test with 1000 rows and `max_block_size = 10` did not
trigger the circular dependency between `PingPongProcessor` and
`MergeJoinTransform`. Use the reproducer from
#98454 (comment)
which reliably triggers the deadlock with larger data and appropriate
settings.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Thanks, now the test is good.

@alexey-milovidov
alexey-milovidov merged commit a18d8ac into master Mar 3, 2026
145 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-pipeline-stuck-pingpong-deadlock branch March 3, 2026 17:41
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Pipeline suck

3 participants