Skip to content

Fix FileCache shutdown deadlock caused by DatabaseCatalog exceptions - #101612

Merged
alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
zoomxi:fix-shutdown-deadlock-filecache
Apr 12, 2026
Merged

alexey-milovidov merged 7 commits into
ClickHouse:masterfrom
zoomxi:fix-shutdown-deadlock-filecache

Conversation

@zoomxi

@zoomxi zoomxi commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

During server shutdown, a deadlock can occur in GlobalThreadPool::shutdown() because FileCache background threads remain stuck in the thread pool. This happens when DatabaseCatalog::shutdown() throws an exception (e.g., ZooKeeper/Keeper timeout in replicated DDL worker), causing deactivateBackgroundOperations() to be skipped.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100543&sha=47354faba96b66dfb0a75ac61efa53e45ca1b4e1&name_0=PR&name_1=Stress%20test%20%28arm_asan_ubsan%2C%20s3%29
#100543

The timeline confirms:

  1. DatabaseCatalog::shutdown() can throw: The Keeper timeout exception at 06:30:17 proves this
  2. Exception skips FileCache cleanup: The original code had deactivateBackgroundOperations() after DatabaseCatalog::shutdown(), which is never reached
  3. GlobalThreadPool deadlock: Threads remain stuck when GlobalThreadPool::shutdown() tries to join them at 06:36:13

Changelog category (leave one):

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

@kssenii kssenii self-assigned this Apr 2, 2026
SHUTDOWN(log, "common executor", common_executor, wait());

/// Deactivate FileCache background threads before shutting down the database catalog.
/// DatabaseCatalog::shutdown() can throw (e.g. ZooKeeper timeout in replicated DDL worker).

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.

Why not just put DatabaseCatalog::shutdown() into try-catch? There can be other things which are good to properly shutdown

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just using try-catch isn't enough because the catalog may partially-shutdown, and FileCache threads can still deadlock when accessing half-destroyed tables.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

CacheMetadata::downloadThreadFunc threads shown in the GDB log are FileCache's background threads. They appear in the gdb.log as follows:

182 "ThreadPool" threads (LWP 468958-468963, etc.) stuck in CacheMetadata::downloadThreadFunc on GlobalThreadPool, e.g. Thread 293 (LWP 468958):

#8 DB::CacheMetadata::downloadThreadFunc at Metadata.cpp:776
#9 DB::CacheMetadata::startup()::$_0::operator() at Metadata.cpp:944
#13 ThreadFromGlobalPoolImpl<...> at ThreadPool.h:312
#20 ThreadPoolImpl<...>::ThreadFromThreadPool::worker at ThreadPool.cpp:809

These threads are waiting on a condition variable and will never finish because deactivateBackgroundOperations() was skipped, which prevents GlobalThreadPool::shutdown() from completing.

@kssenii kssenii added the can be tested Allows running workflows for external contributors label Apr 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2635903]

Summary: ✅


AI Review

Summary

This PR fixes a real shutdown deadlock risk by moving FileCache background deactivation to occur before DatabaseCatalog::shutdown, so exceptions from catalog shutdown no longer skip cache thread deactivation. The follow-up adjustment keeps FileCacheFactory::instance().clear() after catalog shutdown, preserving prior teardown ordering while still guaranteeing deadlock prevention. I did not find correctness, concurrency, or compatibility regressions in the touched code path.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Apr 2, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

The Stress test (arm_msan) failure is fixed by #101239, which should be merged first. After it is merged, please update the branch to include the fix.

@kssenii
kssenii enabled auto-merge April 7, 2026 10:27
@zoomxi

zoomxi commented Apr 7, 2026

Copy link
Copy Markdown
Contributor Author

Upgrade check (amd_release) "There is no cache by name: cached_s3"

Comment thread src/Interpreters/Context.cpp Outdated
LOG_TRACE(log, "Shutting down caches");
for (const auto & cache_data : FileCacheFactory::instance().getUniqueInstances())
cache_data->cache->deactivateBackgroundOperations();
FileCacheFactory::instance().clear();

@kssenii kssenii Apr 8, 2026 •

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.

The upgrade check fails because of FileCacheFactory::instance().clear();, we should not do this until tables are shutdown.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry about that, I missed this case. Will fix by moving FileCacheFactory::instance().clear() after DatabaseCatalog::shutdown().

auto-merge was automatically disabled April 8, 2026 13:53

Head branch was pushed to by a user without write access

@alexey-milovidov

Copy link
Copy Markdown
Member

The 02346_text_index_bug101913 test failure is fixed by #102108 (already merged). Please rebase or merge master to pick up the fix.

@clickhouse-gh

clickhouse-gh Bot commented Apr 11, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 78.00% 84.00% +6.00%
Functions 90.40% 90.90% +0.50%
Branches 70.50% 76.50% +6.00%

Changed lines: 100.00% (18/18) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@zoomxi

zoomxi commented Apr 12, 2026

Copy link
Copy Markdown
Contributor Author

@kssenii All checks passed. PTAL, thanks.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Apr 12, 2026
Merged via the queue into ClickHouse:master with commit 9029369 Apr 12, 2026
164 checks passed
@clickgapai

Copy link
Copy Markdown
Contributor

Hi — this PR may need backporting to 26.3 (LTS), 26.2, 26.1, 25.8 (LTS), but no backport label was found.

Affected code: ContextSharedPart::shutdown ordering of FileCache::deactivateBackgroundOperations relative to DatabaseCatalog::shutdown — core code present in all supported branches.

Why: This fixes a server shutdown deadlock triggered when DatabaseCatalog::shutdown throws (e.g., Keeper timeout). The affected code (deactivateBackgroundOperations in Context.cpp shutdown path and FileCache background thread management) has existed since 2023, predating all supported release branches. Any production server using filesystem cache with replicated tables could hit this deadlock during shutdown.

If this should be backported, consider adding pr-must-backport or a version-specific label (e.g. v26.3-must-backport). Ignore this if backporting is not applicable.

@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 12, 2026
@zoomxi
zoomxi deleted the fix-shutdown-deadlock-filecache branch April 13, 2026 02:08
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 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.

5 participants