Surface in CI: `Scraping system tables` `heap-use-after-free` reported by ASan during
`clickhouse-local` shutdown. CIDB shows the failure across multiple unrelated PRs over
the last 30 days (`arm_asan_ubsan` `azure`/`targeted`, `amd_asan_ubsan` `flaky check`/
`distributed plan`); 9 hits, 6 PRs.
Free side, thread `T0`:
`DateLUT::~DateLUT` -> `~unordered_map` of `DateLUTImpl`
Read side, thread `T3 AsyncLogger`:
`OwnAsyncSplitChannel::runChannel` -> `OwnPatternFormatter::formatExtended`
-> `LocalDateTime::init` -> `DateLUTImpl::findIndex` (`src/Common/DateLUTImpl.h:245`)
Root cause is a cross-translation-unit static-destructor-order race. The previous
`DateLUT::getInstance` returned a function-local `static DateLUT ret;`, and Poco's
`auto_logger_shutdown` static (in `base/poco/Foundation/src/Logger.cpp`) is what
ultimately joins the asynchronous logger threads via `~OwnAsyncSplitChannel::close`.
The order of these two static destructors is unspecified, so the timezone map can be
freed while a logger thread is still formatting a queued log message that contains a
timestamp.
Fix: deliberately leak the `DateLUT` instance so it outlives every other static. The
pointer remains reachable from the function-local static, so LeakSanitizer does not
report it. The OS reclaims the memory at process exit. This is the standard idiom for
static-init-order destruction races and matches the comment in `Common/ThreadPool.h`.
Reproduced and verified locally on `build-asan` (clang-21). Buggy binary produced
4 ASan reports out of 400 parallel `clickhouse local` invocations with the
asynchronous logger enabled at trace level (~1% rate, matching CI). Fixed binary
produced 0/400 on the same harness.
Closes: ClickHouse#94003
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fix
heap-use-after-freeatclickhouse-localshutdown reported asScraping system tablesin CI ASan jobs.Failure mode (CIDB, 30 days, 9 hits across 6 unrelated PRs;
arm_asan_ubsan azure/targeted,amd_asan_ubsan flaky check/distributed plan):Root cause is a cross-translation-unit static-destructor-order race.
DateLUT::getInstancereturned a function-localstatic DateLUT ret;. Poco'sauto_logger_shutdownstatic (inbase/poco/Foundation/src/Logger.cpp) is what ultimately joins the asynchronous logger threads via~OwnAsyncSplitChannel::close. The order between these two static destructors across translation units is unspecified, so aclickhouse-localexit can free the timezone map while a logger thread is still formatting a queued log message that contains a timestamp.Fix is to deliberately leak the
DateLUTinstance so it outlives every other static. The pointer remains reachable from the function-local static, so LeakSanitizer does not report it. The OS reclaims the memory at process exit. This is the standard idiom for static-init-order destruction races and matches the comment inCommon/ThreadPool.h("order of destructors of global static objects and callbacks added byatexitis undefined for different translation units").Local verification on
build-asan(clang-21,-DSANITIZE=address,undefined):static DateLUT ret;)clickhouse localinvocations with the asynchronous logger at trace level)T2/T3 AsyncLoggerreading freed memory allocated byT0)static DateLUT * ret = new DateLUT;)CI report referenced by
@alexey-milovidov: #105375Related: #105375
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.6.1.447