Skip to content

Catch exceptions from assign/unassign in handle_rebalance - #7

Merged
alexey-milovidov merged 2 commits into
ClickHouse/release-0.4.1from
fix-rebalance-assign-timeout
Apr 28, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse/release-0.4.1from
fix-rebalance-assign-timeout

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Summary

handle_rebalance is called from rebalance_proxy, a C callback registered with librdkafka. The assign() and unassign() calls inside it can now throw HandleException when rd_kafka_assign() times out (see ClickHouse/librdkafka#18) instead of blocking forever. Throwing from a C callback is undefined behavior.

Wrap assign() and unassign() in try/catch and route timeout errors through the existing rebalance_error_callback_, which the application already handles.

handle_rebalance is called from rebalance_proxy, a C callback
registered with librdkafka.  The assign() and unassign() calls inside
it can now throw HandleException when rd_kafka_assign() times out
(instead of blocking forever).  Throwing from a C callback is
undefined behavior.

Wrap assign() and unassign() in try/catch and route timeout errors
through the existing rebalance_error_callback_, which the application
already handles.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Mar 30, 2026
…gn wait

When a Kafka consumer experiences a heartbeat error followed by a
rebalance (e.g. after broker becomes temporarily unreachable),
DROP TABLE can hang indefinitely.  The root cause is inside
librdkafka: rd_kafka_assign(), called from the rebalance callback
during poll_batch(), sends an op to the consumer-group thread and
waits with RD_POLL_INFINITE.  If the cgrp thread is blocked waiting
for the coordinator, poll_batch() never returns, making pipeline
cancellation impossible regardless of the cancel callback.

This updates both contrib/librdkafka and contrib/cppkafka:
- librdkafka: replace RD_POLL_INFINITE with 30s timeout in
  rd_kafka_assign0 (ClickHouse/librdkafka#18)
- cppkafka: catch HandleException from assign/unassign in
  handle_rebalance to prevent throwing from a C callback
  (ClickHouse/cppkafka#7)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
`handle_rebalance` is called from `rebalance_proxy`, a C callback.
`assign`/`unassign` can throw not only `HandleException` (e.g. on
`rd_kafka_assign` timeout) but also other exceptions such as allocation
failures while converting `TopicPartitionList`. Letting any exception
escape a C callback is undefined behavior.

Catch `std::exception` and `...` in addition to `HandleException`,
routing errors through the existing `rebalance_error_callback_` with
`RD_KAFKA_RESP_ERR__FAIL` as a generic error code.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov alexey-milovidov self-assigned this Apr 28, 2026
@alexey-milovidov
alexey-milovidov merged commit 107d5b1 into ClickHouse/release-0.4.1 Apr 28, 2026
alexey-milovidov added a commit to ClickHouse/ClickHouse that referenced this pull request Apr 29, 2026
Bump `contrib/cppkafka` and `contrib/librdkafka` to the heads of their
ClickHouse release branches now that the dependent fork PRs are merged:

- cppkafka: bump to merge of ClickHouse/cppkafka#7
  (Catch exceptions from `assign`/`unassign` in `handle_rebalance`).
- librdkafka: bump to merge of ClickHouse/librdkafka#18
  (Use bounded timeout in `rd_kafka_assign0` instead of `RD_POLL_INFINITE`)
  and ClickHouse/librdkafka#17
  (Fix use-after-free in consumer lag timer).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…gn wait

When a Kafka consumer experiences a heartbeat error followed by a
rebalance (e.g. after broker becomes temporarily unreachable),
DROP TABLE can hang indefinitely.  The root cause is inside
librdkafka: rd_kafka_assign(), called from the rebalance callback
during poll_batch(), sends an op to the consumer-group thread and
waits with RD_POLL_INFINITE.  If the cgrp thread is blocked waiting
for the coordinator, poll_batch() never returns, making pipeline
cancellation impossible regardless of the cancel callback.

This updates both contrib/librdkafka and contrib/cppkafka:
- librdkafka: replace RD_POLL_INFINITE with 30s timeout in
  rd_kafka_assign0 (ClickHouse/librdkafka#18)
- cppkafka: catch HandleException from assign/unassign in
  handle_rebalance to prevent throwing from a C callback
  (ClickHouse/cppkafka#7)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Bump `contrib/cppkafka` and `contrib/librdkafka` to the heads of their
ClickHouse release branches now that the dependent fork PRs are merged:

- cppkafka: bump to merge of ClickHouse/cppkafka#7
  (Catch exceptions from `assign`/`unassign` in `handle_rebalance`).
- librdkafka: bump to merge of ClickHouse/librdkafka#18
  (Use bounded timeout in `rd_kafka_assign0` instead of `RD_POLL_INFINITE`)
  and ClickHouse/librdkafka#17
  (Fix use-after-free in consumer lag timer).

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant