Skip to content

Fix data race in Context::executeTableFunction - #94171

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-data-race-in-execute-table-function
Jan 14, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-data-race-in-execute-table-function

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jan 14, 2026 •

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 data race in evaluating table functions in materialized views.

Found by BuzzHouse, but I lost where it was.
Running the test under TSan on master gives https://pastila.nl/?00001f0c/3ca86a2ec3fd4c7442e446bf2b79ba00#yrPNRTU3JtB/psfKHE+a2g==GCM

Closes #90712.

Version info

  • Merged into: 26.1.1.536
  • Backported to: 25.12.4.15, 25.11.7.36, 25.10.5.28

@alexey-milovidov alexey-milovidov added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jan 14, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jan 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [82a10c4]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 1/6) failure
Select historical data failure
Performance Comparison (amd_release, master_head, 5/6) failure
Select historical data failure
Performance Comparison (amd_release, master_head, 6/6) failure
Select historical data failure
BuzzHouse (amd_tsan) error

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jan 14, 2026
@novikd novikd self-assigned this Jan 14, 2026
/// TODO: maybe replace with temporary tables?
StoragePtr view_source; /// Temporary StorageValues used to generate alias columns for materialized views
Tables table_function_results; /// Temporary tables obtained by execution of table functions. Keyed by AST tree id.
mutable std::mutex table_function_results_mutex;

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.

Query analysis is done in a single thread. I don't quite understand how this data race happens. Is it possible that incorrect context was passed into the processing view?

Comment on lines +12 to +13
-- When data is inserted into source, multiple threads may process chunks
-- and resolve the numbers() table function concurrently.

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.

But why do they have the same Context?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think it's because it is the query context, which belongs to the original query.

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.

ExecutingInnerQueryFromViewTransform::process creates a context copy on each block. I think the bug is somewhere else.
Of course, we can merge this PR, but the proper fix would be to find the place where incorrect context is used.

@azat azat Jan 15, 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.

Analyzer uses Context::getQueryContext() which is the same even after Context::copy()
And I guess there can be more problem like this

We can use smth like this create_context->setQueryContext(std::const_pointer_cast<Context>(current_context)), does not looks good, but we already have such in i.e. InterpreterCreateQuery

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.

Something like this - #94287

@alexey-milovidov

Copy link
Copy Markdown
Member Author

This is suspicious to me. But I'd merge, because:

  • the test reproduces the bug and checks that it is fixed;
  • concurrent access to table functions might happen somewhere else - I'd also prefer not to have it, but the fix for now is reasonable.

@alexey-milovidov
alexey-milovidov merged commit bac8f31 into master Jan 14, 2026
124 of 128 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-data-race-in-execute-table-function branch January 14, 2026 21:21
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jan 14, 2026
@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 Jan 14, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jan 14, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jan 15, 2026
Backport #94171 to 25.12: Fix data race in Context::executeTableFunction
alexey-milovidov added a commit that referenced this pull request Jan 15, 2026
Backport #94171 to 25.10: Fix data race in Context::executeTableFunction
alexey-milovidov added a commit that referenced this pull request Jan 17, 2026
Backport #94171 to 25.11: Fix data race in Context::executeTableFunction
pull Bot pushed a commit to CrazyForks/ClickHouse that referenced this pull request May 14, 2026
…results

`table_function_results` is a `Tables` map protected by `table_function_results_mutex`.
All 5 production accesses (`Context::executeTableFunction`,
`Context::clearTableFunctionResults`) take the mutex before reading or writing
the map. The `ContextData(const ContextData &)` copy constructor, however,
copied the map in its initializer list:

    table_function_results(o.table_function_results),

without acquiring `o.table_function_results_mutex`. When another thread was
mutating `o.table_function_results` under the mutex (e.g. inserting a new
table-function cache entry from `Context::executeTableFunction`), TSan
reported a data race against the unsynchronized read inside the copy
constructor.

Reported via STID 1003-358c (issue ClickHouse#104807). The truncated stack from the
AST fuzzer hit on PR ClickHouse#104479 (amd_tsan) showed:

  T762 (write, holding M0): Context::executeTableFunction ->
    std::map::__insert_node_at
  T748 (read, no mutex):    ContextData::ContextData(const ContextData&) ->
    std::map __copy_construct_tree

The fix follows the established pattern used elsewhere in this file
(`QueryAccessInfo::QueryAccessInfo`, `QueryFactoriesInfo::QueryFactoriesInfo`,
`QueryPrivilegesInfo::QueryPrivilegesInfo`): take the source object's
mutex and copy the protected field in the constructor body.

PR ClickHouse#94171 originally added `table_function_results_mutex` and locked the
production readers/writers, but did not update the `ContextData` copy
constructor. This fix closes that gap.

Also adds a regression test in `gtest_context_race.cpp` that races a
writer thread (`executeTableFunction`) against a copier thread
(`Context::createCopy`). Without this fix, the TSan build reports the
race; with the fix, the test passes.

CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104479&sha=42334bfbb7d48857ed6741a30fd2a876590b1dff&name_0=PR&name_1=AST%20fuzzer%20%28amd_tsan%29

Closes ClickHouse#104807

Co-Authored-By: Claude Opus 4.7 <[email protected]>
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 Pull request should be backported intentionally. Use this label with great care! 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TSAN data race at executeTableFunction

5 participants