Skip to content

Add regression test for ReplicatedAccessStorage race that evicted a just-created entity (UNKNOWN_ROLE) - #106525

Merged
fm4v merged 11 commits into
ClickHouse:masterfrom
Slach:fix_replicated_rbac_race_condition
Jul 17, 2026
Merged

fm4v merged 11 commits into
ClickHouse:masterfrom
Slach:fix_replicated_rbac_race_condition

Conversation

@Slach

@Slach Slach commented Jun 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #106521
Related: #107097

Adds a regression test for a TOCTOU race in ReplicatedAccessStorage that could temporarily make a just-created access entity unresolvable by name.

ZooKeeperReplicator::refreshEntities(all=false), run by the background watch thread, read the /uuid children list via getChildrenWatch without holding mutex, and only then took the lock to call memory_storage.removeAllExcept(snapshot). A concurrent insertEntity creates the entity's znode first and only then takes mutex to put the entity into memory_storage, so it could commit an entity after the snapshot was taken but before the watch thread acquired the lock. removeAllExcept then evicted the freshly inserted entity from the local cache. Any statement resolving it by name in that window failed with There is no role ... (UNKNOWN_ROLE), even on the same session that had just created the role (e.g. CREATE USER ... DEFAULT ROLE, CREATE QUOTA ... TO).

The fix itself was merged separately via #107097 (serializing ZooKeeper read->publish sequences under refresh_mutex) and backported to 25.8, 26.3, 26.4 and 26.5, so this PR now only adds the test.

The test is a probabilistic stress regression test in test_replicated_users: it concurrently creates access entities and immediately references them by name while background churn keeps the /uuid children list volatile and many seeded entities widen the eviction window. Eviction of any freshly created entity is treated as a race detection (UNKNOWN_ROLE, THERE_IS_NO_PROFILE, UNKNOWN_USER). The detection logic was validated against an unpatched clickhouse-server 26.3.4.11, where it reproduces UNKNOWN_ROLE within ~17s; with the fix it never fails.

Originally observed as a flaky clickhouse-backup TestRBAC failure: https://github.com/Altinity/clickhouse-backup/actions/runs/26991435518/job/79652420544

Changelog category (leave one):

  • Not for changelog (changelog entry is not required)

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.7.1.1081 (included in 26.7 and later)
  • Backported to: 26.6.2.77

Slach and others added 4 commits June 5, 2026 09:01
…ntity

`ZooKeeperReplicator::refreshEntities(all=false)`, run by the background
watch thread, read the `/uuid` children list via `getChildrenWatch` without
holding `mutex` and only then took the lock to call
`memory_storage.removeAllExcept(snapshot)`. A concurrent `insertEntity`
(which creates the znode first and only then takes `mutex` to put the entity
into `memory_storage`) could commit an entity after the snapshot was taken
but before the watch thread acquired the lock, so `removeAllExcept` evicted
the freshly inserted entity. Any statement resolving it by name in that
window failed with `There is no role ... (UNKNOWN_ROLE)`, even on the same
session that had just created the role (e.g. `CREATE USER ... DEFAULT ROLE`,
`CREATE QUOTA ... TO`).

Acquire `mutex` before reading the children list so the snapshot is
consistent with the in-memory state, matching the `all=true` branch which
already reads entities from ZooKeeper under the lock.

Reproducer and analysis: ClickHouse#106521
Originally observed as a flaky clickhouse-backup TestRBAC failure:
https://github.com/Altinity/clickhouse-backup/actions/runs/26991435518/job/79652420544

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
…race

Stress test in `test_replicated_users`: concurrently create a role and
immediately reference it by name (`CREATE USER ... DEFAULT ROLE`,
`CREATE QUOTA ... TO`), with background churn keeping the `/uuid` children
list volatile and many seeded entities widening the eviction window. On the
buggy build the freshly created role is evicted from the local cache by the
background watch refresh and one of the referencing statements fails with
`There is no role ... (UNKNOWN_ROLE)`; with the fix it never does.

The detection logic was validated locally against an unpatched
clickhouse-server 26.3.4.11: it reproduces `UNKNOWN_ROLE` within ~17s.

Related: ClickHouse#106521

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Explain in the test why the stress driver is an in-container bash script
with background jobs and `wait`, rather than Python threads driving
`node.query` from the host (a host<->container round trip per iteration
would lower the iteration rate and weaken reproduction of this narrow
race) or `xargs -P` (only replaces the `& ... wait` fan-out while forcing
the worker bodies into a third level of nested quoting).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@nickitat nickitat added the can be tested Allows running workflows for external contributors label Jul 5, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e96f0f0]

Summary: ✅


AI Review

Summary

Adds a probabilistic integration regression test for the ReplicatedAccessStorage TOCTOU race fixed separately in #107097. I re-checked the current tests/integration/test_replicated_users/test.py at PR head e96f0f02554fdff248618efaef4b2d857aca18a1, including the prior inline threads, and the current script no longer has the vacuous-pass and ignored-worker-failure problems from earlier revisions. I did not find any remaining correctness, coverage-contract, or PR-metadata issues in this test-only change.

Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-critical-bugfix pr-must-backport Pull request should be backported intentionally. Use this label with great care! labels Jul 5, 2026
Slach and others added 2 commits July 14, 2026 16:58
…esolution

The fix for the UNKNOWN_ROLE race was already merged into master via
ClickHouse#107097 (refresh_mutex +
scoped mutex locks at publish points). The merge of master into this
branch (db83926) incorrectly kept the older approach's outer
std::lock_guard lock{mutex} before getChildrenWatch on top of it,
producing a double lock of mutex in refreshEntities and failing the
build with -Wshadow and -Wthread-safety-analysis:

https://github.com/ClickHouse/ClickHouse/actions/runs/29334767283/job/87091850817

Remove the leftover hunk so ZooKeeperReplicator.cpp matches master;
this PR now only adds the integration test.

Co-Authored-By: Claude Fable 5 <[email protected]>
…in the test

The victim chain does by-name lookups of three just-created entities
(settings profile, role, user), and the ReplicatedAccessStorage race is
generic across entity types, but the detection matched only UNKNOWN_ROLE.
If the race hit the profile lookup first, the multiquery failed with
THERE_IS_NO_PROFILE before ever reaching the role reference and the
iteration was silently retried, so a buggy build could print NO_RACE.
Match UNKNOWN_ROLE, THERE_IS_NO_PROFILE, UNKNOWN_USER and the generic
"There is no" text from IAccessStorage::throwNotFound instead.

ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@Slach Slach changed the title Fix race in ReplicatedAccessStorage that could evict a just-created entity (UNKNOWN_ROLE) Add regression test for ReplicatedAccessStorage race that evicted a just-created entity (UNKNOWN_ROLE) Jul 15, 2026
The fix itself was merged via ClickHouse#107097,
so this PR is test-only now and Bugfix validation no longer applies.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-not-for-changelog This PR should not be mentioned in the changelog and removed pr-critical-bugfix labels Jul 15, 2026
Two review findings on the in-container driver:

1. `local w=$1 name="race_$w"` dies under `set -u` with "w: unbound
   variable": bash expands the whole `local` command line before
   performing any of its assignments. Every victim worker died before
   its first iteration, and since a bare `wait` discards worker exit
   statuses, the driver still printed `NO_RACE` - the test passed
   without issuing a single victim query. Split the declarations and
   wait on each collected worker PID, turning nonzero worker exits into
   a driver failure (`WORKER_FAILED`, exit 1).

2. The detection `case` silently retried every client failure other
   than the targeted not-found errors until timeout. Now a clean
   success continues the loop, the specific race detections stop it,
   and any other nonzero client result fails the worker and the driver,
   so the test cannot pass while never exercising the intended path.

Also fail the driver if seeding the profiles fails (`SEED_FAILED`).

Verified with a stub clickhouse-client: clean run -> NO_RACE (exit 0),
race errors (UNKNOWN_ROLE / THERE_IS_NO_PROFILE) -> RACE_DETECTED
(exit 0), unrelated error -> WORKER_FAILED (exit 1), failing seed ->
SEED_FAILED (exit 1).

ClickHouse#106525 (comment)
ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
Slach and others added 2 commits July 15, 2026 21:01
The integration-test containers mount the server binary at
/usr/bin/clickhouse and have no clickhouse-client symlink, so every
client call in the driver failed with "clickhouse-client: command not
found". Before the previous commit hardened the driver this was
silently swallowed and the test vacuously printed NO_RACE; now it
failed the flaky check as SEED_FAILED:

https://github.com/ClickHouse/ClickHouse/actions/runs/29428630429/job/87398275184?pr=106525
ClickHouse#106525

Re-verified all driver paths with a stub `clickhouse` handling the
`client` subcommand: clean run -> NO_RACE (exit 0), race error ->
RACE_DETECTED (exit 0), unrelated error -> WORKER_FAILED (exit 1).

Co-Authored-By: Claude Fable 5 <[email protected]>
The churn is part of the test's contract - it is what keeps the /uuid
children list volatile and widens the eviction window - but its client
failures were redirected to /dev/null and ignored, so a driver whose
churn queries stopped working would still report NO_RACE after silently
dropping the very stressor that makes the race reproducible. Handle
churn client failures like victim ones: record the output and fail the
worker and the driver (WORKER_FAILED, exit 1). No race detection is
needed in churn: IF NOT EXISTS/IF EXISTS make its statements no-ops
instead of errors when the race hides the entity.

Verified with a stub clickhouse-client failing only churn_* queries:
the driver now reports WORKER_FAILED (exit 1) instead of NO_RACE; the
clean, race-detection and victim-unexpected-error paths are unchanged.

ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 17, 2026
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 17, 2026
clickhouse-gh Bot pushed a commit that referenced this pull request Jul 17, 2026
…rage race that evicted a just-created entity (UNKNOWN_ROLE)
clickhouse-gh Bot added a commit that referenced this pull request Jul 17, 2026
Backport #106525 to 26.6: Add regression test for ReplicatedAccessStorage race that evicted a just-created entity (UNKNOWN_ROLE)
@fm4v fm4v removed the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 17, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…in the test

The victim chain does by-name lookups of three just-created entities
(settings profile, role, user), and the ReplicatedAccessStorage race is
generic across entity types, but the detection matched only UNKNOWN_ROLE.
If the race hit the profile lookup first, the multiquery failed with
THERE_IS_NO_PROFILE before ever reaching the role reference and the
iteration was silently retried, so a buggy build could print NO_RACE.
Match UNKNOWN_ROLE, THERE_IS_NO_PROFILE, UNKNOWN_USER and the generic
"There is no" text from IAccessStorage::throwNotFound instead.

ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Two review findings on the in-container driver:

1. `local w=$1 name="race_$w"` dies under `set -u` with "w: unbound
   variable": bash expands the whole `local` command line before
   performing any of its assignments. Every victim worker died before
   its first iteration, and since a bare `wait` discards worker exit
   statuses, the driver still printed `NO_RACE` - the test passed
   without issuing a single victim query. Split the declarations and
   wait on each collected worker PID, turning nonzero worker exits into
   a driver failure (`WORKER_FAILED`, exit 1).

2. The detection `case` silently retried every client failure other
   than the targeted not-found errors until timeout. Now a clean
   success continues the loop, the specific race detections stop it,
   and any other nonzero client result fails the worker and the driver,
   so the test cannot pass while never exercising the intended path.

Also fail the driver if seeding the profiles fails (`SEED_FAILED`).

Verified with a stub clickhouse-client: clean run -> NO_RACE (exit 0),
race errors (UNKNOWN_ROLE / THERE_IS_NO_PROFILE) -> RACE_DETECTED
(exit 0), unrelated error -> WORKER_FAILED (exit 1), failing seed ->
SEED_FAILED (exit 1).

ClickHouse#106525 (comment)
ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The integration-test containers mount the server binary at
/usr/bin/clickhouse and have no clickhouse-client symlink, so every
client call in the driver failed with "clickhouse-client: command not
found". Before the previous commit hardened the driver this was
silently swallowed and the test vacuously printed NO_RACE; now it
failed the flaky check as SEED_FAILED:

https://github.com/ClickHouse/ClickHouse/actions/runs/29428630429/job/87398275184?pr=106525
ClickHouse#106525

Re-verified all driver paths with a stub `clickhouse` handling the
`client` subcommand: clean run -> NO_RACE (exit 0), race error ->
RACE_DETECTED (exit 0), unrelated error -> WORKER_FAILED (exit 1).

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
The churn is part of the test's contract - it is what keeps the /uuid
children list volatile and widens the eviction window - but its client
failures were redirected to /dev/null and ignored, so a driver whose
churn queries stopped working would still report NO_RACE after silently
dropping the very stressor that makes the race reproducible. Handle
churn client failures like victim ones: record the output and fail the
worker and the driver (WORKER_FAILED, exit 1). No race detection is
needed in churn: IF NOT EXISTS/IF EXISTS make its statements no-ops
instead of errors when the race hides the entity.

Verified with a stub clickhouse-client failing only churn_* queries:
the driver now reports WORKER_FAILED (exit 1) instead of NO_RACE; the
clean, race-detection and victim-unexpected-error paths are unchanged.

ClickHouse#106525 (comment)

Co-Authored-By: Claude Fable 5 <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ace_condition

Add regression test for ReplicatedAccessStorage race that evicted a just-created entity (UNKNOWN_ROLE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore 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.

ReplicatedAccessStorage: freshly created role evicted from local cache by background watch refresh (TOCTOU race) -> UNKNOWN_ROLE

6 participants