Skip to content

Backport #100396 to 25.8: Fix TSan data race in Keeper AuthID between preprocess and commit - #108965

Merged
alexey-milovidov merged 2 commits into
25.8from
backport/25.8/100396
Jul 1, 2026
Merged

alexey-milovidov merged 2 commits into
25.8from
backport/25.8/100396

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Backport of #100396 (commit 15ad49a97fd9) to the 25.8 release branch.

The AddAuthDelta stores a shared_ptr<AuthID> that is shared between the delta and the uncommitted state. During commit, the code was move-constructing from this shared AuthID via std::move(*operation.auth_id), which could race with a concurrent preprocess call reading the same AuthID through uncommitted_state.session_and_auth without holding auth_mutex. The fix copies instead of moving.

The fix landed on master on 2026-03-24 but was never backported, so 25.8 still has the racy std::move(*operation.auth_id) in KeeperStorage::commit. ThreadSanitizer keeps reporting this data race in the Keeper instance during integration test teardown on 25.8 and on backport PRs built on it.

For example, on the 25.8 backport PR #108917 the only CI failure is exactly this race, detected at the shutdown of the unrelated test_polymorphic_parts cluster:

WARNING: ThreadSanitizer: data race
  Read of size 1 ... DB::KeeperStorageBase::AuthID::operator==
    ... DB::preprocess<...>(Coordination::ZooKeeperAuthRequest const&, ...)
  Previous write of size 8 ... DB::KeeperStorageBase::AuthID::AuthID(AuthID&&)
    ... DB::KeeperStorage<...>::commit(...)::operator()<DB::AddAuthDelta>

Related issue: #82719.

Changelog category (leave one):

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

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

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 25.8.26.6

The `AddAuthDelta` stores a `shared_ptr<AuthID>` that is shared between
the delta and the uncommitted state. During `commit`, the code was
move-constructing from this shared `AuthID` via `std::move(*operation.auth_id)`,
which could race with a concurrent `preprocess` call reading the same
`AuthID` through `uncommitted_state.session_and_auth` without holding
`auth_mutex`.

Fix by copying instead of moving.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=3134af4b1eeebf8773015eb04eb1aa1a80bd2f17&name_0=MasterCI&name_1=Integration%20tests%20%28amd_tsan%2C%206%2F6%29

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
(cherry picked from commit 15ad49a)
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [BackportPR], commit [d3c675a]

@alexey-milovidov

Copy link
Copy Markdown
Member Author

continue-pr status

The change is correct and minimal — a one-line backport of #100396: copy instead of std::move the shared AuthID in KeeperStorage::commit for AddAuthDelta, so a concurrent preprocess reading it without auth_mutex no longer races. The integration amd_tsan shards (which is exactly where the original race fired, at test_polymorphic_parts teardown) are passing in the current run — the fix does what it intends.

The only red is an unrelated, known CI-infra flake, not caused by this PR:

  • Failure: Install packages (amd_debug) → Run server init.d. The init.d wrapper prints Server started (it only checks the pid file), then clickhouse-client -q 'SELECT version()' is run once and gets Code: 210. DB::NetException: Connection refused (localhost:9000) because the slow amd_debug server has not yet opened the TCP listener.
  • In the same job, the sibling binary_test exercises the identical server start but retries SELECT version() 5×; it gets Connection refused for ~4 s before succeeding on attempt 4 — proving the server is healthy and just slow to bind 9000, and that initd_test's lack of a retry loop is the sole cause.
  • This is the same failure mode as CI: Run server init.d failed #86278, fixed on master by Extend Run server init.d retry budget from 5s to 30s #106056 ("Extend Run server init.d retry budget from 5s to 30s"), which was never backported to 25.8 — so it keeps blocking 25.8 backport PRs (this one and Backport #107119 to 25.8: Fix flaky test_async_load_databases::test_materialized_views_replicated #108917). The change here only touches src/Coordination/KeeperStorage.cpp, which is not exercised by a plain server start + SELECT version().

Fix for the flake: I opened #108987 to backport the init.d retry loop to 25.8 (a clean cherry-pick of #106056 is not possible because the 25.8 initd_test predates the retry loop entirely; #108987 adapts the same fix). On the slow amd_debug build the init.d test fails essentially deterministically without a retry, so re-running this job alone is unlikely to go green — the reliable path is to land #108987 on 25.8 and then re-merge 25.8 into this PR (or merge this PR accepting the known-unrelated install flake, since the amd_tsan integration tests already validate the actual fix).

I did not re-run the failed install job (the overall run is still in progress so GitHub refuses) and did not re-merge 25.8 (merge-base is only a few hours old, and a re-merge would not pick up #108987 until it lands).

@alexey-milovidov

Copy link
Copy Markdown
Member Author

continue-pr update

The blocker is now resolved. The sole red on the previous head (6932922e) was Install packages (amd_debug) → Run server init.d failing with Code:210 Connection refused (localhost:9000) — the known init.d timing flake #86278, where the 25.8 initd_test had no retry loop and probed a slow amd_debug server exactly once before it bound port 9000. That is unrelated to this backport (it changes only src/Coordination/KeeperStorage.cpp).

The fix for that flake, #108987 (backport of the init.d retry loop to 25.8), was merged into 25.8 as 12ebc553175. I merged current origin/25.8 into this branch (6932922e..d3c675a6369) so it now includes the retry loop; the Install packages (amd_debug) job should go green on re-run.

The backport itself is unchanged — still the one-line copy-instead-of-std::move of the shared AuthID in KeeperStorage::commit for AddAuthDelta. No conflicts, no drift on the changed file. MERGEABLE, no unresolved review threads.

@alexey-milovidov
alexey-milovidov merged commit 1e7f412 into 25.8 Jul 1, 2026
33 checks passed
@alexey-milovidov
alexey-milovidov deleted the backport/25.8/100396 branch July 1, 2026 05:43
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
The `initd_test` script in `ci/jobs/install_check.py` on `25.8` runs
`clickhouse-client -q 'SELECT version()'` exactly once, immediately after
the init.d wrapper prints `Server started`. The wrapper only checks that
the pid file exists, but on a slow CI host (notably the `amd_debug` build)
the server can take several seconds longer to open the TCP listener on
port 9000. The test then fails with
`Code: 210. DB::NetException: Connection refused (localhost:9000)`.

In the same job the sibling `binary_test` already retries `SELECT version()`
in a loop, and on `25.8` backport runs it routinely takes ~4 seconds (4
retries) before the debug server accepts connections, while `initd_test`
gives up after the first attempt.

This is the same failure mode as ClickHouse#86278, which was fixed on `master` by
ClickHouse#106056 ("Extend `Run server init.d` retry budget from 5s to 30s"). That
fix never reached `25.8`, so the flake keeps blocking `25.8` backport PRs
(for example ClickHouse#108965 and ClickHouse#108917). Because the `25.8` `initd_test` predates
the retry loop entirely, ClickHouse#106056 does not cherry-pick cleanly; this commit
adapts the same fix by adding a 30-iteration poll loop that breaks early on
success, matching the existing `binary_test` and `keeper_test` patterns.

`SYSTEMCTL_SKIP_REDIRECT` is intentionally not added, since the `25.8`
init.d script does not support it.

Related: ClickHouse#86278, ClickHouse#106056

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant