Skip to content

Log the initial disk check period before scheduling the disk check task - #110783

Merged
alexey-milovidov merged 4 commits into
masterfrom
fix-disklocalcheckthread-startup-race
Jul 27, 2026
Merged

alexey-milovidov merged 4 commits into
masterfrom
fix-disklocalcheckthread-startup-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 17, 2026 •

Copy link
Copy Markdown
Member

Description

Log the initially configured disk check period before scheduling the periodic task in DiskLocalCheckThread::startup, instead of after it.

History and current status of this change. This PR was originally opened as a fix for a ThreadSanitizer data race: startup called task->activateAndSchedule() first and only afterwards read check_period (a DynamicDelay, at that time documented as "Not thread safe") on the main thread to format the Disk check ... started with period ... message. Once scheduled, run could write check_period (rotateToMin / up) on a BackgroundSchedulePool thread concurrently with that read. Because the abort happened at server startup, it surfaced as a Start ClickHouse Server / Server died failure in the Stateless tests (amd_tsan, flaky check) job:

WARNING: ThreadSanitizer: data race
  Write ... DB::DynamicDelay::rotateToMin() src/Common/DynamicDelay.cpp:69
    DB::DiskLocalCheckThread::run() src/Disks/DiskLocalCheckThread.cpp:55
  Previous read ... DB::DynamicDelay::getCurrentDelay() src/Common/DynamicDelay.cpp:49
    DB::DiskLocalCheckThread::startup() src/Disks/DiskLocalCheckThread.cpp:46
SUMMARY: ThreadSanitizer: data race src/Common/DynamicDelay.cpp:69:11 in DB::DynamicDelay::rotateToMin()

Observed in CI on PR #109369: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=109369&sha=7b341f536dbac2c97b55196c58066093ef980bd3&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20flaky%20check%29

Meanwhile #110817 made DynamicDelay itself thread safe (all accessors take a mutex, the class comment flipped from "Not thread safe" to "Thread safe"), which removes that race at its source. So on the current merge result this diff no longer fixes a data race — it is a two-line ordering cleanup: the log message reports the initial period, so reading it before the task can start is the natural order and keeps the read out of the concurrency window regardless of DynamicDelay's internal synchronization. The logged value is unchanged. Merge it as a small cleanup or close it — it is no longer load-bearing.

Related: #110817
Related: #110773

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.8.1.203 (included in 26.8 and later)

`DiskLocalCheckThread::startup` scheduled the periodic check task via
`task->activateAndSchedule()` and only afterwards read `check_period`
(a `DynamicDelay`, documented "Not thread safe") on the main thread to
format the "Disk check ... started with period ..." log message. Once
the task is scheduled, `run` may start executing on a `BackgroundSchedulePool`
thread and write `check_period` (`rotateToMin`/`up`) concurrently with that
read, so the two race.

ThreadSanitizer reports this as a data race between the main-thread read in
`DynamicDelay::getCurrentDelay` (from `startup`) and the pool-thread write in
`DynamicDelay::rotateToMin` (from `run`), which aborts server startup under
the `Stateless tests (amd_tsan, flaky check)` job:

  WARNING: ThreadSanitizer: data race
    Write ... DB::DynamicDelay::rotateToMin() src/Common/DynamicDelay.cpp:69
      DB::DiskLocalCheckThread::run() src/Disks/DiskLocalCheckThread.cpp:55
    Previous read ... DB::DynamicDelay::getCurrentDelay() src/Common/DynamicDelay.cpp:49
      DB::DiskLocalCheckThread::startup() src/Disks/DiskLocalCheckThread.cpp:46

Read the delay for the log message before scheduling the task. That orders
the main-thread read before the task can run, and the task thereafter runs
only on the schedule pool (serialized with itself) until `deactivate` in
`shutdown`/the destructor waits for it, so `check_period` has a single
accessor at any time. The logged value is unchanged (the initially
configured period).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [7911b2e]

Summary: ✅


AI Review

Summary

This PR now just reorders DiskLocalCheckThread::startup so the Disk check ... started with period ... message reads the initial check_period before activateAndSchedule(). The original TSan rationale is obsolete because DynamicDelay is already mutex-protected, but in the current code I do not see an outstanding correctness, safety, compatibility, or testing issue from the remaining one-line cleanup.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 17, 2026
Comment thread src/Disks/DiskLocalCheckThread.cpp Resolved
@alexey-milovidov

Copy link
Copy Markdown
Member Author

The sole CI failure — Error message in clickhouse-server.log (see upgrade_error_messages.txt) in Upgrade check (amd_release) — is unrelated to this PR: it is the pre-existing benign clickhouse_disk_checker_* errno 2 <Error> log line from the disk-checker teardown race at server shutdown, which currently hits many PRs in the Upgrade check (e.g. #109369, #110606, #107236). @groeneai, the fix is already in progress in a separate PR: #110773 (allow-listing the message in upgrade_runner.sh).

The startup data race this PR fixes is validated by this run: all amd_tsan jobs are green and the Start ClickHouse Server abort no longer occurs.

Comment thread src/Disks/DiskLocalCheckThread.cpp Resolved
@alexey-milovidov alexey-milovidov changed the title Fix data race in DiskLocalCheckThread::startup Log the initial disk check period before scheduling the disk check task Jul 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.30% 86.20% -0.10%
Functions 92.00% 92.00% +0.00%
Branches 78.40% 78.40% +0.00%

Changed lines: Changed C/C++ lines covered: 2/2 (100.00%) · Uncovered code

Full report · Diff report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 27, 2026
Merged via the queue into master with commit ecb9cf7 Jul 27, 2026
180 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-disklocalcheckthread-startup-race branch July 27, 2026 10:42
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci 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.

3 participants