Skip to content

Fix data race between BackgroundJobsAssignee::start and finish - #99366

Merged
alexey-milovidov merged 2 commits into
masterfrom
fix-tsan-background-jobs-assignee
Mar 16, 2026
Merged

alexey-milovidov merged 2 commits into
masterfrom
fix-tsan-background-jobs-assignee

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 13, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Fixed a TSan-detected data race in BackgroundJobsAssignee between start() and finish() operating on the holder member concurrently
  • finish() was reading holder without holder_mutex to avoid a deadlock with deactivate() waiting for threadFunc which locks the same mutex
  • Fix: move holder into a local variable under the lock, release the lock, then call deactivate() on the local copy — avoids both the race and the deadlock

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=83c801db6fb5f4cec6b47e8d3c73ac0b573db39f&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

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

...

🤖 Generated with Claude Code


Note

Medium Risk
Touches MergeTree background scheduling teardown logic and locking; while small, mistakes here could cause hangs or leaked background tasks under concurrency.

Overview
Fixes a concurrency issue in BackgroundJobsAssignee::finish() by taking ownership of the scheduled task holder under holder_mutex (via move to a local) and calling deactivate() only after releasing the mutex.

This removes the prior unlocked access to holder (TSan race with start()) while also preventing a potential deadlock where deactivate() waits for threadFunc() that can re-enter trigger()/postpone() and lock holder_mutex.

Written by Cursor Bugbot for commit 740fb11. This will update automatically on new commits. Configure here.

Version info

  • Merged into: 26.3.1.723 (included in 26.3 and later)
  • Backported to: 25.8.31.10

`finish()` was reading `holder` without `holder_mutex`, while `start()`
writes to it under the lock. The comment said the lock cannot be held
because `deactivate()` waits for `threadFunc` which calls
`trigger()`/`postpone()` that also lock `holder_mutex` — a deadlock.

Fix: move `holder` into a local variable under the lock, release the
lock, then call `deactivate()` on the local copy. This avoids both
the data race and the deadlock.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=83c801db6fb5f4cec6b47e8d3c73ac0b573db39f&name_0=MasterCI&name_1=Stress%20test%20%28azure%2C%20amd_tsan%29

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Mar 13, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c928cd9]

Summary: ✅


AI Review

Summary

This PR fixes a real TSan race in BackgroundJobsAssignee::finish by moving holder under holder_mutex into a local and calling deactivate after unlocking, which also avoids the lock-order deadlock risk documented in the code. I did not find blocker or major correctness/safety/performance issues in the patch.

ClickHouse Rules

Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsHistory.cpp ➖
Safe rollout ✅
Compilation time ✅

Final Verdict

  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Mar 13, 2026

@alexey-milovidov alexey-milovidov left a comment

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.

Sounds reasonable.

@alexey-milovidov alexey-milovidov self-assigned this Mar 16, 2026
@clickhouse-gh

clickhouse-gh Bot commented Mar 16, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 83.70% 83.70% +0.00%
Functions 23.90% 23.90% +0.00%
Branches 76.20% 76.20% +0.00%

PR changed lines: PR changed-lines coverage: 100.00% (11/11, 0 noise lines excluded)
Diff coverage report
Uncovered code

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Mar 16, 2026
Merged via the queue into master with commit d47bed6 Mar 16, 2026
159 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-tsan-background-jobs-assignee branch March 16, 2026 21:15
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 16, 2026
@alesapin alesapin added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Aug 11, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Aug 11, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Aug 11, 2026
alexey-milovidov added a commit that referenced this pull request Aug 19, 2026
Backport #99366 to 25.8: Fix data race between BackgroundJobsAssignee::start and finish
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR 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.

5 participants