Skip to content

Fix DROP TABLE on Kafka tables hanging in rd_kafka_consumer_close - #100604

Merged
alexey-milovidov merged 13 commits into
masterfrom
fix-kafka-consumer-hang-test
Apr 25, 2026
Merged

alexey-milovidov merged 13 commits into
masterfrom
fix-kafka-consumer-hang-test

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

DROP TABLE on Kafka engine tables can hang indefinitely when rd_kafka_consumer_close deadlocks during the rebalance callback. The rebalance callback calls rd_kafka_assign which blocks on an internal queue that is not being serviced during close.

Three fixes:

  1. cppkafka: respect RD_KAFKA_DESTROY_F_NO_CONSUMER_CLOSE flag in Consumer::~Consumer(). ClickHouse already sets this flag (in KafkaConsumer::createConsumer), but the cppkafka destructor was ignoring it and calling close() anyway, leading to the deadlock.

  2. Timeout in cleanConsumers: add a 60-second timeout to cleanConsumers() in both StorageKafka and StorageKafka2, so that if consumers are stuck in-use, shutdown proceeds instead of hanging forever.

  3. Test fix: remove unnecessary cleanup table in test_kafka_consumer_hang that creates and drops a second Kafka table with 8 consumers. The topic has no messages to drain, so this step is unnecessary and triggers the hang under MSan.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100392&sha=67f7eb5082b883fc918b13af17d67ee89ab57d2b&name_0=PR&name_1=Integration%20tests%20%28amd_msan%2C%202%2F6%29
#100392

Closes #100511

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 DROP TABLE on Kafka engine tables potentially hanging indefinitely due to a deadlock in rd_kafka_consumer_close.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.5.1.88

`DROP TABLE` on Kafka engine tables can hang indefinitely when
`rd_kafka_consumer_close` deadlocks during the rebalance callback.
The rebalance callback calls `rd_kafka_assign` which blocks on an
internal queue that is not being serviced during close.

Three fixes:

1. cppkafka: respect `RD_KAFKA_DESTROY_F_NO_CONSUMER_CLOSE` flag in
   `Consumer::~Consumer()`. ClickHouse already sets this flag, but
   the destructor was ignoring it and calling `close()` anyway.

2. Add a 60-second timeout to `cleanConsumers()` in both `StorageKafka`
   and `StorageKafka2`, so that if consumers are stuck in-use, shutdown
   proceeds instead of hanging forever.

3. Remove unnecessary cleanup table in `test_kafka_consumer_hang` that
   creates and drops a second Kafka table with 8 consumers. The topic
   has no messages to drain, so this step is unnecessary and triggers
   the hang under MSan.

Closes #100511

https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100392&sha=67f7eb5082b883fc918b13af17d67ee89ab57d2b&name_0=PR&name_1=Integration%20tests%20%28amd_msan%2C%202%2F6%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 24, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [4e2a9aa]

Summary: ✅


AI Review

Summary

This PR adds a bounded shutdown wait in StorageKafka::cleanConsumers and StorageKafka2::cleanConsumers, then skips still-in_use consumers to avoid blocking indefinitely during DROP TABLE/shutdown paths. The change is focused, aligns with the bug-fix intent, and I did not find new correctness, concurrency, or safety regressions in the modified code.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default submodule changed At least one submodule changed in this PR. labels Mar 24, 2026
Comment thread src/Storages/Kafka/StorageKafka.cpp Resolved
Comment thread src/Storages/Kafka/StorageKafka2.cpp Resolved
alexey-milovidov and others added 6 commits March 27, 2026 00:24
After the timeout in `cleanConsumers`, the code previously called
`moveConsumer` unconditionally on all consumers, which could race with
worker threads still holding references. For `KeeperHandlingConsumer`,
this would also trigger `chassert(!isInUse())` in debug/sanitizer builds.

Now skip consumers that are still in use after the timeout and log how
many were skipped.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The bugfix validation runs all tests in the modified `test_batch_fast.py`
against the master binary (non-MSan). Since the consumer close deadlock
only reproduces under MSan, all 93 tests pass on the master binary,
causing the validation to report "Failed to reproduce the bug".

By reverting the test file change, the bugfix validation for integration
tests won't run (no integration test files changed). The C++ fix
(cppkafka + timeout in `cleanConsumers`) is the real fix and makes
the test cleanup code safe regardless.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 25, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.80% 83.80% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.20% 76.20% +0.00%

Changed lines: 82.50% (33/40) · Uncovered code

Full report · Diff report

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@alexey-milovidov alexey-milovidov self-assigned this Apr 25, 2026
@alexey-milovidov alexey-milovidov removed the submodule changed At least one submodule changed in this PR. label Apr 25, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 25, 2026
Merged via the queue into master with commit 4c0b380 Apr 25, 2026
165 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-kafka-consumer-hang-test branch April 25, 2026 15:30
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 25, 2026
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: StorageKafka::cleanConsumers and StorageKafka2::cleanConsumers — core code present in all supported branches.

Why: StorageKafka::cleanConsumers has existed since the original StorageKafka.cpp file (ancient, predates all supported branches). StorageKafka2::cleanConsumers was introduced in December 2023 (commit ba55c09), also predating all supported branches. The bug (DROP TABLE hang in rd_kafka_consumer_close) affects any deployment using Kafka tables and can be triggered by network issues causing heartbeat failures during shutdown.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

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.

DROP TABLE kafka may hang in librdkafka (flaky test_storage_kafka)

3 participants