Skip to content

Fix data race in Context::executeTableFunction while pushing to MVs v2 - #94287

Closed
azat wants to merge 2 commits into
ClickHouse:masterfrom
azat:mv-analyzer-resolve-race-v2
Closed

azat wants to merge 2 commits into
ClickHouse:masterfrom
azat:mv-analyzer-resolve-race-v2

Conversation

@azat

@azat azat commented Jan 15, 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 Context::executeTableFunction while pushing to MVs v2

Refs: #94171

@clickhouse-gh

clickhouse-gh Bot commented Jan 15, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ac0070f]

Summary: ❌

job_name test_name status info comment
Fast test failure
01927_query_views_log_current_database FAIL cidb
02790_async_queries_in_query_log FAIL cidb
Finish Workflow failure
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py failure
Build (amd_debug) dropped
Build (amd_asan) dropped
Build (amd_tsan) dropped
Build (amd_msan) dropped
Build (amd_ubsan) dropped
Build (amd_binary) dropped
Build (arm_asan) dropped
Build (arm_binary) dropped

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jan 15, 2026
/// InterpreterSelectQuery will do processing of alias columns.
auto local_context = Context::createCopy(context);
/// Avoid sharing query_context, since it can be accessed from multiple threads
local_context->setQueryContext(std::const_pointer_cast<Context>(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.

const_pointer_cast can be also replaced with storing proper MutableContextPtr initially

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.

It is interesting, I thought query contexts is suppose to be shared shared between all parts of query, like processors. We set it once and use it after.

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.

is it right to store table function result in query 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 guess it make sense to use query_context here, since it has broader scope, but capped to the query (i.e. no issues with releasing resources)

But with this change, there are other problems, i.e. collection of used tables/columns in views is broken, and maybe something else (for what we do not have tests)

And even though this can be fixed, in general usually methods of Context are thread-safe

So looks like mutex is a better choice... Thoughts?

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.

Yes, using query context is better. It has to be thread safe. Alexey's mutex works in that way. I agree with his change.
Maybe I would only hide internals details like mutex inside container/class for table_function_results.

@alexey-milovidov alexey-milovidov 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.

Looks good.

@alexey-milovidov alexey-milovidov self-assigned this Jan 15, 2026
@azat
azat marked this pull request as draft January 15, 2026 20:54
@novikd novikd self-assigned this Jan 15, 2026
@azat azat closed this Jan 16, 2026
@azat
azat deleted the mv-analyzer-resolve-race-v2 branch January 16, 2026 12:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants