Skip to content

Never destroy UntrackedMemoryRegistry - #120386

Merged
Algunenano merged 3 commits into
ClickHouse:masterfrom
Algunenano:fix-untracked-memory-registry-lifetime
Sep 21, 2026
Merged

Algunenano merged 3 commits into
ClickHouse:masterfrom
Algunenano:fix-untracked-memory-registry-lifetime

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 16, 2026 •

Copy link
Copy Markdown
Member

format_fuzzer crashes at process exit, after the fuzzing loop is already done:

ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (WRITE) T2 (ExternalLoader)
  #4 DB::UntrackedMemoryRegistry::remove(...)        src/Common/UntrackedMemoryRegistry.cpp:45
  #5 DB::UntrackedMemoryCounter::~UntrackedMemoryCounter()  src/Common/UntrackedMemoryRegistry.cpp:15
  #6 DB::ThreadStatus::~ThreadStatus()               src/Common/ThreadStatus.cpp:333
  #7 startThreadFromGlobalPool(...)                  src/Common/ThreadPool.cpp:1319
 ...
Thread T2 (ExternalLoader) created by T0 here:
  #9 DB::ExternalLoader::PeriodicUpdater::enable(bool)  src/Interpreters/ExternalLoader.cpp:1356
 #10 DB::ExternalDictionariesLoader::ExternalDictionariesLoader(...)

UntrackedMemoryCounter is a member of ThreadStatus and unregisters itself from the registry when the thread goes away. The registry was a function-local static, so a thread that outlives static destruction writes through an already destroyed boost::intrusive list head, which is a write to address 0.

Every program entry point joins the global thread pool before returning from main, which is what the old comment relied on. The libFuzzer entry points cannot: libFuzzer owns main, and an atexit handler is not a way out either, because the fuzzer's global Context still owns the dictionary PeriodicUpdater at that point and GlobalThreadPool::shutdown() deadlocks while a background thread is parked on the pool. Giving the registry a lifetime that spans the whole process fixes it for every entry point, present and future, and the object stays reachable through the pointer so it is not reported as a leak. Nothing depends on the registry being destroyed: the only users are the UntrackedMemory asynchronous metric and the MEMORY_LIMIT_EXCEEDED message, both read-only.

Reproduced twice on the CI format_fuzzer binary, replaying the 18705-unit corpus (crash at exit, identical stack both times). A standalone reproduction of the same destruction order gives the exact same signature, SEGV at list_node.hpp:66:16 in set_next, and exits cleanly with this change applied.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=eb77c64f3d9837380c3b793e5688162493efe12c&name_0=NightlyFuzzers&name_1=libFuzzer%20tests&name_1=libFuzzer%20tests

Related: #120385

Changelog category (leave one):

  • CI Fix or Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.10.1.359-master (included in 26.10 and later)

A thread owning a `ThreadStatus` can outlive static destruction, and its
`UntrackedMemoryCounter` destructor then writes into the already destroyed
registry, crashing with a null write in `boost::intrusive`. Every program entry
point joins the global thread pool before returning from `main`, but the
libFuzzer entry points do not own `main` and cannot, so give the registry a
lifetime that covers the whole process instead of relying on that invariant.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=eb77c64f3d9837380c3b793e5688162493efe12c&name_0=NightlyFuzzers&name_1=libFuzzer%20tests&name_1=libFuzzer%20tests

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

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [bdafb22]

Summary: ✅


AI Review

Summary

This PR makes UntrackedMemoryRegistry process-lifetime to avoid the format_fuzzer exit-time ASan exception when a late ThreadStatus destroys its UntrackedMemoryCounter after the old function-local static registry had already been torn down. I re-checked the current code and the existing review thread against the actual format_fuzzer shutdown order (getContext() statics and ContextSharedPart::shutdown()), and I do not have remaining review findings on the PR head.

Final Verdict

✅ No blocking issues found in the current PR head.

LLVM Coverage Report

Measured on commit bdafb22.

Metric Baseline Current Δ
Lines 88.00% 88.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 79.30% 79.30% +0.00%

Changed lines: Changed C/C++ lines covered: 2/2 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Sep 16, 2026
Comment thread src/Common/UntrackedMemoryRegistry.cpp Resolved
@clickhouse-gh clickhouse-gh Bot added the comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... label Sep 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing bdafb22cd with master 2ee0ab46f (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 295f03a88; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

7 translation units recompiled, 5 s compile time in total, 7 of them have a recent master baseline.

Job report

alexey-milovidov and others added 2 commits September 19, 2026 18:03
The destruction order argument belongs in the pull request, not in the source.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@Algunenano
Algunenano added this pull request to the merge queue Sep 21, 2026
Merged via the queue into ClickHouse:master with commit 5317116 Sep 21, 2026
177 checks passed
@Algunenano
Algunenano deleted the fix-untracked-memory-registry-lifetime branch September 21, 2026 15:00
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-memory Memory management infrastructure: allocators (Allocator, Arena, PODArray), memory tracking, memor... pr-ci 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