Skip to content

Stop the asynchronous logging threads at clickhouse-local exit - #112753

Closed
alexey-milovidov wants to merge 1 commit into
masterfrom
fix-local-async-logger-thread-leak
Closed

alexey-milovidov wants to merge 1 commit into
masterfrom
fix-local-async-logger-thread-leak

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 31, 2026 •

Copy link
Copy Markdown
Member

Closes: #110816
Related: #110099
Related: #111979

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

clickhouse-local never stopped the asynchronous logging threads (one AsyncLogger per log channel plus AsyncTextLog) started by Loggers::buildLoggers. Unlike the server, where ~BaseDaemon calls stopLogging, LocalServer had no such call, so OwnAsyncSplitChannel::close never ran and the threads were still consuming the log queues while exit ran static destructors — verified with gdb breakpoints and strace: close is never called and the threads are terminated by exit_group instead of being joined.

In CI this intermittently fails the "Scraping system tables" step, which dumps every system.*_log table with a separate clickhouse-local --config-file=/etc/clickhouse-server/config.xml invocation. Under ThreadSanitizer the unjoined threads surface at exit as

WARNING: ThreadSanitizer: thread leak (pid=8365)
  Thread T3 'AsyncLogger' (tid=8369, finished) created by main thread at:
    ...
    #3 DB::OwnAsyncSplitChannel::open() src/Loggers/OwnSplitChannel.cpp:276:25
    #4 Loggers::buildLoggers(...) src/Loggers/Loggers.cpp:297:12
    #5 DB::LocalServer::processConfig() programs/local/LocalServer.cpp:1321:9

and since sanitizers run with abort_on_error=1, the report aborts clickhouse-local, so every table dump in an affected run is reported as failed (about 33 such failures across amd_tsan checks since 2026-07-21).

The MSan use-of-uninitialized-value failures of the same CI step on amd_msan are the same shutdown race, caught in the act. OwnPatternFormatter::formatExtended reads Poco's static priority-name strings (writeString(getPriorityName(priority), wb)), and the MSan origin shows they had already been destroyed by exit (with poison_in_dtor=1, destroyed objects are poisoned) while the AsyncLogger thread was still formatting a message:

  Member fields were destroyed
    #1 std::__1::basic_string<...>::~basic_string() contrib/llvm-project/libcxx/include/string:917:3
    #3 __cxx_global_array_dtor base/poco/Foundation/src/PatternFormatter.cpp
    #4 MSanCxaAtExitWrapper(void*) msan_interceptors.cpp
    #5 __run_exit_handlers stdlib/exit.c:113:8

The fix mirrors BaseDaemon: ~LocalServer stops and joins the asynchronous logging threads (OwnAsyncSplitChannel::close flushes the queues before joining), so they never overlap with static destruction. Verified with strace that the AsyncLogger/AsyncTextLog threads now exit and are joined before the final exit_group, on both the success and the error path, with exit codes unchanged.

No test is added: every clickhouse-local invocation in the sanitizer stateless CI runs exercises this path.

Unlike the server, where `~BaseDaemon` calls `stopLogging`, `LocalServer`
never stopped the `AsyncLogger`/`AsyncTextLog` threads started by
`Loggers::buildLoggers`, so `OwnAsyncSplitChannel::close` was never called
and the threads were still consuming the log queues while `exit` ran
static destructors. ThreadSanitizer intermittently reported them as leaked
threads at exit and aborted the process (sanitizers run with
`abort_on_error=1`), which failed the "Scraping system tables" CI step for
every system table dumped by `clickhouse-local`.

Closes: #110816

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ad912f6]


AI Review

Summary

This PR fixes clickhouse-local leaking the async logging threads by stopping Loggers during LocalServer teardown. I reviewed the current diff and surrounding shutdown paths, checked the existing PR discussion state, and did not find a correctness or lifecycle regression in the current code; the change is consistent with the intended server-side shutdown model, and the current CI report is green.

Final Verdict

✅ No blocking findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 31, 2026
@Algunenano Algunenano self-assigned this Jul 31, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. LocalServer is the only one not inheriting from BaseDaemon, which makes this necessary

@clickhouse-gh

clickhouse-gh Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 80.90% 86.50% +5.60%
Functions 91.40% 91.90% +0.50%
Branches 72.90% 78.60% +5.70%

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

Full report · Diff report

alexey-milovidov pushed a commit that referenced this pull request Jul 31, 2026
`clickhouse-local` never stopped the asynchronous logging threads (one `AsyncLogger` per log
channel plus `AsyncTextLog`) started by `Loggers::buildLoggers`. Unlike the server, where
`~BaseDaemon` calls `stopLogging`, `LocalServer` had no such call, so
`OwnAsyncSplitChannel::close` never ran and the threads were still consuming the log queues
while `exit` ran static destructors.

Under ThreadSanitizer the unjoined threads surface at exit as a `thread leak` report on
`AsyncLogger`, and since sanitizers run with `abort_on_error=1` the report aborts
`clickhouse-local`. That made the `Scraping system tables` CI step report every
`system.*_log` dump of an affected run as failed. The `amd_msan`
`use-of-uninitialized-value` in `OwnPatternFormatter::formatExtended` is the same shutdown
race caught in the act: the `AsyncLogger` thread was still formatting a message using Poco's
static priority-name strings after `exit` had destroyed them.

The fix mirrors `BaseDaemon`: `~LocalServer` stops and joins the asynchronous logging threads
(`OwnAsyncSplitChannel::close` flushes the queues before joining), so they never overlap with
static destruction.

Originally submitted as #112753 against the
first version of this change. Folded in here because that version was reverted in
#112793 and is reintroduced by this pull
request, so the fix must land together with it.

Closes: #110816
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Superseded by #112803.

#107363, the change that exposed this shutdown race, was reverted in #112793 and is being reintroduced by #112803. Since the fix has to land together with it rather than after it, this commit was folded into that pull request as d9193328342 — byte-identical to the diff here.

Closing in favour of #112803, which now carries Closes: https://github.com/ClickHouse/ClickHouse/issues/110816.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Scraping system tables

2 participants