Skip to content

Fix data race in LocalServer - #62309

Merged
evillique merged 2 commits into
ClickHouse:masterfrom
evillique:fix-data-race-in-context
Apr 29, 2024
Merged

evillique merged 2 commits into
ClickHouse:masterfrom
evillique:fix-data-race-in-context

Conversation

@evillique

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes to CHANGELOG.md):

Closes #62025.

@robot-ch-test-poll1 robot-ch-test-poll1 added the pr-not-for-changelog This PR should not be mentioned in the changelog label Apr 4, 2024
@robot-ch-test-poll1

robot-ch-test-poll1 commented Apr 4, 2024 •

Copy link
Copy Markdown
Contributor

This is an automated comment for commit 8f06a1a with description of existing statuses. It's updated for the latest CI running

❌ Click here to open a full report in a separate page

Check nameDescriptionStatus
A SyncThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS⏳ pending
CI runningA meta-check that indicates the running CI. Normally, it's in success or pending state. The failed status indicates some problems with the PR⏳ pending
Mergeable CheckChecks if all other necessary checks are successful❌ failure
Successful checks
Check nameDescriptionStatus
Docs checkBuilds and tests the documentation✅ success
Fast testNormally this is the first check that is ran for a PR. It builds ClickHouse and runs most of stateless functional tests, omitting some. If it fails, further checks are not started until it is fixed. Look at the report to see which tests fail, then reproduce the failure locally as described here✅ success
PR CheckThere's no description for the check yet, please add it to tests/ci/ci_config.py:CHECK_DESCRIPTIONS✅ success
Style checkRuns a set of checks to keep the code style clean. If some of tests failed, see the related log from the report✅ success

@Avogar Avogar self-assigned this Apr 5, 2024
Comment thread src/Interpreters/Context.cpp Outdated

@antonio2368 antonio2368 Apr 5, 2024 •

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.

why not just

SharedLockGuard lock(&mutex_: mutex);

UPD: ah sorry @Avogar didn't notice you assigned yourself 😓

@Avogar Avogar Apr 5, 2024 •

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.

I actually have another question, data race is between thread in clickhouse-local that modifies global context settings after it’s initialisation and some background thread in MergeTree. This sounds not right, background threads should be started after all modifications and initialisations in global context are done. So maybe there is a bug in clickhouse-local with initialisation order

@Avogar

Avogar commented Apr 5, 2024

Copy link
Copy Markdown
Member

The actual problem is that adjustSettings in LocalServer happens when we already started async tables loading that can use global context. I think we should call adjustSettings in applySettingsOverridesForLocal because it happens before tables loading is started

@Avogar

Avogar commented Apr 24, 2024

Copy link
Copy Markdown
Member

@evillique will you make a proper fix I described? If you don't have time, I can do it

@evillique

Copy link
Copy Markdown
Member Author

@Avogar Sorry, I got distracted and forgot about this PR. I will make a proper fix

@evillique
evillique force-pushed the fix-data-race-in-context branch from 6aad90d to bf3d33b Compare April 24, 2024 21:09
@evillique evillique changed the title Fix data race in Context Fix data race in LocalServer Apr 24, 2024
@evillique
evillique enabled auto-merge April 29, 2024 14:38
@evillique
evillique added this pull request to the merge queue Apr 29, 2024
Merged via the queue into ClickHouse:master with commit 1892598 Apr 29, 2024
@evillique
evillique deleted the fix-data-race-in-context branch April 29, 2024 14:56
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Apr 29, 2024
clickgapai pushed a commit to clickgapai/ClickHouse that referenced this pull request Oct 1, 2026
Context::getReadSettings() read about 130 settings through getSettingsRef(),
an unsynchronized inline accessor, while every writer of those fields holds
Context::mutex exclusively. That is a missing reader-side lock, not an unlocked
writer. Context::getWriteSettings() had the identical shape, and the four query
throttler getters read their bandwidth setting the same way.

Two of the settings on that path (local_filesystem_read_method,
remote_filesystem_read_method) are SettingFieldString, and the getter holds a
std::string_view into the heap String while a concurrent assignment can
reallocate it, so the consequence is worse than a torn integer. A background
thread has no query context, so free getReadSettings() resolves to the global
context - the documented system_profile path for background work - which is the
object a settings change mutates. The report this comes from was a Keeper
changelog reader racing setDefaultProfiles at server startup.

setDefaultProfiles() was the other half: applySettingsQuirks,
adjustSettingsForMakeDistributedPlan and doSettingsSanityCheckClamp ran after
setCurrentProfile had released the mutex, so locking only the reader would have
excluded nothing.

Both getters now take a shared lock over the settings extraction and release it
before the cache and throttler getters, which take the same mutex exclusively;
ContextSharedMutex cannot be upgraded, so the release is required rather than
stylistic. The throttler getters' own bandwidth reads are synchronized the same
way, and setDefaultProfiles' trailing mutations move under the exclusive lock.
There is no behavior change: the effective settings a server reports are
byte-identical before and after.

Scope is deliberately narrow. Still unsynchronized and not addressed here: the
five checkSettingsConstraints/clampToSettingsConstraints overloads that reach
doSettingsSanityCheckClamp holding only a shared lock, reloadClusterConfig(),
getBackupsThrottler(), and nine call sites outside Context that read the global
settings directly.

Issue ClickHouse#62025 reports this same race on getReadSettings, also on a String
setting. It was closed by ClickHouse#62309, which reordered one clickhouse-local caller
and left the accessor itself unsynchronized.

The added test only carries signal under ThreadSanitizer: it reports the race on
the unpatched tree in 3 of 3 runs and is silent in 5 of 5 runs with the fix.
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 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.

Data race on Context::setSettings & Context::getReadSettings

5 participants