Repository navigation
Make RestorerFromBackup use its own context copy, not the live query context - #108765
Conversation
…context Background `RESTORE` workers (`createDatabase`/`createTable`, running on the `RESTORE` thread pool) copied the live query context via `Context::createCopy(query_context)`. For a `RESTORE ASYNC` query the originating thread can still touch that same `Context` after the query has returned (for example the server-side AST fuzzer resets `merge_tree_transaction` in its finish callback), so the background copy reads `merge_tree_transaction` under `Context::mutex` while that thread writes it -- the TSAN data race seen on `BuzzHouse (amd_tsan)` (STID 3336-2c6d). `RestoreStarter` already creates a dedicated `restore_context` copy that the restorer holds as `context`, precisely so background work does not depend on the live query context. Switch the remaining `query_context` uses in `RestorerFromBackup` (`createDatabase`, `createTable`, `checkTable`, `checkAccessForObjectsFoundInBackup`) over to `context`, and drop the now-unused `query_context` member and constructor/`doRestore` parameter. This resolves the long-standing `TODO: Get rid of using query_context in class RestorerFromBackup` and the root cause raised in review on #103158 (background backup/restore tasks must not share a `Context` that another query can mutate concurrently). Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Workflow [PR], commit [024f574] Summary: ✅
AI ReviewSummaryThis PR moves Final Verdict✅ Approve |
Switching `RestorerFromBackup` off the live `query_context` regressed
`03315_query_log_privileges_backup_restore` (Fast test): the restore
worker now runs on `restore_context`, but `RestoreStarter` calls
`makeQueryContext` on that copy, which resets it to a fresh, empty
`QueryPrivilegesInfo`. As a result the access checks performed during the
restore (`checkAccessForObjectsFoundInBackup` and the `TABLE ENGINE` check
when creating tables from the backup) were recorded into the restore
context's own privileges object, while `system.query_log` reads the
privileges from the original query context, so the restore query's
`used_privileges`/`missing_privileges` columns became empty:
-[] ['INSERT, CREATE TABLE ON default.d_03315_query_log']
-['TABLE ENGINE ON MergeTree'] []
+[] []
+[] []
Share the `QueryPrivilegesInfo` object of the original query context with
`restore_context` via a new `Context::setQueryPrivilegesInfo` helper, so the
privileges checked while restoring are accounted to the RESTORE query in
`system.query_log`, restoring the pre-change behavior. `QueryPrivilegesInfo`
has its own mutex and is not one of the context fields a concurrent
originating thread mutates, so sharing it does not reintroduce the data race
that moving the background workers off the live query context avoids.
CI report: #108765
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Status update (continue-pr review of This PR is green except for 3 stress jobs, and all three are the known hung-check flake (#107941), unrelated to this change. I downloaded and inspected each
None of the three contain a Notably, the job that originally caught the race this PR fixes — BuzzHouse (amd_tsan) — is now green, along with all other functional / integration / AST-fuzzer / unit jobs. AI Review is ✅ Approve and there are no unresolved review threads. Not re-merging master: the merge-base is from 2026-06-29 23:39 UTC (< 1 day old) and the red checks are a known flake that a re-merge would not fix. No code change required. Ready to merge pending a stress re-run or a maintainer override of the known hung-check flake. |
|
Status update (continue-pr, Two distinct red signals; one was a real build break that I fixed, the other is a known flake. 1.
|
|
📊 Cloud Performance Report ✅ AI verdict: This PR only changes backup/RESTORE code paths — it drops the redundant query_context from the restorer classes and shares privilege accounting so RESTORE access checks show up in query_log. None of that touches the query-execution hot path a ClickBench SELECT exercises, so the flagged -14.23% (Q4) and -10.55% (Q5) improvements cannot plausibly be caused by this change; both are downgraded to not-sure as off-path run-to-run variance (Q4's baseline was also fairly noisy). No genuine, PR-attributable regressions or improvements were found. clickbenchFlagged queries (2 of 43)
q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Status update (continue-pr,
|
|
Status update (continue-pr,
|
|
Status update (continue-pr) — re-merged Re-merge (
|
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 51/53 (96.23%) · Uncovered code |
|
Status update (continue-pr, CI on
|
|
Status update (continue-pr, Following up on the previous note ("ready to merge once the re-run clears the flakes"): that condition is now satisfied. The full status rollup is:
|
Re-merge master to pick up #108765 (RestorerFromBackup uses its own context copy), which fixes the chronic transaction-context data race `ThreadSanitizer: data race (STID: 3336-2c6d)` (tracking issue #101319) that was the sole public test failure (Stress test amd_tsan) on the prior head. The branch was behind and red, so re-merging is warranted.
Switching `RestorerFromBackup` off the live `query_context` regressed
`03315_query_log_privileges_backup_restore` (Fast test): the restore
worker now runs on `restore_context`, but `RestoreStarter` calls
`makeQueryContext` on that copy, which resets it to a fresh, empty
`QueryPrivilegesInfo`. As a result the access checks performed during the
restore (`checkAccessForObjectsFoundInBackup` and the `TABLE ENGINE` check
when creating tables from the backup) were recorded into the restore
context's own privileges object, while `system.query_log` reads the
privileges from the original query context, so the restore query's
`used_privileges`/`missing_privileges` columns became empty:
-[] ['INSERT, CREATE TABLE ON default.d_03315_query_log']
-['TABLE ENGINE ON MergeTree'] []
+[] []
+[] []
Share the `QueryPrivilegesInfo` object of the original query context with
`restore_context` via a new `Context::setQueryPrivilegesInfo` helper, so the
privileges checked while restoring are accounted to the RESTORE query in
`system.query_log`, restoring the pre-change behavior. `QueryPrivilegesInfo`
has its own mutex and is not one of the context fields a concurrent
originating thread mutates, so sharing it does not reintroduce the data race
that moving the background workers off the live query context avoids.
CI report: ClickHouse#108765
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…-own-context Make RestorerFromBackup use its own context copy, not the live query context
Changelog category (leave one):
Documentation entry for user-facing changes
Not applicable — internal backup/restore robustness change.
What & why
Background
RESTOREworkers (RestorerFromBackup::createDatabase/createTable, scheduled on theRESTOREthread pool) copied the live query context viaContext::createCopy(query_context). For aRESTORE ASYNCquery the originating thread can still touch that sameContextafter the query has returned — most notably the server-side AST fuzzer (executeASTFuzzerQueries), which resetsmerge_tree_transactionin its query-finish callback. The background copy then readsmerge_tree_transactionunderContext::mutexwhile the originating thread writes it unsynchronized → the TSAN data race seen onBuzzHouse (amd_tsan):Fix
RestoreStarteralready creates a dedicatedrestore_contextcopy that the restorer holds ascontext, precisely so background work does not depend on the live query context. This PR switches the remainingquery_contextuses inRestorerFromBackup(createDatabase,createTable,checkTable,checkAccessForObjectsFoundInBackup) over to thatcontext, and drops the now-unusedquery_contextmember and constructor/doRestoreparameter.This resolves the long-standing in-code
TODO: Get rid of using query_context in class RestorerFromBackupand the root cause @tavplubix asked for in review on #103158 ("async tasks shouldn't share the same context that can be used concurrently by other queries").contextis a copy made when the restore started and is only read on the worker threads, so concurrentContext::createCopy(context)calls are safe.Relationship to #103158
This is the root-cause / read-side follow-up to #103158, which fixes the write side in the AST fuzzer (and skips fuzzing
BACKUP/RESTORE). The two are complementary: with this change in place the background restore never touches the live query context regardless of what the originating thread does to it, so the same class of race cannot reappear from other post-query mutations. #103158 remains valuable on its own (the fuzzer should not clobber the caller's transaction).No new test: the symptom is a sanitizer-only data race with no deterministic functional repro (same limitation noted on #103158); the
BuzzHouse/TSAN jobs provide coverage.Evidence on master
This race is not BuzzHouse-only — it also fired on the regular
Stress test (amd_tsan)for amastercommit (sameSTID 3336-2c6d, write sideexecuteASTFuzzerQueries->setCurrentTransaction, read sideRestorerFromBackup::createTable->Context::createCopy):https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=75154b382fe8161e14ef5c505b251ebb876d19a8&name_0=MasterCI&name_1=Stress%20test%20%28amd_tsan%29
(The remaining red
Stress test (amd_tsan)/(arm_tsan)jobs on this PR are an unrelatedHung checkflake that currently hits nearly everymastertsan stress run since 2026-06-29; the data raceSTID 3336-2c6dno longer appears in this PR's runs.)🤖 Generated with Claude Code
Version info
26.7.1.364(included in26.7and later)