Skip to content

Fix message loss on consumer stop and use-after-free on destruction in StorageKafka2 - #121478

Merged
alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/fix-storageKafka2-bug
Oct 20, 2026
Merged

alexey-milovidov merged 4 commits into
ClickHouse:masterfrom
JingYanchao:johnjing/fix-storageKafka2-bug

Conversation

@JingYanchao

@JingYanchao JingYanchao commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

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):

Fixed StorageKafka2 skipping messages when a consumer is stopped mid-batch, a possible use-after-free when such a table is dropped without a shutdown, and partitions outside the permanent locks being consumed only every other round.

Description

Message loss when a consumer is stopped mid-batch. KafkaConsumer2::consume
discards the messages it has already pulled from the partition queue once it
observes the stop, but the Kafka position has already moved past them. The batch
that was being assembled still had consumed_messages > 0, so after
failed_poll_attempts tripped the message sink, poll returned an OffsetGuard
and the committed offset advanced past messages that were never inserted. Nothing
rewound the consumer afterwards, because prepareToPoll keeps an existing
assignment for LOCKS_REFRESH_POLLS rounds. resetIfStopped now drops the
interrupted batch (as StorageKafka has always done in cleanUnprocessed), and
poll discards the batch and rewinds to the committed offsets instead of handing
out a guard.

Use-after-free when the storage is destroyed without a shutdown. The
librdkafka interceptors installed by KafkaConfigLoader capture this and
dereference it from rdKafkaOnThreadExit when a broker thread exits.
thread_statuses_mutex is declared after consumers, so it is destroyed first,
and a storage that was started but never shut down destroyed its consumers after
that mutex was already gone. The destructor now falls back to shutdown, the same
way StorageKafka does.

Temporary lock quota could never be held. The shrink condition in
lockTemporaryLocksLocked was quota > 0 && quota <= available, the inverse of
what its comment describes. Reaching that code guarantees available >= 1, so a
quota of 1 always matched and was pushed straight back to 0: the quota alternated
1, 0, 1, 0, ... and a consumer held a temporary lock only every other round, with
every reacquisition moving the partition to another replica and rewinding it. The
leftover partitions that permanent locks cannot cover (P mod R) were therefore
consumed about half the time.


Workflow [PR]
Sync PR [sync-upstream/pr/121478]

Version info

  • Merged into: 26.11.1.265-master (included in 26.11 and later)

@clickhouse-gh clickhouse-gh Bot added comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). external Author is not a member of the ClickHouse organization labels Sep 22, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 18, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f1c4c10]

Summary: ✅


AI Review

Summary

This PR prevents interrupted StorageKafka2 batches from advancing committed offsets, closes consumers before callback state is destroyed, and corrects temporary-lock quota adjustment. No unresolved actionable findings remain after reviewing the current code and all prior discussion.

Missing context / blind spots
  • ⚠️ Integration jobs for head f1c4c10b3cf700374c77d29c1367d6d247319481 are pending. The quota regression passed under ASan/UBSan on ce8d8639870d3f3050145e6b7c9a000c58e3b40e; completion of the current CI run would close this validation gap.
Final Verdict

✅ Approve — no code changes requested.

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Oct 18, 2026
Comment thread src/Storages/Kafka/StorageKafka2.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Commit f1c4c10b3cf700374c77d29c1367d6d247319481 was not compared: the CI logs cluster did not answer (every POST attempt failed with an exception; stopped retrying after the 1250 s read budget).

See the job log for details.

alexey-milovidov and others added 2 commits October 19, 2026 08:49
`lockTemporaryLocksLocked` returned early without logging when it reset
`tmp_locks_quota` to zero (no available partitions, or a replica without
locks). `test_temporary_lock_quota_is_retained_across_rounds` reads that
log line, so under fluctuating availability the old 1, 0, 1, 0, ... pattern
looked like consecutive non-zero rounds and the test could pass with the
original faulty condition. Now every round logs the resulting quota.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 19, 2026
Merged via the queue into ClickHouse:master with commit 629df33 Oct 20, 2026
178 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 20, 2026
vitlibar pushed a commit to ilejn/ClickHouse that referenced this pull request Oct 22, 2026
The test from ClickHouse#121478 fails in `Integration tests (arm_binary, 2/4)` with
"No consumer kept a non-zero temporary-lock quota for 4 consecutive rounds"
(6 failures on 5 unrelated PRs, none on master or amd).

The four `kafka_tmp_r*` tables share one server and one schedule, so their
slow-path rounds fire together. Each round releases its temporary locks before
`get_metadata`, which takes 150-560 ms on that runner, so every round
re-contests the three leftover partitions: the first reader grows its quota to
2, the second takes 1, the others drop to 0, and each quota cycles 1, 2, 0.
`lockTemporaryLocksLocked` behaves as specified (all leftovers are taken in
every round); only the threshold of 4 assumed non-overlapping rounds.

Lower the threshold to 2. The old inverted shrink condition reset a quota of 1
to 0 in the next round, so it can never produce two consecutive non-zero
quotas, and the test still separates the two behaviours. All six recorded CI
failures already contain a run of 2. Locally 10 separate runs pass, and with
the old condition restored in `KeeperHandlingConsumer.cpp` 3 of 3 runs fail
with every quota alternating 1, 0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added comp-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... and removed comp-message-queues Message queue integrations (Kafka, RabbitMQ, NATS table engines for stream ingestion/egress). labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-kafka Kafka table engine: consumers and producers on librdkafka, the Kafka2 engine that keeps offsets i... external Author is not a member of the ClickHouse organization 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.

3 participants