Skip to content

Fix TSan data race on NuRaft peer config_ - #107429

Closed
alexey-milovidov wants to merge 2 commits into
masterfrom
fix-nuraft-peer-config-race
Closed

alexey-milovidov wants to merge 2 commits into
masterfrom
fix-nuraft-peer-config-race

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

Related: ClickHouse/NuRaft#119

Bumps the NuRaft submodule to fix a ThreadSanitizer data race on nuraft::srv_config::get_id, reported by the amd_tsan integration test test_keeper_4lw_reconfiguration::test.py::test_precondition_failure on master.

The peer::config_ member is protected by config_mutex_ and is meant to be read only through the locked accessors. Several call sites in peer.cxx still read config_->get_id directly, bypassing the mutex. ThreadSanitizer caught a race between the unlocked read in peer::handle_rpc_result (an asio RPC worker thread) and the construction of a fresh srv_config published via peer::set_config while cluster_config::deserialize runs inside raft_server::commit_conf on the commit thread. The fix routes those reads through the locked get_id accessor.

This is a rare race: 1 failure in 144 master runs on the day it surfaced, and 0 in the preceding 30 days. No existing tracking issue was found.

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

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):

Fix a ThreadSanitizer data race on the per-peer config in the embedded Raft library (Keeper).

Bump the `NuRaft` submodule to pick up a fix for a ThreadSanitizer data
race on `nuraft::srv_config::get_id`. It was reported by the `amd_tsan`
integration test
`test_keeper_4lw_reconfiguration::test.py::test_precondition_failure` on
master.

The `peer::config_` member is protected by `config_mutex_` and should be
read only through the locked accessors, but several call sites in
`peer.cxx` read `config_->get_id` directly. ThreadSanitizer caught a
race between the unlocked read in `peer::handle_rpc_result` (an asio RPC
worker) and the construction of a fresh `srv_config` published via
`peer::set_config` while `cluster_config::deserialize` runs inside
`raft_server::commit_conf` on the commit thread.

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

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

clickhouse-gh Bot commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [66a87ef]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_tsan, s3 storage, parallel, 1/2) FAIL
03560_parallel_replicas_memory_bound_merging_projection FAIL cidb
Integration tests (amd_asan_ubsan, db disk, old analyzer, 3/6) FAIL
test_dictionaries_all_layouts_separate_sources/test_mongo.py::test_simple_ssl[cache-True] FAIL cidb

AI Review

Summary
  • This PR bumps contrib/NuRaft from c3d2e767add5 to 131671cbfb7c to pick up the upstream fix for a ThreadSanitizer race on peer::config_. The old submodule-reachability issue is fixed because the new target is now reachable from NuRaft master, but the selected upstream revision still leaves another unlocked peer::config_ read in the same reconfiguration surface.
Findings
  • ⚠️ Majors
  • [contrib/NuRaft:1] The NuRaft bump does not fully preserve the promised invariant that peer::config_ is read only through locked accessors. The upstream patch replaces direct config_->get_id reads in peer.cxx, but include/libnuraft/peer.hxx:114 still implements peer::is_new_joiner as an unlocked return config_->is_new_joiner();. Vote and append paths call it through raft_server::is_regular_member without peer::lock_ (for example src/handle_vote.cxx:53 and src/handle_append_entries.cxx:1801), while config commit can concurrently run pp->set_config(entry) at src/handle_commit.cxx:1171. Since ptr is std::shared_ptr, the unlocked read can still race with assignment to the same config_ member.
    Suggested fix: update the upstream NuRaft fix so peer::is_new_joiner also locks or copies under config_mutex_, then bump the submodule to that commit.
Tests
  • ⚠️ The direct Praktika report for commit 66a87ef6ab08d65dfc9b94686bedd0eb50a62a7d currently reports zero test results, so it does not prove the race fix. A ThreadSanitizer run of the affected Keeper reconfiguration integration test, or a smaller focused concurrency regression that exercises peer::set_config against all peer::config_ accessors, would close this gap.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required action: update the NuRaft submodule to an upstream revision that protects the remaining peer::is_new_joiner access to peer::config_, and provide ThreadSanitizer evidence for the affected reconfiguration path.

@clickhouse-gh clickhouse-gh Bot added pr-ci submodule changed At least one submodule changed in this PR. labels Jun 14, 2026
Comment thread contrib/NuRaft Resolved
NuRaft PR #119 has been merged. Move the submodule pointer from the
fix commit to the fork's `master` tip (the merge commit), so it tracks
master rather than a feature branch.

