SelectCancelable: the callback still gets data blocks after it returns false
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start in clickhouse/client.cpp at Client::Impl::ReceiveData (around lines 895–900) and inspect how it calls OnData and OnDataCancelable for each Data packet. Check ut/client_ut.cpp at ClientCase.Cancellable (around line 866), whose current row count assertion misses repeated callbacks. Done means callbacks stop after the first false for both cancelable select methods, with a regression test that verifies no later blocks reach the callback.
Written by the indexing model from the issue text.
Description
Describe the bug
After the callback of Client::SelectCancelable returns false, the client calls it again for each Data block that the server sent before it stopped the query. The server stops the query in about 100 ms (exception 735, QUERY_WAS_CANCELLED_BY_CLIENT, in system.query_log), but in that time it sends many blocks. In the run below, the callback got about 96 more blocks (6.3M rows) with LZ4 compression, and about 1,050 more blocks (69M rows) with no compression. SelectWithExternalDataCancelable has the same behavior. A caller that stops at a row limit and keeps the rows that the callback gets also receives these rows, unless it keeps its own "stopped" flag.
BeginSelect + NextBlock + Cancel() is not affected: Cancel() reads the remaining blocks and gives none of them to the caller.
Steps to reproduce
Reproduction program (C++, 32 lines)
#include <clickhouse/client.h>
#include <cstdio>
using namespace clickhouse;
// Returns false on the first non-empty block, then only counts the calls that follow.
struct StopAtFirstBlock {
bool stopped = false;
size_t calls_after_false = 0, rows_after_false = 0;
bool operator()(const Block& b) {
if (stopped) { ++calls_after_false; rows_after_false += b.GetRowCount(); return false; }
if (b.GetRowCount() == 0) return true;
stopped = true;
return false;
}
};
int main() {
const std::string query = "SELECT number FROM numbers(3000000000)";
for (auto [tag, cm] : {std::pair{"lz4", CompressionMethod::LZ4}, std::pair{"none", CompressionMethod::None}}) {
Client client(ClientOptions().SetHost("localhost").SetCompressionMethod(cm));
StopAtFirstBlock a;
client.SelectCancelable(query, [&](const Block& b) { return a(b); });
std::printf("%s SelectCancelable: callbacks after false=%zu, rows after false=%zu\n",
tag, a.calls_after_false, a.rows_after_false);
StopAtFirstBlock b;
client.SelectWithExternalDataCancelable(query, {}, [&](const Block& blk) { return b(blk); });
std::printf("%s SelectWithExternalDataCancelable: callbacks after false=%zu, rows after false=%zu\n",
tag, b.calls_after_false, b.rows_after_false);
}
}
Output:
lz4 SelectCancelable: callbacks after false=96, rows after false=6279264
lz4 SelectWithExternalDataCancelable: callbacks after false=97, rows after false=6344673
none SelectCancelable: callbacks after false=1049, rows after false=68614041
none SelectWithExternalDataCancelable: callbacks after false=1057, rows after false=69137313
Expected behavior
After the callback returns false, the client does not call it again for this query. clickhouse/client.h:271-272: "Executes a select query which can be canceled by returning false from the data handler function". The native client of the server works this way: RemoteQueryExecutor::read() gives no more data after was_cancelled is set, and tryCancel() sends Cancel only once.
Root cause
Client::Impl::ReceiveData (clickhouse/client.cpp:895-900) calls OnData and OnDataCancelable for each Data packet, and calls SendCancel() each time the callback returns false. Nothing records that the query is already cancelled. So the client also writes a Cancel packet for each in-flight block while the callback returns false. The server logs one cancel, and the next query on the same connection works, so I saw no server-side effect of the extra packets. The test ClientCase.Cancellable (ut/client_ut.cpp:866) checks only row_cnt <= total, so it does not find this.
The fix is a per-query "cancelled" flag in ReceiveData.
Polyglot AI can open a fix PR if a maintainer assigns this issue to @polyglotAI-bot.
Environment
- clickhouse-cpp:
master14782ab - ClickHouse server: 26.11.4.11
- clang 18.1.3, Linux x86_64 (Ubuntu 24.04)
Classification: impact: none · area: Connectivity (cancellation) · native: protocol
Found by automated analysis (Polyglot AI) while working on ClickHouse/clickhouse-go#2184. Verified against a live server.
- Dominant language
- C
- Stars
- 388
- Forks
- 220
- Avg merge
- 1d 25m
- Merged PRs (30d)
- 16
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ClickHouse/clickhouse-cpp
-
bug data-types
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
ClickHouse/clickhouse-cpp#645 ·
Maintainers usually reply within 1 day
-
bug queries
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
ClickHouse/clickhouse-cpp#644 ·
Maintainers usually reply within 1 day
-
bug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ClickHouse/clickhouse-cpp#643 ·
Maintainers usually reply within 1 day
-
Core SQL test harness: ParseSetAssignments does not skip comments inside SETPossibly taken @puretechteam claimed this 12 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
ClickHouse/clickhouse-cpp#626 · 1 comment ·
Maintainers usually reply within 1 day
-
Enum type parsing: duplicate ordinals (Enum8('a' = 1, 'b' = 1)) and duplicate labels (Enum8('a' = 1, 'a' = 2)) are silently accepted and lose a memberPossibly taken @SBNREX claimed this 19 days ago. Openbug data-types native-format
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
ClickHouse/clickhouse-cpp#607 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ClickHouse/clickhouse-cpp
Similar issues
-
CF_CFDP_S_SendFileData computes the file checksum from the SB buffer after CFE_SB_TransmitBufferOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
389ds/389-ds-base#8251 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100