Skip to content

Use weak_ptr to the Storage in IMergeTreeDataPart#95129

Closed
alexey-milovidov wants to merge 2 commits intomasterfrom
fix-storage-from-part-lifetime-v2
Closed

Use weak_ptr to the Storage in IMergeTreeDataPart#95129
alexey-milovidov wants to merge 2 commits intomasterfrom
fix-storage-from-part-lifetime-v2

Conversation

@alexey-milovidov
Copy link
Member

Change IMergeTreeDataPart::storage from a reference to std::weak_ptr<const IStorage> to prevent segfaults when parts outlive their storage (e.g., during DETACH TABLE).

Add getStorage() method that locks the weak_ptr and returns a shared_ptr, throwing if storage is destroyed. Add tryGetStorage() for safe use in destructors.

Update all call sites to call getStorage() once at function start and hold the shared_ptr for the duration, avoiding repeated locking overhead.

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Note: We didn't find any reason why IMergeTreeDataPart could outlive the storage, so this change should be a no-op. But maybe it will let us find issues faster.

@clickhouse-gh
Copy link
Contributor

clickhouse-gh bot commented Jan 26, 2026

Workflow [PR], commit [e88d08d]

Summary:

job_name test_name status info comment
Stateless tests (arm_asan, targeted) failure
Start ClickHouse Server failure
Stateless tests (amd_asan, flaky check) failure
Start ClickHouse Server failure
Stateless tests (amd_asan, distributed plan, parallel, 1/2) failure
Start ClickHouse Server failure
Stateless tests (amd_asan, distributed plan, parallel, 2/2) failure
Start ClickHouse Server failure
Stateless tests (amd_debug, parallel) failure
Start ClickHouse Server failure
Stateless tests (amd_tsan, parallel, 1/2) failure
Start ClickHouse Server failure
Stateless tests (amd_tsan, parallel, 2/2) failure
Start ClickHouse Server failure
Stateless tests (arm_binary, parallel) failure
Start ClickHouse Server failure
Fast test error
Build (amd_release) dropped

@clickhouse-gh clickhouse-gh bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Jan 26, 2026
@alexey-milovidov alexey-milovidov force-pushed the fix-storage-from-part-lifetime-v2 branch from 3621311 to ff914cf Compare January 26, 2026 04:03
Change `IMergeTreeDataPart::storage` from a reference to `std::weak_ptr<const IStorage>`
to prevent segfaults when parts outlive their storage (e.g., during `DETACH TABLE`).

Add `getStorage()` method that locks the weak_ptr and returns a shared_ptr, throwing
if storage is destroyed. Add `tryGetStorage()` for safe use in destructors.

Update all call sites to call `getStorage()` once at function start and hold the
shared_ptr for the duration, avoiding repeated locking overhead.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@alexey-milovidov alexey-milovidov force-pushed the fix-storage-from-part-lifetime-v2 branch from ff914cf to 7a92a31 Compare January 26, 2026 04:12
@alexey-milovidov alexey-milovidov marked this pull request as draft January 26, 2026 04:19
Combine the two loops over parts_to_activate into one to avoid calling
getStorage() twice per part.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-not-for-changelog This PR should not be mentioned in the changelog

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant