Repository navigation
Fix flaky distributed query "Unexpected packet from server (expected TablesStatusResponse, got ProfileInfo)" - #108854
Conversation
…TablesStatusResponse, got ProfileInfo)"
Distributed queries over hedged connections intermittently fail during connection
establishment with:
Code: 102. UNEXPECTED_PACKET_FROM_SERVER:
Unexpected packet from server 127.0.0.2:9000 (expected TablesStatusResponse, got ProfileInfo)
thrown from `Connection::getTablesStatus` via `ConnectionEstablisher::run`. A pooled
connection (initiator to replica) is reused while it still holds undelivered packets
(`ProfileInfo`/`EndOfStream`) that a previous, cancelled distributed query left behind, so
the table-status request reads a stale packet instead of the expected response.
This is a recurrence of the issue fixed in #93029 (originally reported as #93018). It came
back after #95466 added `|| was_cancelled` to the early-return guard in
`RemoteQueryExecutor::finish`, which makes `finish` skip the drain after a cancellation.
Two changes:
1. `RemoteQueryExecutor::finish`: when the query was already cancelled and its connections may
still hold undelivered packets, disconnect them instead of leaving them to be returned to
the pool in an out-of-sync state. We do not drain them here (the read side may already be
torn down, which previously crashed - see #95466), but they must not be reused. This mirrors
the cleanup the destructor already does, performed eagerly so it cannot be skipped if
`finished` later becomes true through another path.
2. `ConnectionEstablisher::run`: treat `UNEXPECTED_PACKET_FROM_SERVER` like the other "the
pooled connection turned out to be unusable" errors (NETWORK_ERROR, ATTEMPT_TO_READ_AFTER_EOF,
...): disconnect the entry and report a soft failure so connection establishment retries on a
freshly established connection, instead of failing the whole distributed query. This is the
documented design (the optimistic path does not ping a pooled connection before use and relies
on retrying), it just did not cover a connection left out of sync.
Adds a fail point (`unexpected_packet_in_table_status_response`) and a deterministic test
(`04489_distributed_table_status_desync_recovery`) that injects the desync and verifies the
query recovers.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=799ffffcce85c605e19d0116ccad68955ebd784a&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
Related: #93018
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [276e727] Summary: ✅
AI ReviewSummaryThis PR prevents cancelled distributed queries from returning possibly out-of-sync remote connections to the pool, and makes Final Verdict✅ Approve. No new findings. |
…04489
The injected `UNEXPECTED_PACKET_FROM_SERVER` makes `ConnectionPoolWithFailover`
log a `Connection failed at try №1` warning before retrying on a freshly
established connection. That retry is exactly the recovery this test verifies,
so the warning is expected, but the functional test harness forwards server
warnings to the client's stderr (default `send_logs_level = 'warning'`) and
flags any stderr output as a failure ("having stderror").
Drop the session log level to `error` for the test, mirroring
`02769_parallel_replicas_unavailable_shards.sql`, which suppresses the same
connection-failover warning. Errors still surface, so a genuine failure on the
recovery path would still be caught.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108854&sha=63855d0ecfeb7fc381daec418f497aeffff6ceb8&name_0=PR&name_1=Fast%20test
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…ed-tablesstatus-desync
|
Pushed two commits:
The |
|
Your fix #1 (eager disconnect so it cannot be skipped if This is not a slow recurrence of #95466. CIDB shows the if (!sent_query)
finished = true;Setting Local A/B on a debug build (16-way concurrent
So |
Algunenano
left a comment
There was a problem hiding this comment.
Thanks for investigating and fixing this. It seems addressing a Darwin bug in #108630 exposed this latent one
|
Continue-PR triage (2026-06-30) @groeneai — thanks for the bisection. Confirmed your conclusion and updated the PR description: the latent precondition is #95466 (skippable drain after cancellation), but the actual trigger of this week's master-wide spike is #108630 / Status: AI Review ✅ Approve, @Algunenano approved, 0 unresolved review threads. Merge-base is today ( CI reds are all unrelated, known issues — none touch the connection/network code this PR changes:
No new commits required; only the description was corrected. Ready to merge once the unrelated stress jobs are re-run. |
…ed-tablesstatus-desync
|
Continue-PR re-check (2026-07-01) — re-merged Pushed a merge of
The merge was conflict-free — all of these are in different functions from this PR's fix #1 in
This validates the fix against current State otherwise unchanged and ready: AI Review ✅ Approve, @Algunenano approved, 0 unresolved review threads, Note: |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 29/29 (100.00%) · Uncovered code |
|
Continue-PR re-check (2026-07-01) — re-ran the fuzzer flake The only red on the current head ( CIDB confirms it is a pre-existing, master-wide fuzzer flake: STID Re-ran the failed job (run |
…tics-race-condition Resolve one conflict in `src/QueryPipeline/RemoteQueryExecutor.cpp` (`finish`). Master gained an `else if (was_cancelled && !finished && connections)` branch that eagerly `connections->disconnect()`s an out-of-sync, already-cancelled connection so it is not returned to the pool with undelivered packets (fixes the `Unexpected packet from server (expected TablesStatusResponse, got ProfileInfo)` desync of #93018 / #95466, from the master-side change in #108854). This PR independently reworked `finish` into a nested guard scope that releases `was_cancelled_mutex` before the (potentially long) blocking drain loop, uses a gated `SCOPE_EXIT`, and drains every replica's trailing `Progress` packets (the `rows_read = 0` fix). The two changes are orthogonal, so this is a take-both resolution: master's disconnect branch is folded into this PR's early-return block (which is likewise taken when `was_cancelled` is true and therefore also skips draining), while the PR's guard-scope / gated-`SCOPE_EXIT` / drain-all-replicas structure is kept. The rest of the file (and `src/Client/ClientBase.cpp`) auto-merged cleanly. #96978 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The `2bffb6e5968` master merge folded master's eager-`disconnect` branch (the connection-pool desync fix of #93018 / #95466, master-side #108854) into this PR's reworked `finish`, whose drain loop runs with `was_cancelled_mutex` released so a concurrent hard cancel can preempt it. On `master` the whole `finish` ran under `was_cancelled_mutex`, so the eager-`disconnect` branch and the drain loop were mutually exclusive. After this PR releases the mutex before draining, two `finish` calls can race: the draining call sets `was_cancelled` via `tryCancel`, releases the mutex and blocks in `receivePacket`; a concurrent `finish` then observes `was_cancelled`, enters the eager-`disconnect` branch and calls `connections->disconnect()`, tearing down the connections under the active read. The draining call then throws `No more packets are available. (LOGICAL_ERROR)`. This deterministically broke six stateless tests on `2bffb6e5968`: `00059_shard_global_in`, `04066_distributed_offset`, `00163_shard_join_with_empty_table`, `01451_dist_logs`, `01787_map_remote`, and the PR's own `03918_statistics_in_formats_parallel_replicas`. Fix: a `draining` flag (guarded by `was_cancelled_mutex`) marks that a `finish` owns the drain loop. The eager-`disconnect` branch skips the disconnect while a drain is in progress and lets the draining call (or the destructor) clean up instead, restoring the mutual exclusion that `master` relied on without holding the mutex across the blocking drain. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=96978&sha=2bffb6e59680a45fba6cf1f92cab3cbfdcb3a3dd&name_0=PR&name_1=Fast%20test PR: #96978 Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…nt64-overflow Pull in the fix for the flaky '03164_parallel_replicas_range_filter_min_max' distributed-query test (PR ClickHouse#108854, 'Unexpected packet from server') to refresh CI.
Pick up merged PR ClickHouse#108854 (fixes the flaky distributed 'expected TablesStatusResponse, got ProfileInfo' protocol desync) so CI re-runs against a base that includes the fix.
…buted-tablesstatus-desync Fix flaky distributed query "Unexpected packet from server (expected TablesStatusResponse, got ProfileInfo)"
…nt64-overflow Pull in the fix for the flaky '03164_parallel_replicas_range_filter_min_max' distributed-query test (PR ClickHouse#108854, 'Unexpected packet from server') to refresh CI.
Caused by: #108630
Related: #93018
Distributed queries over hedged connections intermittently fail during connection establishment with:
thrown from
Connection::getTablesStatusviaConnectionEstablisher::run. A pooled connection (initiator → replica) is reused while it still holds undelivered packets (ProfileInfo/EndOfStream) that a previous, cancelled distributed query left behind, so the table-status request reads a stale packet instead of the expected response.This is a recurrence of the issue fixed in #93029 (reported as #93018). The latent precondition was introduced by #95466, which added
|| was_cancelledto the early-return guard inRemoteQueryExecutor::finish: this makesfinishskip the drain after a cancellation, so a cancelled query can leave its connection out of sync in the pool. On its own, however, that guard predates the recent failures and did not reproduce the spike.The actual trigger of the recent master-wide spike is #108630 (commit
b681776, "RemoteQueryExecutor: mark finished when draining a never-sent query"), merged on 2026-06-29: it setsfinished = trueearly when!sent_query. Under load this flipsisQueryPendingto false on a concurrent path, which lets the out-of-sync connection be returned to the pool. CIDB shows 0 hits of the(expected TablesStatusResponse, got ProfileInfo)desync in the prior 29 days, then a sharp spike starting2026-06-29 17:54Z— ~33 min after #108630 merged — and an A/B bisection on a debug build confirmed the trigger (withb681776: 12 desync hits / 150s; reverting only that hunk: 0). Thanks to @groeneai for the bisection. The regression source for backport targeting is therefore #108630.Changes
RemoteQueryExecutor::finish: when the query was already cancelled and its connections may still hold undelivered packets, disconnect them instead of letting them be returned to the pool in an out-of-sync state. We do not drain them here — the read side may already be torn down, which previously crashed (Avoid possible crash for distributed queries in case of cancellation #95466) — but they must not be reused. This mirrors the cleanup the destructor already does, performed eagerly so it cannot be skipped iffinishedlater becomes true through another path.ConnectionEstablisher::run: treatUNEXPECTED_PACKET_FROM_SERVERlike the other "the pooled connection turned out to be unusable" errors (NETWORK_ERROR,ATTEMPT_TO_READ_AFTER_EOF, …): disconnect the entry and report a soft failure so connection establishment retries on a freshly established connection, instead of failing the whole distributed query. This matches the documented design — the optimistic path does not ping a pooled connection before use and relies on retrying — it just did not cover a connection left out of sync.Adds a fail point (
unexpected_packet_in_table_status_response) and a deterministic test (04489_distributed_table_status_desync_recovery) that injects the desync and verifies the query recovers.CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=799ffffcce85c605e19d0116ccad68955ebd784a&name_0=MasterCI&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix distributed queries occasionally failing with
UNEXPECTED_PACKET_FROM_SERVER("expected TablesStatusResponse, got ProfileInfo") when a connection that a previous cancelled query left out of sync was reused from the pool.Documentation entry for user-facing changes
🤖 Generated with Claude Code
Version info
26.7.1.351(included in26.7and later)