Repository navigation
Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2) - #103890
antaljanosbenjamin merged 2 commits into
Conversation
|
cc @antaljanosbenjamin @kssenii — could you review this? It fixes a use-after-free in Session: cron:clickhouse-ci-task-worker:20260502-021500 |
|
Pre-PR validation gate answers (per (a) Deterministic repro? Partial. The crash is a race-y use-after-free that requires:
It hit once on master/PR #101113's arm_release stress run (STID 3555-2986). I did not deterministically reproduce it locally because that requires the stress-test ZK fault-injection harness (or a unit test that mocks shared_ptr ownership for ZooKeeper, which would require non-trivial scaffolding). However, the static-analysis argument for the bug is exhaustive (see (b)). (b) Root cause explained? Yes.
The exact same hazard is documented and fixed in (c) Fix matches root cause? Yes. Pure reordering: clear lock holders first (while (d) Test intent preserved? N/A — this is a production-code lifecycle fix, no test changes. (e) Both directions demonstrated? Static-analysis-only. Without the stress-test fault-injection setup, I cannot easily exhibit the crash on demand. The change compiles cleanly ( (f) Fix is general, not a narrow patch? Yes. I audited every other
The deeper "why does the bad value arrive" answer is: Session: cron:clickhouse-ci-task-worker:20260502-021500 |
|
Workflow [PR], commit [ad88999] Summary: ❌
AI ReviewSummaryThis PR reorders operations in ClickHouse Rules
Final Verdict
|
| /// Drop the lock holders BEFORE replacing the `keeper` shared_ptr. | ||
| /// | ||
| /// `LockedTopicPartitionInfo::lock` is a `zkutil::EphemeralNodeHolder`, which stores | ||
| /// a raw `ZooKeeper &` reference captured at construction time (see `EphemeralNodeHolder` | ||
| /// in `Common/ZooKeeper/ZooKeeper.h`). Its destructor calls `zookeeper.expired()` and | ||
| /// possibly `zookeeper.tryRemove()`. If we assigned `keeper = keeper_` first and our | ||
| /// `keeper` was the last shared owner of the previous session, that assignment would | ||
| /// synchronously destroy the old `ZooKeeper`, and the subsequent `permanent_locks.clear()` | ||
| /// / `tmp_locks.clear()` would then access a dangling reference -> use-after-free. | ||
| /// | ||
| /// `StorageKafka2::partialShutdown()` already documents and applies the same ordering for | ||
| /// `replica_is_active_node`; this mirrors that fix for the per-consumer lock holders. |
There was a problem hiding this comment.
@groeneai this is too long explanation. Give a much simpler explanation, maybe just reference the partial shutdown and active replica node holder.
There was a problem hiding this comment.
Done in fdcaa94 — trimmed to two lines that just reference the precedent at StorageKafka2::partialShutdown for replica_is_active_node. Thanks for the review @antaljanosbenjamin!
|
@groeneai, fix |
`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 ClickHouse#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: ClickHouse#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.
fdcaa94 to
ad88999
Compare
|
@alexey-milovidov —
CIDB verification (master only, Branch updated — rebased onto current master (
Awaiting fresh CI on the rebased branch. Session: cron:clickhouse-ci-task-worker:20260507-014500 |
LLVM Coverage Report
Changed lines: 0.00% (0/7) · Uncovered code |
3617837
|
Hi — could this be a backport candidate for Affected code: Why this might apply: Fix in Caveat: I verified that the modified code exists in those branches, but not whether the bug is actually reachable there — a new caller in master may be what makes this newly observable. If older branches can't reach the affected path, please ignore this; otherwise consider |
Cherry pick #103890 to 25.8: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
…r::setKeeper (StorageKafka2)
Cherry pick #103890 to 26.2: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
…r::setKeeper (StorageKafka2)
Cherry pick #103890 to 26.3: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
…r::setKeeper (StorageKafka2)
Cherry pick #103890 to 26.4: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
…r::setKeeper (StorageKafka2)
Backport #103890 to 26.2: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
Backport #103890 to 26.3: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
Backport #103890 to 26.4: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
Backport #103890 to 25.8: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
…ingConsumer::setKeeper (StorageKafka2)
…3890 Backport ClickHouse#103890 to 26.4: Fix use-after-free in KeeperHandlingConsumer::setKeeper (StorageKafka2)
What
Fix a use-after-free in
DB::KeeperHandlingConsumer::setKeeper()(used byStorageKafka2) where, on a Keeper session swap, the lock holders could outlive theZooKeeperinstance they reference and crash in~zkutil::EphemeralNodeHolder().Why / root cause
LockedTopicPartitionInfo::lock(held inpermanent_locksandtmp_locks) is azkutil::EphemeralNodeHolder, which stores a rawZooKeeper &reference captured at construction time (seeEphemeralNodeHolderinCommon/ZooKeeper/ZooKeeper.h). Its destructor callszookeeper.expired()and possiblyzookeeper.tryRemove().KeeperHandlingConsumer::setKeeper()was assigning the newkeepershared_ptr before clearing the lock maps:If the consumer was the last shared owner of the previous
ZooKeeper(which is plausible: by the timestreamToViews()callssetKeeper,StorageKafka2::keeperandContext's keeper have typically already moved on to the new session), then step (1) synchronously destroys the oldZooKeeper. Step (2) then runs the holders' destructors which callzookeeper.expired()on the freed object — SIGSEGV.This is the same hazard already documented and fixed in
StorageKafka2::partialShutdown()forreplica_is_active_node. Fromsrc/Storages/Kafka/StorageKafka2.cpplines 221-225:The per-consumer lock holders need the same treatment. This PR clears
permanent_locksandtmp_locksfirst, then replaceskeeper.Crash report
Caught on Stress test (arm_release) on PR #101113 (which is unrelated to Kafka/Keeper — the crash surfaced under stress-test ZK fault injection):
3555-2986Related
cppkafka::HandleExceptionthere, raw SIGSEGV in~EphemeralNodeHolderhere) but same component and trigger (Keeper session loss). This fix likely addresses one manifestation of that family of bugs; StorageKafka2 crashed after a connection loss with Keeper #84616 itself may have additional manifestations.Scope review
Audited every
EphemeralNodeHolderPtr/EphemeralNodeHolderusage that could outlive akeeperswap:StorageKafka2::replica_is_active_nodepartialShutdown())StorageReplicatedMergeTree::replica_is_active_nodepartialShutdown())StorageKafka2.cpp:574metadata_drop_lockkeepershared_ptr aliveStorageKafka2.cpp:766drop_lockKeeperHandlingConsumer::permanent_locks/tmp_locksTesting notes
The bug is a race-y use-after-free: it requires the consumer to be the last
shared_ptrowner of the previousZooKeeper, with at least one acquired ephemeral lock at the moment ofsetKeeper. Reproducing this deterministically requires the stress-test ZK fault-injection harness that originally caught it (the bug is rare in normal operation and the master CIDB shows only one hit so far for STID3555-2986).The fix itself is provably correct from the
shared_ptr/ raw-reference lifetime argument and mirrors the documented and already-merged precedent forreplica_is_active_node. It is a pure reordering of operations withinsetKeeper; the externally observable post-state of the function is unchanged.The change compiles cleanly (
ninja -C build src/CMakeFiles/dbms.dir/Storages/Kafka/KeeperHandlingConsumer.cpp.o).Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a use-after-free in
StorageKafka2that could crash the server when a Keeper session is replaced while the consumer is holding ephemeral topic-partition locks.Documentation entry for user-facing changes
Version info
26.5.1.67426.4.3.23,26.3.11.23,26.2.19.30,25.8.24.17