Skip to content

Fix possible crash during shutting down background pools - #81473

Merged
azat merged 2 commits into
ClickHouse:masterfrom
azat:fix-bg-pool-shutdown
Jun 13, 2025
Merged

azat merged 2 commits into
ClickHouse:masterfrom
azat:fix-bg-pool-shutdown

Conversation

@azat

@azat azat commented Jun 7, 2025 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

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

Fix possible crash during shutting down background pools (background_.*pool_size)

There are multiple issues in the BackgroundSchedulePool, due to
BackgroundSchedulePoolTaskInfo holds raw reference to the pool, which is
wrong for multiple reasons, and there is no way to fix it properly
because we do not track all tasks in the BackgroundSchedulePool (even
inactive), and eventually this may lead to crash (during server
shutdown of background pools).

There were multiple attempts to fix it:

Also I've encapsulated the BackgroundSchedulePool and
BackgroundSchedulePoolTaskInfo more, to avoid any misuse.

Fixes: #80066 (cc @serxa @VicoWu)

@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2920d33]

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jun 7, 2025

@alexey-milovidov alexey-milovidov 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.

A test?

@alexey-milovidov

Copy link
Copy Markdown
Member

I've also preventively reverted #81481

@azat
azat marked this pull request as draft June 8, 2025 08:37
@azat
azat force-pushed the fix-bg-pool-shutdown branch from a0f20c2 to 3dadc61 Compare June 8, 2025 21:57
@azat azat changed the title Fix BackgroundSchedulePool destroying (deadlock and UB) Fix possible crash during shutting down background pools Jun 8, 2025
@azat
azat force-pushed the fix-bg-pool-shutdown branch from 3dadc61 to 6404951 Compare June 9, 2025 07:19
@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Jun 9, 2025
@azat
azat force-pushed the fix-bg-pool-shutdown branch 3 times, most recently from c713396 to 3cf8ad5 Compare June 9, 2025 09:42
@azat
azat marked this pull request as ready for review June 9, 2025 11:16
@azat

azat commented Jun 9, 2025

Copy link
Copy Markdown
Member Author

A test?

Add a unit test

@serxa serxa self-assigned this Jun 9, 2025

@serxa serxa 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.

Thanks for fixing it properly!

Comment thread src/Core/BackgroundSchedulePool.cpp Resolved
azat added 2 commits June 9, 2025 15:39
There are multiple issues in the BackgroundSchedulePool, due to
BackgroundSchedulePoolTaskInfo holds raw reference to the pool, which is
wrong for multiple reasons, and there is no way to fix it properly
because we do not track all tasks in the BackgroundSchedulePool (even
inactive), and eventually this may lead to crash (during server
shutdown of background pools).

There were multiple attempts to fix it:

- ClickHouse#80069

  - It introduces deadlock [1]
  - And a UB due to usuing foreach and erase inside
  - But the main reason is that it does not fix all the issues, i.e.
    active() after the pool has been destroyed still leads to UB

  [1]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81398/c07d7ee6eb7c3d1d0c65649ea4b57f881571599c//stress_test_amd_debug/gdb.log

- previous version of this patch set

  - Introduces lock order inversion [2] and [3], since I tried the
    apprach that ClickHouse#80069 was been using - clean all jobs in the
    destructor of the pool

  - But, when it came to the test I found out that while
    clickhouse-server may use this pool correctly it is hard to do so
    without knowing how to, so after this I converted the
    raw reference of the BackgroundSchedulePool in
    BackgroundSchedulePoolTaskInfo to weak_ptr, to avoid any UBs

  [2]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81473/a0f20c2ad7efc19d698ceb0281e0a122ebef457e//stress_test_amd_tsan/stderr.log
  [3]: https://s3.amazonaws.com/clickhouse-test-reports/PRs/81473/6404951bfb5156b00a4aa5302d1c0ca8272a2c56//stress_test_amd_tsan/stderr.log

Also I've encapsulated the BackgroundSchedulePool and
BackgroundSchedulePoolTaskInfo more, to avoid any misuse.
@azat
azat force-pushed the fix-bg-pool-shutdown branch from 3cf8ad5 to 2920d33 Compare June 9, 2025 13:39
@azat

azat commented Jun 9, 2025

Copy link
Copy Markdown
Member Author

@alexey-milovidov changes requests blocks from merging, PTAL

@azat
azat enabled auto-merge June 9, 2025 20:00
@azat
azat requested a review from alexey-milovidov June 11, 2025 20:25
@alexey-milovidov

Copy link
Copy Markdown
Member

Two more problems:

  1. The BugFix check in the CI does not use unit tests. Need to ask @maxknv to fix it.
  2. The stateless check failed sporadically due to some infrastructure problem. While we can tolerate this, let's think about external contributors who will be confused. Again, @maxknv should fix it.

