Repository navigation
Fix data race in Context::executeTableFunction - #94171
Conversation
|
Workflow [PR], commit [82a10c4] Summary: ❌
|
| /// 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; |
There was a problem hiding this comment.
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?
| -- When data is inserted into source, multiple threads may process chunks | ||
| -- and resolve the numbers() table function concurrently. |
There was a problem hiding this comment.
But why do they have the same Context?
There was a problem hiding this comment.
I think it's because it is the query context, which belongs to the original query.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
This is suspicious to me. But I'd merge, because:
|
Backport #94171 to 25.12: Fix data race in Context::executeTableFunction
Backport #94171 to 25.10: Fix data race in Context::executeTableFunction
Backport #94171 to 25.11: Fix data race in Context::executeTableFunction
…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]>
Changelog category (leave one):
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
26.1.1.53625.12.4.15,25.11.7.36,25.10.5.28