Skip to content

Fix possible usage of outdated parts due to TOCTOU race for shared parts - #93022

Merged
azat merged 1 commit into
ClickHouse:masterfrom
azat:fix-shared-parts-TOCTOU-race
Dec 24, 2025
Merged

azat merged 1 commit into
ClickHouse:masterfrom
azat:fix-shared-parts-TOCTOU-race

Conversation

@azat

@azat azat commented Dec 24, 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 into CHANGELOG.md):

Fix possible usage of outdated parts due to TOCTOU race for shared parts

Imagine the following scenario:

T1: TRUNCATE TABLE                       T2: OPTIMIZE TABLE

      lockParts() # exclusive lock!
        shared_parts_list = nullptr
                                           getPossiblySharedVisibleDataPartsRanges()
                                             shared_lock_holder = readLockParts() # shared lock, shared_parts_list == nullptr
                                             auto parts = getDataPartsVectorForInternalUsage({DataPartState::Active}, *shared_lock_holder);
                                             shared_lock_holder.reset();
      lockParts() # exclusive lock
        remove parts from list
        shared_parts_list = nullptr
                                             lock_holder.emplace(data_parts_mutex, /*data_=*/ nullptr); # exclusive lock!
                                             shared_ranges_in_parts = std::make_shared<const RangesInDataParts>(parts);
                                             shared_parts_list = std::make_shared<const DataPartsVector>(std::move(parts));

It will lead to T2 will cache outdated parts in shared_parts_list.

And I think this is why 01443_merge_truncate_long started to fail

Fixes: #92098
Fixes: #85535

@clickhouse-gh

clickhouse-gh Bot commented Dec 24, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ae20578]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, AsyncInsert, s3 storage, parallel) failure
03448_analyzer_array_join_alias_in_join_using_bug FAIL
Integration tests (arm_binary, distributed plan, 2/4) failure
test_scheduler_cpu_preemptive/test.py::test_independent_pools FAIL issue
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting in Function_or: the query: (STID: 1941-1bfa) FAIL issue
Finish Workflow failure
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py failure

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Dec 24, 2025
@ahmadov ahmadov self-assigned this Dec 24, 2025
@azat

azat commented Dec 24, 2025

Copy link
Copy Markdown
Member Author

Things got more complex because OPTIMIZE in the issue does not use list of parts from this snapshot, so it does nothing, and it was hard to understand

It even reset list of these parts (heh)

if (auto * snapshot_data = dynamic_cast<MergeTreeData::SnapshotData *>(storage_snapshot->data.get()))
    snapshot_data->parts = {};

table->optimize(query_ptr, metadata_snapshot, ast.partition, ast.final, ast.deduplicate, column_names, ast.cleanup, getContext());

Imagine the following scenario:

    T1: TRUNCATE TABLE                       T2: OPTIMIZE TABLE

          lockParts() # exclusive lock!
            shared_parts_list = nullptr
                                               getPossiblySharedVisibleDataPartsRanges()
                                                 shared_lock_holder = readLockParts() # shared lock, shared_parts_list == nullptr
                                                 auto parts = getDataPartsVectorForInternalUsage({DataPartState::Active}, *shared_lock_holder);
                                                 shared_lock_holder.reset();
          lockParts() # exclusive lock
            remove parts from list
            shared_parts_list = nullptr
                                                 lock_holder.emplace(data_parts_mutex, /*data_=*/ nullptr); # exclusive lock!
                                                 shared_ranges_in_parts = std::make_shared<const RangesInDataParts>(parts);
                                                 shared_parts_list = std::make_shared<const DataPartsVector>(std::move(parts));

It will lead to T2 will cache outdated parts in `shared_parts_list`.

And I think this is why `01443_merge_truncate_long` started to fail

Fixes: ClickHouse#92098
Fixes: ClickHouse#85535
@azat
azat force-pushed the fix-shared-parts-TOCTOU-race branch from 86dc699 to ae20578 Compare December 24, 2025 17:36

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

LGTM 👍 Thanks for a quick fix!

@azat
azat enabled auto-merge December 24, 2025 17:50
@azat

azat commented Dec 24, 2025

Copy link
Copy Markdown
Member Author

@azat
azat added this pull request to the merge queue Dec 24, 2025
Merged via the queue into ClickHouse:master with commit fe839c6 Dec 24, 2025
@azat
azat deleted the fix-shared-parts-TOCTOU-race branch December 24, 2025 21:06
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Dec 24, 2025
robot-clickhouse added a commit that referenced this pull request Dec 24, 2025
robot-clickhouse added a commit that referenced this pull request Dec 24, 2025
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-synced-to-cloud The PR is synced to the cloud repo pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore labels Dec 24, 2025
clickhouse-gh Bot added a commit that referenced this pull request Dec 24, 2025
Backport #93022 to 25.12: Fix possible usage of outdated parts due to TOCTOU race for shared parts
clickhouse-gh Bot added a commit that referenced this pull request Dec 24, 2025
Backport #93022 to 25.11: Fix possible usage of outdated parts due to TOCTOU race for shared parts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-bugfix Pull request with bugfix, not backported by default pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo v25.12-must-backport

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: 01443_merge_truncate_long

4 participants