ClickHouse/NuRaft#119

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Comment thread contrib/NuRaft
@@ -1 +1 @@
Subproject commit c3d2e767add589ef4276577cd89df969649fdf4f
Subproject commit 131671cbfb7c3568ce2b9300c35e945833a164d0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This NuRaft revision still leaves the same peer::config_ race on another accessor. The patch replaces direct config_->get_id reads in peer.cxx, but include/libnuraft/peer.hxx:114 still implements peer::is_new_joiner as an unlocked return config_->is_new_joiner();. That accessor is reached from raft_server::is_regular_member in vote/append paths without peer::lock_ (for example src/handle_vote.cxx:53 and src/handle_append_entries.cxx:1801), while config commit can concurrently run pp->set_config(entry) at src/handle_commit.cxx:1171. Since ptr is std::shared_ptr, that unlocked read races with the assignment to the same config_ member, so this bump does not fully satisfy the "all peer::config_ reads go through locked accessors" contract. Please update the upstream fix so peer::is_new_joiner also locks or copies under config_mutex_ before bumping the submodule.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, fix 03560_parallel_replicas_memory_bound_merging_projection and test_dictionaries_all_layouts_separate_sources/test_mongo.py::test_simple_ssl

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov done on the first one; the second is a separate (rare) issue.

03560_parallel_replicas_memory_bound_merging_projection - flaky, fix in #107444.
Fails with Code 160 TOO_SLOW: Estimated query execution time (1198.32 seconds) is too long. Maximum: 600 in ReadPoolParallelReplicasInOrder. The test env sets max_estimated_execution_time=600; under randomized settings + slow storage the parallel-replicas in-order read starts slowly, so ExecutionSpeedLimits::throttle over-extrapolates and aborts. The clickhouse-test minimizer confirms it (28/28 pass without randomization). This is a plan/result test, not a perf test, so #107444 sets max_estimated_execution_time=0 for it. 0 master failures in 30d; 3 unrelated Stateless PRs.

test_mongo.py::test_simple_ssl[cache-True] - a NEW signature, distinct from the config-isolation fix in #103837. It is a LeakSanitizer leak inside the vendored contrib/mongo-c-driver (rev a26c8e251fec), not in our code:

Direct leak of 128 byte(s):
  bson_malloc  mongo-c-driver/src/libbson/src/bson/memory.c:106
  bson_copy_to mongo-c-driver/src/libbson/src/bson/bson.c:2106
  _mongoc_cluster_run_opmsg_recv mongoc-cluster.c:3313  (bson_copy_to(&body, reply))

reached via MongoDBSource::generate() -> FlatDictionary::loadData(), surfaced at cluster.shutdown(). Very rare: 2 hits / 2 PRs / 0 master in 30d, so it reads as a shutdown-timing artifact in the driver rather than a steady leak. I do not have a local reproduction yet (needs the ASAN build + the dockerized Mongo dictionary cluster).

For the mongo one, which would you prefer:

  1. a narrow LSan suppression following the existing OpenSSL pattern in base/base/sanitizer_options.h (e.g. leak:_mongoc_cluster_run_opmsg_recv), or
  2. I dig into the mongo-c-driver cursor/reply teardown for the actual missing bson_destroy and propose a submodule patch.

(1) is low-risk and immediate; (2) is the real fix but slower and touches a submodule. I will take whichever you want.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, We have to fix it, even if it is a bug inside the Mongo C library.

@groeneai

Copy link
Copy Markdown
Collaborator

We have to fix it, even if it is a bug inside the Mongo C library.

Done. The test_simple_ssl LeakSanitizer failure is a real leak in the bundled mongo-c-driver, fixed at the source:

Root cause: _mongoc_retryable_cmd_run reuses *reply across retry attempts. After a retryable read error it calls select_retry_server(..., reply, ...) while reply still owns the previous attempt's bson_copy_to allocation; that function overwrites reply as an out-param (via _mongoc_cluster_stream_for_optype) without freeing the prior contents, and the loop's own bson_destroy(reply) is after the !server_description break, so it is skipped when selection fails. The 128 bytes leak. Fix destroys+reinits reply before select_retry_server. Trigger (retryable read error then retry-selection failure) is a rare failover race (2 hits / 30 days, 0 on master).

Verified both directions with a standalone ASan/LSan harness driving the real _mongoc_retryable_cmd_run: leak reported without the fix, clean with it.

@antonio2368 antonio2368 self-assigned this Jun 15, 2026
@antonio2368

Copy link
Copy Markdown
Member

Included in #107338

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-ci submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants