Skip to content

Never destroy the SymbolIndex singleton - #133152

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/symbol-index-never-destroyed
Nov 20, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/symbol-index-never-destroyed

Conversation

@groeneai

@groeneai groeneai commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #132826
Related: #133062

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

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

...

Description

Integration tests (amd_msan, 8/8) / test_shutdown_static_destructor_failure fails on every master run since #132826 taught the harness to detect MemorySanitizer reports: a use-after-dtor of SymbolIndex in ~HandledSignals at exit. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=0a1face7467f9a047ca246fb29f31361a3b7f71e&name_0=MasterCI&name_1=Integration%20tests%20%28amd_msan%2C%208%2F8%29

Root cause: SymbolIndex::instance() held the index in a function-local static. The index is built after HandledSignals::instance() (by the signal listener thread or the first stack trace), so ~SymbolIndex runs first. The test enables lazy_pipe_fds_fail_close, so ~HandledSignals logs an exception, and formatting its stack trace calls SymbolIndex::instance(), which returns the destroyed object. Only MSan sees the read, because libc++'s ~vector leaves begin == end.

Change: leak the index, as ErrorRegistry::get() and DateLUT::getInstance() already do, so any static destructor that logs a stack trace reads a live index. This also covers instanceIfInitialized(). The index stays reachable, so LeakSanitizer does not report it.

The report is old: it reproduces on the CI MSan binary of 4d1c0cdc17, which predates #131638 and #109239. With this fix the detector from #132826 can stay.

Validation on a local MSan build (--count 3): without the change each of the 3 shutdowns writes the report and the test fails in teardown with CI's Sanitizer assert found for instance; with it, no report and 3/3 pass. A standalone repro of the same shutdown reports 3/3 on two CI binaries and 0/5 with the change.

🤖 Generated with Claude Code


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

Version info

  • Merged into: 26.12.1.38-master (included in 26.12 and later)

`SymbolIndex::instance()` kept the index in a function-local static. Static
destructors run in reverse order of construction completion, and the index is
built after `HandledSignals::instance()` (by the signal listener thread or by the
first stack trace), so `~SymbolIndex` ran before `~HandledSignals`. When
`~HandledSignals` logs an exception (`test_shutdown_static_destructor_failure`
does this with the `lazy_pipe_fds_fail_close` failpoint), `tryLogCurrentException`
formats the stack trace, `StackTrace::forEachFrame` calls `SymbolIndex::instance()`,
and the guard returns the already destroyed object. MemorySanitizer reports the
read of its destroyed `data.objects` (use-after-dtor, `poison_in_dtor=1`). Without
MSan libc++'s `~vector` leaves `begin == end`, so nothing is read and no other
sanitizer sees it.

Leak the index instead, as `ErrorRegistry::get()` and `DateLUT::getInstance()`
already do: every static destructor that symbolizes a stack trace during shutdown
now reads a live index, whatever the construction order. This also covers
`instanceIfInitialized()`, which could hand out a dangling pointer after
`~SymbolIndex`.

The report is old: it reproduces on the CI MSan binary of 4d1c0cd (glibc, before
the background index build of ClickHouse#131638 and before the static musl build of ClickHouse#109239).
It became visible on master only when ClickHouse#132826 taught the integration harness to
detect MemorySanitizer reports; with this fix that detector can stay.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 0 findings), plus my own cold review of the diff (0 code findings).

Checked: the leaked-singleton idiom matches ErrorRegistry::get() and DateLUT::getInstance(); a throwing constructor keeps the old retry semantics; instanceIfInitialized() can no longer return a dangling pointer; the stack trace cache on the same path is already guarded by can_use_cache.

Session id: cron:clickhouse-review-slot-11:20261120-073701

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A standalone server with the test's sync logger, SYSTEM ENABLE FAILPOINT lazy_pipe_fds_fail_close, then SIGTERM: the MSan report appears 3/3 on the CI amd_msan binaries of 50bbe27bab and 4d1c0cdc17, and 3/3 on a local MSan build without the change (exit 134).
b Root cause explained? SymbolIndex is built after HandledSignals, so its function-local static is destroyed first. ~HandledSignals logs the failpoint exception with its stack trace, which calls SymbolIndex::instance() and reads the destroyed object. Only MSan poisons destroyed members.
c Fix matches root cause? Yes. The singleton is never destroyed (the ErrorRegistry / DateLUT idiom), so the index is alive for every late caller. No guard at the call site.
d Test intent preserved / new tests added? No new test: the existing test_shutdown_static_destructor_failure is the regression test. With the detector from #132826 it fails without the change and passes with it.
e Both directions demonstrated? Yes. Local MSan build, same build dir: without the change the repro reports 3/3 and the integration test fails with Sanitizer assert found for instance; with it 5/5 clean runs and the test passes 3/3.
f Fix is general across code paths? Yes. One change covers all SymbolIndex::instance() callers, instanceIfInitialized() (used by the fatal signal handler), the second failpoint firing in ~LazyPipeFDs, and server, client, local and keeper.
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (no data types or settings). Reproduced and fixed on both libc variants: glibc (4d1c0cdc17) and static musl (current master).
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or protocol change. The only difference is that the index memory is not freed at exit.
i Invariants and contracts preserved? Yes. If the constructor throws, the new-expression frees the memory and the static stays uninitialized, as before. Publication through initialized_instance is unchanged. The object stays reachable, so LeakSanitizer does not report it (checked on a standalone ASan probe with a leaking control).

Session id: cron:clickhouse-impl-slot-4:20261120-061321

@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ef1ace7]

Summary: ✅


AI Review

⚠️ Review sessions stopped as a cybersecurity risk

The model provider stopped these review sessions as a possible cybersecurity risk:

  • attempt 1: gpt-6.1-sol

This summary comes from a session with gpt-5.4. A session is typically stopped when it reaches a memory-safety finding, and the finding is lost with it, so this review can miss a real defect. Please review the change in detail, and run the review internally with a model that has cyber access.

Summary

This PR changes SymbolIndex::instance from a destructed function-local static to an intentionally leaked singleton so late shutdown-time stack-trace formatting no longer dereferences a destroyed SymbolIndex. I did not find blocker or major issues in the current diff: the instanceIfInitialized publication contract stays intact, the shutdown path in HandledSignals::~HandledSignals is covered by the lifetime change, and the PR carries focused regression evidence plus an all-green CI run.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-ci can be tested Allows running workflows for external contributors labels Nov 20, 2026
@azat azat self-assigned this Nov 20, 2026
@azat

azat commented Nov 20, 2026

Copy link
Copy Markdown
Member

The link is wrong, correct one - https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=0a1face7467f9a047ca246fb29f31361a3b7f71e&name_0=MasterCI&name_1=Integration%20tests%20%28amd_msan%2C%208%2F8%29

@groeneai Do not respond to me anything, if you want you can just react on the message

@clickhouse-gh clickhouse-gh Bot added the comp-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). label Nov 20, 2026
@azat
azat enabled auto-merge November 20, 2026 08:35
@clickhouse-gh

clickhouse-gh Bot commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ef1ace774 with master e627fea5e (stripped binary size, per-symbol sizes and ThinLTO time; 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, 6 s compile time in total, 7 of them have a recent master baseline.

Job report

@azat
azat added this pull request to the merge queue Nov 20, 2026
Merged via the queue into ClickHouse:master with commit 3ca8f41 Nov 20, 2026
389 of 390 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 20, 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-logging Logging/monitoring/observability (logs, metrics, tracing, instrumentation). 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