SelectCancelable: the callback still gets data blocks after it returns false

未关闭 适合新手
#638 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
clickhouse, cpp
领域
api, database

调研方向

从 clickhouse/client.cpp 中的 Client::Impl::ReceiveData(约第 895–900 行)开始,检查它如何为每个 Data 数据包调用 OnData 和 OnDataCancelable。查看 ut/client_ut.cpp 中的 ClientCase.Cancellable(约第 866 行);当前的行数断言无法发现重复的回调。完成标准是:对于两个可取消的 select 方法,回调都在第一次返回 false 后停止,并且回归测试验证后续数据块不会传递给回调。

由索引模型根据 Issue 内容生成。

描述

bug connectivity native-protocol

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: master 14782ab
  • 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.

主要语言
C
星标
388
派生
220
平均合并
1 天 25 分钟
30 天内合并 PR
16

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

ClickHouse/clickhouse-cpp 的其他 Issue

查看 ClickHouse/clickhouse-cpp 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。