@azat
azat added this pull request to the merge queue Jun 13, 2025
Merged via the queue into ClickHouse:master with commit 3839958 Jun 13, 2025
@azat
azat deleted the fix-bg-pool-shutdown branch June 13, 2025 13:29
@maxknv

maxknv commented Jun 13, 2025 •

Copy link
Copy Markdown
Member

Two more problems:

  1. The BugFix check in the CI does not use unit tests. Need to ask @maxknv to fix it.

I think with unit tests it's only possible to check that they were added. Or how do you suggest validating that a new unit test covers a problem?

  1. The stateless check failed sporadically due to some infrastructure problem. While we can tolerate this, let's think about external contributors who will be confused. Again, @maxknv should fix it.
    I see this one, it's not infrastructural
+ sudo -E -u clickhouse /usr/bin/clickhouse-server --config /etc/clickhouse-server/config.xml --daemon --pid-file /var/run/clickhouse-server/clickhouse-server.pid
==439==LeakSanitizer has encountered a fatal error.
==439==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==439==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)
INFO:root:Run failed, exit code 1
INFO:root:Files in result folder ['run.log']
Run action failed for: [Stateless tests (asan, distributed plan, 2/2)] with exit code [1]
Run action done for: [Stateless tests (asan, distributed plan, 2/2)]
{}
ERROR: Run failed with exit code [1]
=== Run script finished ===

@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 13, 2025
@alexey-milovidov

Copy link
Copy Markdown
Member

I think with unit tests it's only possible to check that they were added.

Yes - added or modified unit test - fulfill the BugFix check.

@alexey-milovidov

Copy link
Copy Markdown
Member

LeakSanitizer has encountered a fatal error.

Ok, and we should fix it. If it's not yours, ask your team lead, @alesapin

@azat

azat commented Jun 14, 2025

Copy link
Copy Markdown
Member Author

==439==LeakSanitizer has encountered a fatal error.

This is some issue with LSan itself I think, if someone will have a reproducer I will take a look, post it please to this issue - #81858

azat added a commit to azat/ClickHouse that referenced this pull request Jul 15, 2025
After ClickHouse#81473 it is possible that BackgroundSchedulePool threads will be
joined from some task, due to weak_ptr, fix this, by adding separate
method for shutdown that will be called explicitly.
pull Bot pushed a commit to Lobinson1/ClickHouse that referenced this pull request Aug 21, 2026
…lease

The Upgrade check starts a previous release and then upgrades to the build
under test. get_previous_release_tag.py picks that baseline, and it could
silently pick one many releases too old.

find_previous_release skipped any release below server_version whose metadata
did not yet list an uploaded clickhouse-common-static_*.deb, returning
(False, None). The caller's unbounded `while not found:` then advanced to the
next page. The /releases endpoint is ordered by created_at, not by version, so
later pages hold only older releases, and nothing re-checked that the answer
was the newest release below the version under test. The only diagnostic on
the skip path was a logger.debug under basicConfig(level=INFO), and the
resolver still exited 0 with a well-formed tag, so upgrade_runner.sh's
`[ $? -ne 0 ]` guard passed.

On PR ClickHouse#114376 commit e1cb0de this resolved v25.1.4.53 as the baseline for
a 26.8.1.1 build; that 25.1 server then aborted during shutdown on a
use-after-free that 2920d33 (ClickHouse#81473) already fixed, so the job reported
`Received signal 6` and `Cannot start clickhouse-server` for a reason
unrelated to the PR. Two commits earlier the same PR resolved v26.7.3.19
correctly with a byte-identical resolver, and 4 of 47 recent Upgrade-check
runs used a wrong baseline. Master and every active release branch already
carry the weak_ptr fix, so no C++ change is needed here.

Select the baseline by version alone and resolve from the first page or raise.
Page 1 holds 100 releases and its newest entry below 26.8.1.1 already equals
the maximum over all 700, so the walk was never needed on the healthy path; a
page-1 miss means the response was degraded, which is exactly when continuing
is wrong. A page cap was rejected because page 2 can supply a stale candidate
before any cap fires.

Removing the asset filter from selection changes one failure mode
deliberately: a partially published release that is genuinely the newest below
the build is now selected and fails at the download boundary with
`Download script failed`, instead of being skipped in favour of an older one.
Validation already fails closed there - download_packages raises for a
required prefix absent from the release metadata - so this trades a narrow
window for a loud, attributable failure instead of a silent wrong-baseline
run.

Adds ci/tests/test_get_previous_release_tag.py; the resolver had no tests.
Three of the six cases fail on the previous code, returning the exact stale
tags observed in CI.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=114376&sha=e1cb0dee5e9bd972e9bbbdd8cd8d971ad1e39ee1&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default 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.

BackgroundSchedulePoolTaskInfo::deactivate() may cause use-after-free issue if the owning pool is already destroyed

5 participants