Skip to content

Cherry pick #103890 to 26.4: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2) - #105082

Merged
robot-ch-test-poll4 merged 4 commits into
backport/26.4/103890from
cherrypick/26.4/103890
May 15, 2026
Merged

robot-ch-test-poll4 merged 4 commits into
backport/26.4/103890from
cherrypick/26.4/103890

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #103890

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 4 commits May 7, 2026 01:47
`LockedTopicPartitionInfo::lock` (held in `permanent_locks` and `tmp_locks`)
is a `zkutil::EphemeralNodeHolder`, which stores a **raw** `ZooKeeper &`
reference captured at construction. Its destructor calls
`zookeeper.expired()` and possibly `zookeeper.tryRemove()`.

`KeeperHandlingConsumer::setKeeper()` assigned the new `keeper` shared_ptr
*before* clearing the lock maps. If the consumer was the last shared owner
of the previous `ZooKeeper` session, that assignment would synchronously
destroy the old session, and the subsequent `permanent_locks.clear()` /
`tmp_locks.clear()` would then access a dangling reference -> SIGSEGV.

This is the same hazard already documented and handled in
`StorageKafka2::partialShutdown()` for `replica_is_active_node`
(see the comment at `StorageKafka2.cpp` lines 221-225). This commit
mirrors that ordering for the per-consumer lock holders.

Caught by `Stress test (arm_release)` on PR #101113 (which is
unrelated to Kafka/Keeper -- the crash surfaced under stress-test
ZK fault injection):

  zkutil::EphemeralNodeHolder::~EphemeralNodeHolder() @ 0x15aa9699
  DB::KeeperHandlingConsumer::setKeeper(...)            @ 0x15a9985c
  DB::StorageKafka2::streamToViews(unsigned long)       @ 0x15b0cf1c

Report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=101113&sha=4cf4eb894643c2dac6b52dea6126f1a3f2e75cd2&name_0=PR&name_1=Stress%20test%20%28arm_release%29

Related issue: #84616 (sibling crash in StorageKafka2 on Keeper session loss).

STID: 3555-2986
Signed-off-by: Groene AI <[email protected]>
Reduce the explanation in `KeeperHandlingConsumer::setKeeper` to a short
two-liner that just references the precedent at
`StorageKafka2::partialShutdown` for `replica_is_active_node`, where the
same use-after-free hazard is already documented.
…storagekafka2-uaf

Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
@robot-ch-test-poll4 robot-ch-test-poll4 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels May 15, 2026
@robot-ch-test-poll4
robot-ch-test-poll4 merged commit 63af3b0 into backport/26.4/103890 May 15, 2026
@robot-ch-test-poll4
robot-ch-test-poll4 deleted the cherrypick/26.4/103890 branch May 15, 2026 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants